2

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

  1. 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();
  1. Call of getAmountsOut method on router contract in bscscan (results in 20000 BUSD -> 163 BAKE)

enter image description here

  1. Screenshot from PancakeSwap UI (results in 20000 BUSD -> 12100 BAKE)

screenshot from PancakeSwap UI

TylerH
  • 20,799
  • 66
  • 75
  • 101
PokatilovArt
  • 1,111
  • 11
  • 13

1 Answers1

1

If you compare the path/route on the pcs screenshot to the path on bsc, you should notice that it doesn't use the same path.

You will get a different price if you Trade on pair BUSD -> BAKE vs. BUSD -> BNB -> BAKE

Also please notice the price impact on the pool.

noscript
  • 77
  • 1
  • 12
  • What do you mean by `notice the price impact on the pool`, do you meant hat `getAmountsOut` doesn't care about price impact and that's why its showing little bit bigger result? – Luka Samkharadze Apr 11 '22 at 08:39
  • @noscript finally I've been looking for this all over the place. Any idea on how to get the intermediary token in the route automatically? I mean without having to include it myself? – grenos Nov 24 '22 at 20:16