Skip to content

Instantly share code, notes, and snippets.

View xdayeh's full-sized avatar
🤘
web3

Mohammad AbuDayeh xdayeh

🤘
web3
View GitHub Profile
@xdayeh
xdayeh / GetPrice.js
Created February 2, 2023 21:23
Retrieve the price of any bsc token from it's address BNB smart chain
(async () => {
const Token = '0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82';
const abi = [{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},]
const web3 = new Web3(Web3.givenProvider);
const contract = new web3.eth.Contract(abi, '0x10ed43c718714eb63d5aa57b78b54704e256024e');
const result = await contract.methods.getAmountsOut((10 ** 18) + '', [Token, '0xe9e7cea3dedca5984780bafc599bd69add087d56']).call();
const price = result[1] / (10 ** 18);
console.log('$' + price.toFixed(2) );
})();