Skip to content

Instantly share code, notes, and snippets.

@earthchie
Created May 13, 2021 15:27
Show Gist options
  • Save earthchie/ad8b15168f14303f6203c15cc4df4938 to your computer and use it in GitHub Desktop.
Save earthchie/ad8b15168f14303f6203c15cc4df4938 to your computer and use it in GitHub Desktop.
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/');
const BEP20 = {
BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
DOP: '0x844fa82f1e54824655470970f7004dd90546bb28',
};
const Contract = {
router: new ethers.Contract(
'0x10ED43C718714eb63d5aA57B78B54704E256024E', // pancake V2 router
['function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)'],
provider
)
}
getExchangeRate(BEP20.DOP, BEP20.BUSD).then(price => {
console.log(`1 DOP =`, price, 'BUSD');
});
async function getExchangeRate(tokenIn, tokenOut){
const amounts = await Contract.router.getAmountsOut(ethers.utils.parseUnits('1', 18), [tokenIn, tokenOut]);
return amounts[1].toString()/1e18;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment