0

In Uniswap the function router02#swapExactETHForTokens(...) and other similar ones which do a swap, require a parameter for "mininum amount out".

function swapExactETHForTokens(
  uint amountOutMin,
  //.........

How is it usually calculated?


The same question goes in regards to the similar parameter - amountInMax which is used, for instance in

function swapTokensForExactETH(
   uint amountOut, 
   uint amountInMax,  // !
   //.....

1 Answer 1

1

When calling the router contract to issue a swapExactEthForTokens swap, due to the fluctuation of the market, the router takes a parameter called AmountOutMin which is how much token you want at least from this transaction , so you could still get more tokens than the AmountOutMin tokens initially set , its just you cant receive less tokens than the amountOutMin specified or the contract will fail.

Same goes for amountInMax vice-versa.

You can get these arguments by calling getAmountsIn and getAmountsOut function for a given swap-token pair. These functions are given in uniswap, pancakeswap router as well.

3
  • My question -- "how to calculate them in web3.js in order to pass values calculated by them futher into a swap function"?
    – Markus736
    Feb 28, 2022 at 12:24
  • getAmountsIn(..) and getAmountsOut(...) - how precisely would I call them? Can you give an example? I've not found any meaningful one.
    – Markus736
    Feb 28, 2022 at 12:26
  • Okay, for this you need pancakeswap's router address. Following code example in vanilla js using web.js: - window.web3 = new Web3(ethereum); await ethereum.enable(); const routerContract = new web3.eth.Contract(contractAbi, routerAddress); async function getAmountsOut(){ let result = await routerContract.methods.getAmountsOut(amountIn, path[]).call(); return result; } Mar 4, 2022 at 10:18

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.