SDK for routing swaps across Uniswap V2, V3, and V4 protocols with automatic route optimization and gas-efficient execution.
—
Core swap routing functionality for encoding transactions across Uniswap V2, V3, and V4 protocols. The SwapRouter class handles single and multi-protocol routes with automatic optimization and gas-efficient execution.
Abstract class representing the Uniswap V2 + V3 SwapRouter02 contract, providing static methods for encoding swap transactions.
/**
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades
*/
abstract class SwapRouter {
/** Contract interface for ISwapRouter02 */
static INTERFACE: Interface;
/**
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade
* @param trades - Single trade or array of trades to execute
* @param options - Options for the call parameters
* @returns Method parameters with calldata and value
*/
static swapCallParameters(
trades: Trade<Currency, Currency, TradeType>
| V2Trade<Currency, Currency, TradeType>
| V3Trade<Currency, Currency, TradeType>
| MixedRouteTrade<Currency, Currency, TradeType>
| (V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType>)[],
options: SwapOptions
): MethodParameters;
/**
* Produces call parameters for swap and add liquidity operations
* @param trades - Trades to execute before adding liquidity
* @param options - Swap and add options
* @param position - Target liquidity position
* @param addLiquidityOptions - Options for adding liquidity
* @param tokenInApprovalType - Approval type for input token
* @param tokenOutApprovalType - Approval type for output token
* @returns Method parameters with calldata and value
*/
static swapAndAddCallParameters(
trades: AnyTradeType,
options: SwapAndAddOptions,
position: Position,
addLiquidityOptions: CondensedAddLiquidityOptions,
tokenInApprovalType: ApprovalTypes,
tokenOutApprovalType: ApprovalTypes
): MethodParameters;
}Configuration options for swap operations including slippage tolerance, recipient, and validation.
/**
* Options for producing the arguments to send calls to the router
*/
interface SwapOptions {
/** How much the execution price is allowed to move unfavorably from the trade execution price */
slippageTolerance: Percent;
/** The account that should receive the output. If omitted, output is sent to msg.sender */
recipient?: string;
/** Either deadline (when the transaction expires, in epoch seconds), or previousBlockhash */
deadlineOrPreviousBlockhash?: Validation;
/** The optional permit parameters for spending the input */
inputTokenPermit?: PermitOptions;
/** Optional information for taking a fee on output */
fee?: FeeOptions;
}
/**
* Extended options for swap and add operations
*/
interface SwapAndAddOptions extends SwapOptions {
/** The optional permit parameters for pulling in remaining output token */
outputTokenPermit?: PermitOptions;
}Return type for swap encoding methods containing transaction calldata and value.
/**
* Parameters for calling router methods
*/
interface MethodParameters {
/** Hex-encoded calldata for the transaction */
calldata: string;
/** Hex-encoded value to send with the transaction */
value: string;
}Usage Examples:
import { SwapRouter, Trade, SwapOptions } from "@uniswap/router-sdk";
import { CurrencyAmount, TradeType, Percent, Token } from "@uniswap/sdk-core";
// Create a simple V3 trade
const trade = new V3Trade(route, amount, TradeType.EXACT_INPUT);
// Configure swap options
const options: SwapOptions = {
slippageTolerance: new Percent(50, 10000), // 0.5%
recipient: "0x742d35Cc6435C6329Eb54F0d86C05B1E11a02E6B",
deadlineOrPreviousBlockhash: Math.floor(Date.now() / 1000) + 1800, // 30 minutes
};
// Generate swap call parameters
const { calldata, value } = SwapRouter.swapCallParameters(trade, options);
// Use with ethers.js
const tx = await wallet.sendTransaction({
to: SWAP_ROUTER_ADDRESS,
data: calldata,
value: value,
});// Multi-protocol trade example
const aggregatedTrade = new Trade({
v2Routes: [{ routev2: v2Route, inputAmount, outputAmount }],
v3Routes: [{ routev3: v3Route, inputAmount, outputAmount }],
tradeType: TradeType.EXACT_INPUT
});
const params = SwapRouter.swapCallParameters(aggregatedTrade, options);// Swap and add liquidity example
import { Position } from "@uniswap/v3-sdk";
const position = new Position({
pool: v3Pool,
liquidity: targetLiquidity,
tickLower: -887220,
tickUpper: 887220
});
const swapAndAddOptions: SwapAndAddOptions = {
...options,
outputTokenPermit: {
v: 27,
r: "0x...",
s: "0x...",
deadline: deadline,
amount: permitAmount
}
};
const params = SwapRouter.swapAndAddCallParameters(
trade,
swapAndAddOptions,
position,
addLiquidityOptions,
ApprovalTypes.MAX,
ApprovalTypes.NOT_REQUIRED
);Routing-related constants used throughout the SDK.
/** Zero address constant */
const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";
/** Message sender placeholder address */
const MSG_SENDER = "0x0000000000000000000000000000000000000001";
/** Contract address placeholder */
const ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
/** Mixed quoter fee path placeholders for different protocol combinations */
const MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = 8388608;
const MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = 32;
const MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = 3145728;
const MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER = 4194304;
/** Percentage constants */
const ZERO_PERCENT: Percent;
const ONE_HUNDRED_PERCENT: Percent;Install with Tessl CLI
npx tessl i tessl/npm-uniswap--router-sdk