-
-
Save gokusenz/2c30fc45e98b464e965eb78d9dafeab1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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