Swap Router

Stateless router for executing token swaps against WarpX V3 pools with optimal routing.

Exact Input (Single Hop)

struct ExactInputSingleParams {    address tokenIn;    address tokenOut;    uint24 fee;    address recipient;    uint256 deadline;    uint256 amountIn;    uint256 amountOutMinimum;    uint160 sqrtPriceLimitX96;}function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut)

Swaps an exact amount of input tokens for as many output tokens as possible in a single pool.

Parameters:

Field
Description

tokenIn

Token to swap from

tokenOut

Token to swap to

fee

Pool fee tier (100, 500, 3000, 10000)

recipient

Address to receive output tokens

deadline

Transaction expiry timestamp

amountIn

Exact amount of input tokens

amountOutMinimum

Minimum output (slippage protection)

sqrtPriceLimitX96

Price limit (0 = no limit)

Example:

// Swap exactly 1000 USDC for WETHISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({    tokenIn: USDC,    tokenOut: WETH,    fee: 3000,    recipient: msg.sender,    deadline: block.timestamp + 300,    amountIn: 1000e6,    amountOutMinimum: 0.4e18,  // Min 0.4 WETH    sqrtPriceLimitX96: 0});uint256 amountOut = swapRouter.exactInputSingle(params);

Exact Input (Multi-Hop)

struct ExactInputParams {    bytes path;    address recipient;    uint256 deadline;    uint256 amountIn;    uint256 amountOutMinimum;}function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut)

Swaps an exact amount of input tokens through multiple pools for maximum output.

Path Encoding:

  • Format: abi.encodePacked(tokenA, feeAB, tokenB, feeBC, tokenC)

  • Example: USDC β†’ (3000) β†’ WETH β†’ (3000) β†’ DAI

Example:


Exact Output (Single Hop)

Swaps as few input tokens as possible for an exact amount of output tokens.

Example:


Exact Output (Multi-Hop)

Swaps through multiple pools to receive an exact output amount.

Path Encoding (Reversed):

  • Format: abi.encodePacked(tokenC, feeBC, tokenB, feeAB, tokenA)

  • For USDC β†’ WETH β†’ DAI, encode as: DAI β†’ WETH β†’ USDC

Example:


Token Approvals

Before swapping, approve the router to spend your tokens:


Handling Native ETH

For ETH swaps, use WETH as tokenIn or tokenOut and send ETH with the transaction:


Price Limits

Set sqrtPriceLimitX96 to prevent execution beyond a certain price:

  • Zero for one (token0 β†’ token1): Price cannot go below this value

  • One for zero (token1 β†’ token0): Price cannot go above this value

  • Set to 0: No price limit


Best Practices

  1. Always set slippage protection: Use amountOutMinimum / amountInMaximum

  2. Use deadlines: Set reasonable expiry times (e.g., 5 minutes)

  3. Multi-hop carefully: More hops = more gas + more slippage

  4. Check routes: Use off-chain router to find optimal paths

  5. Handle refunds: For exact output swaps, refund unused input tokens


Interface

Last updated