I'm making first steps in web3, that's why question can be silly.
I can't figure out, why results from getAmountsOut method of Pancakeswap's v2 router differs a lot from swap results I see on a frontend of Pancakeswap.
BAKE decimals should be everywhere 18 automatically, so I don't understand the reason of such different results.
Example BUSD-BAKE swap
- My code (results in 20000 BUSD -> 163 BAKE)
const Web3 = require('web3');
const abis = require('./abis');
const web3 = new Web3('wss://bsc-ws-node.nariox.org:443');
const amountInBUSD = web3.utils.toBN(web3.utils.toWei('20000'));
const busdTokenAddress = "0xE02dF9e3e622DeBdD69fb838bB799E3F168902c5";
const bakeTokenAddress = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
const init = async () => {
const pancakeSwap = new web3.eth.Contract(
abis.pancakeSwap.router,
"0x10ED43C718714eb63d5aA57B78B54704E256024E"
);
web3.eth.subscribe('newBlockHeaders')
.on('data', async block => {
const bakeOutput = await pancakeSwap.methods.getAmountsOut(amountInBUSD, [busdTokenAddress, bakeTokenAddress]).call();
console.log(`PancakeSwap BUSD-BAKE: ${web3.utils.fromWei(amountInBUSD.toString())} -> ${web3.utils.fromWei(bakeOutput[1].toString())}`);
})
.on('error', error => {
console.log(error);
});
}
init();
- Call of getAmountsOut method on router contract in bscscan (results in 20000 BUSD -> 163 BAKE)
- Screenshot from PancakeSwap UI (results in 20000 BUSD -> 12100 BAKE)