Skip to main content
Crypto Readout

Crypto markets, protocols and policy

Should I Call the Router or Pair Directly for Swaps?

Call the Uniswap V2 router for user-facing swaps; call a pair directly only when your contract transfers tokens atomically and checks output on every route.

By Crypto Readout Editorial3 min read#5a2f6d

Should I Call the Router or Pair Directly for Swaps?

For a user-facing swap, call Router02; use a pair directly only when your own contract transfers tokens and checks output in the same transaction. That condition can make a direct call cheaper for contract-based routing, arbitrage or flash swaps.

Router02 handles the checks and the route

Router02 is the safer entry point for a swap integration because it pulls approved tokens, calculates the route and applies the user’s minimum output and deadline. For a token-to-token swap, swapExactTokensForTokens takes amountIn, amountOutMin, a token-address path, a recipient to and a deadline. It returns an amounts[] array for the route’s hops.

The user approves the router to spend the input token, then the router pulls it and sends it through each pair in the path. amountOutMin sets the least output the user will accept; the deadline limits how long the transaction remains valid. If the output is too low, the call reverts with INSUFFICIENT_OUTPUT_AMOUNT; if the deadline has passed, it reverts with EXPIRED. The [Router02 reference](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02) documents these arguments and the fee-on-transfer variants.

This is also where ETH handling belongs. Uniswap v2 pairs hold WETH, while router functions can wrap or unwrap ETH for the user. A direct pair call works with the pair’s two ERC-20 tokens, so an integration handling ETH must do the WETH conversion itself.

Fee-on-transfer tokens need special handling because the pair may receive less than the nominal input. Router02 provides SupportingFeeOnTransferTokens variants for those swaps. When ShibaSwap is part of the route, verify its router and token behavior against the integration you are building; the shiba swap topic covers what users can do on the exchange and how it fits into this choice.

A direct pair call saves gas but shifts the checks to your contract

A direct call can save the router’s gas, but pair.swap(amount0Out, amount1Out, to, data) does not pull tokens or set a user’s minimum output. Your contract must calculate output using getReserves and the 0.997 fee factor, transfer input tokens to the pair, then call swap() atomically. The pair checks that its constant-product invariant still holds after the 0.3% fee; it does not check whether the result meets your product’s slippage limit. The [Pair reference](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/pair) describes the swap signature and transfer-first flow, while the [Uniswap v2 whitepaper](https://app.uniswap.org/whitepaper.pdf) explains the fee-adjusted invariant and flash swaps.

Atomicity matters for funds as well as pricing. If one transaction transfers tokens to a pair and a later transaction calls swap(), someone else can interact with the pair first. Anyone can call swap() or skim() to take excess balances, so do not leave input tokens sitting at the pair between transactions.

There is another fork-specific trap: pair addresses are derived from the factory address and the pair’s init code hash. A Uniswap v2 library copied unchanged uses Uniswap’s values, so it can compute the wrong pair address for ShibaSwap. Use ShibaSwap’s own factory and init code hash when deriving its pairs.

  • Choose Router02 for a user-facing swap that needs slippage and deadline protection, multi-hop routing, ETH handling or fee-on-transfer support.
  • Choose direct pair.swap() when your contract already routes atomically, computes output and enforces its own minimum. It suits gas-sensitive arbitrage and flash-swap flows where that control is worth the extra code.

For most product integrations, Router02 is the right call path. Go directly to the pair only when your contract can keep the transfer, swap and output check together in one transaction.