CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-uniswap--router-sdk

SDK for routing swaps across Uniswap V2, V3, and V4 protocols with automatic route optimization and gas-efficient execution.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Uniswap Router SDK

The Uniswap Router SDK is a TypeScript library for routing swaps across Uniswap V2, V3, and V4 protocols. It provides unified access to liquidity across different AMM implementations with automatic route optimization, gas-efficient swap execution, and cross-protocol trade aggregation.

Package Information

  • Package Name: @uniswap/router-sdk
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @uniswap/router-sdk

Core Imports

import { SwapRouter, Trade, Protocol } from "@uniswap/router-sdk";

For CommonJS:

const { SwapRouter, Trade, Protocol } = require("@uniswap/router-sdk");

Basic Usage

import { 
  SwapRouter, 
  Trade, 
  Protocol, 
  SwapOptions 
} from "@uniswap/router-sdk";
import { CurrencyAmount, TradeType, Percent } from "@uniswap/sdk-core";

// Create a trade across multiple protocols
const trade = new Trade({
  v2Routes: [/* V2 routes */],
  v3Routes: [/* V3 routes */], 
  v4Routes: [/* V4 routes */],
  mixedRoutes: [/* Mixed routes */],
  tradeType: TradeType.EXACT_INPUT
});

// Configure swap options
const options: SwapOptions = {
  slippageTolerance: new Percent(50, 10000), // 0.5%
  recipient: "0x...", // recipient address
  deadlineOrPreviousBlockhash: Math.floor(Date.now() / 1000) + 1800 // 30 min
};

// Generate swap call parameters
const { calldata, value } = SwapRouter.swapCallParameters(trade, options);

Architecture

The Uniswap Router SDK is built around several key components:

  • SwapRouter: Core class for encoding swap transactions across all Uniswap protocols
  • Trade System: Unified trade representation aggregating routes from multiple protocols
  • Route Wrappers: Protocol-specific route classes (V2, V3, V4, Mixed) with common interfaces
  • Protocol Support: Native support for Uniswap V2, V3, and V4 with mixed routing capabilities
  • Utility System: Path encoding, multicall, and payment utilities for transaction construction
  • Type Safety: Full TypeScript integration with generic type preservation

Capabilities

Swap Routing

Core swap routing functionality for encoding transactions across Uniswap V2, V3, and V4 protocols. Handles single and multi-protocol routes with automatic optimization.

abstract class SwapRouter {
  static INTERFACE: Interface;
  static swapCallParameters(
    trades: Trade | V2Trade | V3Trade | MixedRouteTrade | (V2Trade | V3Trade | MixedRouteTrade)[],
    options: SwapOptions
  ): MethodParameters;
  static swapAndAddCallParameters(
    trades: AnyTradeType,
    options: SwapAndAddOptions,
    position: Position,
    addLiquidityOptions: CondensedAddLiquidityOptions,
    tokenInApprovalType: ApprovalTypes,
    tokenOutApprovalType: ApprovalTypes
  ): MethodParameters;
}

interface SwapOptions {
  slippageTolerance: Percent;
  recipient?: string;
  deadlineOrPreviousBlockhash?: Validation;
  inputTokenPermit?: PermitOptions;
  fee?: FeeOptions;
}

Swap Routing

Trade Management

Multi-protocol trade aggregation system supporting V2, V3, V4, and mixed routes. Provides price impact calculation, slippage protection, and execution optimization.

class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
  readonly routes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
  readonly tradeType: TTradeType;
  readonly swaps: {
    route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>;
    inputAmount: CurrencyAmount<TInput>;
    outputAmount: CurrencyAmount<TOutput>;
  }[];
  
  get inputAmount(): CurrencyAmount<TInput>;
  get outputAmount(): CurrencyAmount<TOutput>;
  get executionPrice(): Price<TInput, TOutput>;
  get priceImpact(): Percent;
  
  minimumAmountOut(slippageTolerance: Percent): CurrencyAmount<TOutput>;
  maximumAmountIn(slippageTolerance: Percent): CurrencyAmount<TInput>;
}

