Skip to content

mcUniswapSwapRouter

A pre-configured multichain contract instance for interacting with Uniswap's SwapRouter across different networks. This allows you to perform token swaps and related operations across multiple chains using a unified interface.

export const mcUniswapSwapRouter = getMultichainContract<
  typeof UniswapSwapRouterAbi
>({
  abi: UniswapSwapRouterAbi,
  deployments: [
    ["0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45", arbitrum.id],
    ["0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45", optimism.id],
    ["0x2626664c2603336E57B271c5C0b26F421741e481", base.id]
  ]
})

Usage

import { mcUniswapSwapRouter } from "@biconomy/abstractjs"
import { optimism } from "viem/chains"
 
// Build an exact input single swap
const swapOp = await mcUniswapSwapRouter.build({
  type: "exactInputSingle",
  data: {
    chainId: optimism.id,
    args: [{
      tokenIn: tokenInAddress,
      tokenOut: tokenOutAddress,
      fee: 3000,
      recipient: userAddress,
      deadline: BigInt(Math.floor(Date.now() / 1000) + 900),
      amountIn: parseUnits("100", 6),
      amountOutMinimum: 1n,
      sqrtPriceLimitX96: 0n
    }],
    // Some complex userOps will sometimes require more gas than the default. All surpluses are returned to the users smart account in native token
    gasLimit: 1000000n
  }
})

Parameters

Common parameters for building swap instructions:

ParameterTypeDescription
typestringThe swap function to call (e.g., "exactInputSingle", "exactInput")
data.chainIdnumberThe chain ID where the swap should be executed
data.argsSwapParams[]The swap parameters
data.gasLimitbigintOptional gas limit for the swap

SwapParams

ParameterTypeDescription
tokenInAddressAddress of input token
tokenOutAddressAddress of output token
feenumberPool fee tier (500, 3000, 10000)
recipientAddressAddress receiving the output tokens
deadlinebigintTimestamp deadline for execution
amountInbigintAmount of input tokens
amountOutMinimumbigintMinimum output tokens to receive
sqrtPriceLimitX96bigintPrice limit for the swap

Example

Here's a complete example showing how to perform a token swap with approval:

import { mcUniswapSwapRouter, mcUSDC } from "@biconomy/abstractjs"
import { optimism } from "viem/chains"
import { parseUnits } from "viem"
 
// First approve the router
const approval = mcUSDC.build({
  type: "approve",
  data: {
    chainId: optimism.id,
    args: [
      mcUniswapSwapRouter.addressOn(optimism.id),
      parseUnits("100", 6)  // 100 USDC
    ]
  }
})
 
// Build the swap
const swap = mcUniswapSwapRouter.build({
  type: "exactInputSingle",
  data: {
    chainId: optimism.id,
    args: [{
      tokenIn: mcUSDC.addressOn(optimism.id),
      tokenOut: wethAddress,
      fee: 3000,
      recipient: userAddress,
      deadline: BigInt(Math.floor(Date.now() / 1000) + 900),
      amountIn: parseUnits("100", 6),
      amountOutMinimum: parseEther("0.05"), // 0.05 ETH minimum
      sqrtPriceLimitX96: 0n
    }],
    // Some complex userOps will sometimes require more gas than the default. All surpluses are returned to the users smart account in native token
    gasLimit: 1000000n
  }
})

Error Handling

The contract will throw an error if:

  • Attempting to swap on an unsupported chain
  • Insufficient token approval
  • Price impact too high
  • Deadline expired
  • Slippage tolerance exceeded