Hello eth communities..

I am trying to interact with ethereum chain using programming and I'm using getAmountsOut to get swap path quote using sushiswap's contract. But the result in my vscode and sushiswap UI is veryyyy different even the path sushiswap UI(user interface) used is the same with mine(using only v2). See this senario

in my code the path WETH -> DPI gives me 8.988475884524729051 DPI

on sushiswap UI the path WETH- DPI gives me 9.26355

The path i used is only sushiswapv2

the path sushiswap UI used is sushiswapV2

Please help..... Even i deduct the gas from sushiswap UI resut, still the results are not even closer.... Also does getAmountsOut consider the gas cost?

  • getAmountsOut should return the same as UI, can find examples of that from me in their discord. UI adds some padding to slippage but getAmount(s)Out can line line up 100%. So you should share more details of how you are getting to the result. Should also check your result on app.sushi.com instead of www.sushi.com to avoid some difference from tines.
    – Maka
    Commented Sep 9, 2023 at 17:17
  • with app.sushiswap the result is exactly the same. Can u please tell me why it is not same with www.sushiswap ? and does getAmountsOut consider the gas cost? Commented Sep 9, 2023 at 17:24
  • No it doesn't consider gas cost, just 0.3% swapfee. It's hard to say without seeing how you get to that result, why it is different. Maybe not every input is what you think it is. I would copy the getAmountOut function and call getReserves() on the pools in the path and step through it manually, to find out what is going on.
    – Maka
    Commented Sep 9, 2023 at 17:40
  • This is the different i get..see the screenshot... the right one is app.sushiswap and the left one is www.sushiswap... my result is the same with the right one snipboard.io/zgXAxJ.jpg Commented Sep 9, 2023 at 18:09

1 Answer 1


I tested it, the prices are different across the two urls, app.sushi.com shows the same as getAmountOut with 2+% price impact.

www.sushi.com shows more as it is aggregating from multiple pools to give you the best price. If anything it is just an inaccuracy in what it is showing as the path on the newer UI.

enter image description here


Hello brother is there a way i can calculate the amount i get after swap using resrve0 and reserve1. Example The pool MKR/WETH have WETH reserve of 137562296675513055507 and MKR reserve of 95089610234884719383 If i input 1 ETH How to calculate the amount of MKR i get??

On the simplest level you can call getAmountOut, or copy the way the function does it from the Uniswap library.

    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
        require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
        require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
        uint amountInWithFee = amountIn.mul(997);
        uint numerator = amountInWithFee.mul(reserveOut);
        uint denominator = reserveIn.mul(1000).add(amountInWithFee);
        amountOut = numerator / denominator;

An example implementation might look like:

>>> def getAmountOut(amountIn: int, reserveIn: int, reserveOut: int) -> int:
...   amountInWithFee = amountIn * 997
...   numerator = amountInWithFee * reserveOut
...   denominator = (reserveIn * 1000) + amountInWithFee
...   amountOut = numerator // denominator
...   return int(amountOut)
>>> getAmountOut(1*10**18,95089610234884719383,137562296675513055507)
>>> getAmountOut(1*10**18,137562296675513055507,95089610234884719383)/10**18
>>> getAmountOut(1*10**18,95089610234884719383,137562296675513055507)/10**18

Have included the reserves in reverse of suggested as I think you got them back to front in the question.

The last result suggests 1.4273540241478273MKR for 1 ETH which is close to the current market rate.

  • so to get best rate like suhiswap UI, i must use reserve0 and reserve1 and calcuate the quote right? or is thier other way to tell getAmountsOut to use specific pool? I see that there is 2 pools in sushiswap. The one is sushi.com/pool/1%3A0x34b13f8cd184f55d0bd4dd1fe6c07d46f245c7ed , which gives 9.22DPI and the other one is sushi.com/pool/137%3A0xcb3716006b3b468624b2a246505ec0d22673e077 which gives 8.76DPI for same input 0.35 ETH Commented Sep 9, 2023 at 18:50
  • One is on Polygon the other Ethereum they won't be the same. v2 can only have 1 pool for each pair, in order to get the price from the newer UI you would use the route processor and pull different amounts from different pool routes by setting the share field in each route. Sushiswap has a system called tines for this, to do yourself you would need a good understanding of v2 and then additional understanding of the other pool types on the platform. With that you would parse data for all relevant pools to calculate the optimal fractions.
    – Maka
    Commented Sep 9, 2023 at 19:41
  • ok ... Thanks very much Maka. U really helped me a lot... Commented Sep 9, 2023 at 20:21
  • Much love and good luck on your journey
    – Maka
    Commented Sep 9, 2023 at 20:22
  • Hello brother is there a way i can calculate the amount i get after swap using resrve0 and reserve1. Example The pool MKR/WETH have WETH reserve of 137562296675513055507 and MKR reserve of 95089610234884719383 If i input 1 ETH How to calculate the amount of MKR i get?? Commented Sep 10, 2023 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.