Uniswap – Resolving Discrepancies Between GetAmountsOut and Sushiswap User Interface

cpp-ethereumethereum-wallet-dappsushiswapuniswap

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?

Best Answer

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

edit:

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)
1427354024147827329
>>> getAmountOut(1*10**18,137562296675513055507,95089610234884719383)/10**18
0.6842149439181904
>>> getAmountOut(1*10**18,95089610234884719383,137562296675513055507)/10**18
1.4273540241478273
>>>

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.

Related Topic