I've been able to do swaps through Uniswap's peripheral smart contract Router 02 by calling any of the functions such as:

function swapTokensForExactTokens(
  uint amountOut,
  uint amountInMax,
  address[] calldata path,
  address to,
  uint deadline
) external returns (uint[] memory amounts);

However, none of these functions has any field referring to the slippage (amount the price moves in a trading pair between when a transaction is submitted and when it is executed), and I don't know exactly at which stage the slippage is set before launching the swap transaction.

Is there any way to handle the slippage tolerance programatically when executing a swap transaction?

2 Answers 2


Yes, you can adjust it with that function.

The Uniswap router swap functions trade asset A for asset B, with slippage. Mostly they are of type "give A, get B, while either the amount of A or B remains static". So you for example have a swap "I want to pay x amount of A tokens and get as many B tokens as possible" or "I want to pay as few A tokens as possible and get x amount of B tokens".

In your case you are using a function which gives a minimum amount of tokenA and gets a certain numbers of tokenB out. So the amountOut is a static number which you define, and amountInMax is the maximum you are willing to input tokens A to get the static amount of tokens B.

So your slippage is defined by amountInMax.

  • 1
    I see! So I guess it means that I should apply the percentage, in this case, over the amountInMax to determine the max slippage allowed. For example, if the calculated amountInMax was 150, but I have defined 5% slippage (too high, but just an example), it means that I should adjust the amountInMax to 157,5. Is that correct? Commented Feb 8, 2021 at 14:29
  • Basically, yes. Commented Feb 8, 2021 at 20:40

You need to set the slippage when you set the route:

The Route entity represents one or more ordered Uniswap pairs with a fully specified path from input token to output token.

after stuying uniswap-sdk-core-repo

 import JSBI from 'jsbi'
 import { CurrencyAmount, TradeType, Percent } from "@uniswap/sdk-core";
 import { AlphaRouter } from "@uniswap/smart-order-router";

 // goerli chainId=5
 const router = new AlphaRouter({ chainId: 5, provider: web3Provider });

// maximum percentage of slippage to fulfill the order
  const perSlippage = new Percent(slippageAmount, 100);
  const wei = ethers.utils.parseUnits(inputAmount.toString(), decimalsOfToken0);
  const currencyAmount = CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(wei));

 //building the route
  const route = await router.route(currencyAmount, UNI, TradeType.EXACT_INPUT, {
    recipient: walletAddress,
    slippageTolerance: perSlippage,
    deadline: enterDeadline,

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.