Trade Management

Route Handling

Route wrapper classes providing unified interfaces for V2, V3, V4, and mixed protocol routes. Enables seamless integration across different Uniswap versions.

interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pair | V3Pool | V4Pool> {
  protocol: Protocol;
  pools: TPool[];
  path: Currency[];
  midPrice: Price<TInput, TOutput>;
  input: TInput;
  output: TOutput;
  pathInput: Currency;
  pathOutput: Currency;
}

enum Protocol {
  V2 = 'V2',
  V3 = 'V3', 
  V4 = 'V4',
  MIXED = 'MIXED'
}

Route Handling

Approval & Liquidity

Token approval and liquidity management utilities for position management and swap-and-add operations. Supports various approval strategies and NFT position handling.

abstract class ApproveAndCall {
  static INTERFACE: Interface;
  static encodeApproveMax(token: Token): string;
  static encodeApproveZeroThenMax(token: Token): string;
  static encodeAddLiquidity(
    position: Position,
    minimalPosition: Position, 
    addLiquidityOptions: CondensedAddLiquidityOptions,
    slippageTolerance: Percent
  ): string;
}

enum ApprovalTypes {
  NOT_REQUIRED = 0,
  MAX = 1,
  MAX_MINUS_ONE = 2,
  ZERO_THEN_MAX = 3,
  ZERO_THEN_MAX_MINUS_ONE = 4
}

Approval & Liquidity

Mixed Route System

Advanced routing system for trades that span multiple Uniswap protocols. Optimizes paths across V2, V3, and V4 pools for maximum efficiency.

class MixedRouteSDK<TInput extends Currency, TOutput extends Currency> {
  readonly pools: TPool[];
  readonly path: Currency[];
  readonly input: TInput;
  readonly output: TOutput;
  get midPrice(): Price<TInput, TOutput>;
}

class MixedRouteTrade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
  readonly swaps: {
    route: MixedRouteSDK<TInput, TOutput>;
    inputAmount: CurrencyAmount<TInput>;
    outputAmount: CurrencyAmount<TOutput>;
  }[];
  
  static async bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(
    pools: TPool[],
    currencyAmountIn: CurrencyAmount<TInput>,
    currencyOut: TOutput,
    options?: BestTradeOptions
  ): Promise<MixedRouteTrade<TInput, TOutput, TradeType.EXACT_INPUT>[]>;
}

Mixed Route System

Utilities & Extensions

Extended multicall and payment utilities for transaction batching and native ETH handling. Provides enhanced functionality beyond standard Uniswap SDK utilities.

abstract class MulticallExtended {
  static INTERFACE: Interface;
  static encodeMulticall(calldatas: string | string[], validation?: Validation): string;
}

abstract class PaymentsExtended {
  static INTERFACE: Interface;
  static encodeUnwrapWETH9(amountMinimum: JSBI, recipient?: string, feeOptions?: FeeOptions): string;
  static encodeSweepToken(token: Token, amountMinimum: JSBI, recipient?: string, feeOptions?: FeeOptions): string;
  static encodePull(token: Token, amount: JSBI): string;
  static encodeWrapETH(amount: JSBI): string;
}

Utilities & Extensions

Types

Core type definitions used throughout the SDK:

type TPool = Pair | V3Pool | V4Pool;

type Validation = BigintIsh | string;

interface MethodParameters {
  calldata: string;
  value: string;
}

type CondensedAddLiquidityOptions = Omit<MintSpecificOptions, 'createPool'> | IncreaseSpecificOptions;

interface BestTradeOptions {
  maxNumResults?: number;
  maxHops?: number;
}

type AnyTradeType = 
  | 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>)[];
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@uniswap/router-sdk@2.0.x
Publish Source
CLI
Badge
tessl/npm-uniswap--router-sdk badge