Skip to content

Instantly share code, notes, and snippets.

@gokusenz
Forked from earthchie/DOP_price.js
Created May 13, 2021 16:29
Show Gist options
  • Save gokusenz/2c30fc45e98b464e965eb78d9dafeab1 to your computer and use it in GitHub Desktop.
Save gokusenz/2c30fc45e98b464e965eb78d9dafeab1 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