0

For estimation of input amount of tokens while calling quoteExactOutputSingle I use following for uni v2

const pair = await Fetcher.fetchPairData(tokenFrom, tokenTo, provider);

const route = new Route([pair], tokenFrom, tokenTo);
const amountOut = new TokenAmount(tokenTo, amountTo.toString());

const trade = Trade.exactOut(route, amountOut);

const amountIn = ethers.BigNumber.from(trade.inputAmount.raw.toString());

and for uni v3

const quoterContract = getUniswap3QuaterContract();
const amountIn = await quoterContract.callStatic.quoteExactOutputSingle(
    tokenFromAddress,
    tokenToAddress,
    fee,
    amountOut,
    0
);

Does anyone know how to estimate token amount in for sushiswap?

1 Answer 1

0

How to fetch amountOut and AmountIn from Sushiswap

Sushiswap V2Router02 address is: 0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F

Follow the next steps to interact with it.

  1. Get the ABI from https://etherscan.io/address/0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F#code (at the button of the page)

  2. Implement the code like this:

(Using EtherJS)

const USDC = { address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', decimal: 6 };
const WETH = { address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', decimal: 18 }
const SushiSwapRouterABI = [.........]
var amount = 100

let quoterContractSushi = new ethers.Contract('0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F', SushiSwapRouterABI, signer)

// For Amount In
// As in ABI: getAmountsIn(uint256,address[])
let quoteIn = await quoterContractSushi.getAmountsIn(BigInt(amount * (10 ** USDC.decimal)), [USDC.address, WETH.address])

   console.log(quoteIn[0])

//For Amount Out
// As in ABI: getAmountsOut(uint256,address[])
let quoteOut = await quoterContractSushi.getAmountsOut(BigInt(amount * (10 ** USDC.decimal)), [USDC.address, WETH.address])

   console.log(quoteOut[1])
  • Since it's a view-flagged function on the contract, "staticCall" is unnecessary.

Be aware of the fact that the return value In both methods must be a

amounts   uint256[]

Where the [0] is the amount In and the [1] is the amount Out

Good luck

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.