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
Overview
Eval results
Files

route-handling.mddocs/

Route Handling

Route wrapper classes providing unified interfaces for V2, V3, V4, and mixed protocol routes. These wrappers enable seamless integration across different Uniswap versions while maintaining protocol-specific functionality.

Capabilities

Common Route Interface

Unified interface implemented by all route types, providing consistent access to route properties across protocols.

/**
 * Generic route interface implemented by all protocol-specific routes
 */
interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pair | V3Pool | V4Pool> {
  /** Protocol identifier (V2, V3, V4, or MIXED) */
  protocol: Protocol;
  
  /** Array of pools or pairs that make up this route */
  pools: TPool[];
  
  /** Array of currencies in the swap path */
  path: Currency[];
  
  /** Mid price of the route calculated from pool reserves */
  midPrice: Price<TInput, TOutput>;
  
  /** Input currency for the route */
  input: TInput;
  
  /** Output currency for the route */
  output: TOutput;
  
  /** Input currency as it appears in the path (may be wrapped) */  
  pathInput: Currency;
  
  /** Output currency as it appears in the path (may be wrapped) */
  pathOutput: Currency;
}

Protocol Enumeration

Enumeration defining the supported Uniswap protocols.

/**
 * Supported Uniswap protocol versions
 */
enum Protocol {
  /** Uniswap V2 protocol */
  V2 = 'V2',
  
  /** Uniswap V3 protocol */
  V3 = 'V3',
  
  /** Uniswap V4 protocol */  
  V4 = 'V4',
  
  /** Mixed protocol routing across multiple versions */
  MIXED = 'MIXED'
}

V2 Route Wrapper

Wrapper class for Uniswap V2 routes extending the V2 SDK functionality.

/**
 * V2 route wrapper extending V2RouteSDK with unified interface
 */
class RouteV2<TInput extends Currency, TOutput extends Currency> 
  extends V2RouteSDK<TInput, TOutput>
  implements IRoute<TInput, TOutput, Pair> {
  
  /** Always Protocol.V2 for V2 routes */
  readonly protocol: Protocol.V2;
  
  /** Array of V2 pairs in the route */
  readonly pools: Pair[];
  
  /** Input currency as it appears in the first pair */
  pathInput: Currency;
  
  /** Output currency as it appears in the last pair */
  pathOutput: Currency;
  
  /**
   * Create a V2 route wrapper
   * @param v2Route - Original V2 route from @uniswap/v2-sdk
   */
  constructor(v2Route: V2RouteSDK<TInput, TOutput>);
}

V3 Route Wrapper

Wrapper class for Uniswap V3 routes extending the V3 SDK functionality.

/**
 * V3 route wrapper extending V3RouteSDK with unified interface
 */
class RouteV3<TInput extends Currency, TOutput extends Currency>
  extends V3RouteSDK<TInput, TOutput>
  implements IRoute<TInput, TOutput, V3Pool> {
  
  /** Always Protocol.V3 for V3 routes */
  readonly protocol: Protocol.V3;
  
  /** Array of tokens in the route path */
  readonly path: Token[];
  
  /** Input currency as it appears in the first pool */
  pathInput: Currency;
  
  /** Output currency as it appears in the last pool */
  pathOutput: Currency;
  
  /**
   * Create a V3 route wrapper
   * @param v3Route - Original V3 route from @uniswap/v3-sdk
   */
  constructor(v3Route: V3RouteSDK<TInput, TOutput>);
}

V4 Route Wrapper

Wrapper class for Uniswap V4 routes extending the V4 SDK functionality.

/**
 * V4 route wrapper extending V4RouteSDK with unified interface
 */
class RouteV4<TInput extends Currency, TOutput extends Currency>
  extends V4RouteSDK<TInput, TOutput>
  implements IRoute<TInput, TOutput, V4Pool> {
  
  /** Always Protocol.V4 for V4 routes */
  readonly protocol: Protocol.V4;
  
  /** Array of currencies in the route path */
  readonly path: Currency[];
  
  /**
   * Create a V4 route wrapper
   * @param v4Route - Original V4 route from @uniswap/v4-sdk
   */
  constructor(v4Route: V4RouteSDK<TInput, TOutput>);
}

Mixed Route Wrapper

Wrapper class for mixed protocol routes that span multiple Uniswap versions.

/**
 * Mixed route wrapper extending MixedRouteSDK with unified interface
 */
class MixedRoute<TInput extends Currency, TOutput extends Currency>
  extends MixedRouteSDK<TInput, TOutput>
  implements IRoute<TInput, TOutput, Pair | V3Pool | V4Pool> {
  
  /** Always Protocol.MIXED for mixed routes */
  readonly protocol: Protocol.MIXED;
  
  /**
   * Create a mixed route wrapper
   * @param mixedRoute - Original mixed route from the mixed route SDK
   */
  constructor(mixedRoute: MixedRouteSDK<TInput, TOutput>);
}

Path Token Helper

Utility function for determining the correct token to use in routing paths.

/**
 * Helper function to get the path token for a currency in a specific pool
 * Handles native ETH vs wrapped ETH conversions for routing
 * @param currency - The currency to find in the pool
 * @param pool - The pool to search in
 * @returns The token as it appears in the pool
 * @throws Error if currency is not found in the pool
 */
function getPathToken(currency: Currency, pool: Pair | V3Pool): Token;

Usage Examples:

import { RouteV2, RouteV3, Protocol, getPathToken } from "@uniswap/router-sdk";
import { Route as V2RouteSDK, Pair } from "@uniswap/v2-sdk";
import { Route as V3RouteSDK, Pool as V3Pool } from "@uniswap/v3-sdk";

// Create V2 route wrapper
const v2RouteSDK = new V2RouteSDK([pair1, pair2], tokenA, tokenB);
const v2Route = new RouteV2(v2RouteSDK);

console.log(v2Route.protocol); // "V2"
console.log(v2Route.pools);    // [pair1, pair2]
console.log(v2Route.midPrice.toSignificant(6));

// Create V3 route wrapper
const v3RouteSDK = new V3RouteSDK([pool1, pool2], tokenA, tokenB);
const v3Route = new RouteV3(v3RouteSDK);

console.log(v3Route.protocol); // "V3"  
console.log(v3Route.path);     // [tokenA, tokenIntermediate, tokenB]
console.log(v3Route.pathInput); // pathInput currency
// Working with different route types uniformly
function analyzeRoute(route: IRoute<Currency, Currency, any>) {
  console.log(`Protocol: ${route.protocol}`);
  console.log(`Input: ${route.input.symbol}`);
  console.log(`Output: ${route.output.symbol}`);
  console.log(`Path length: ${route.path.length}`);
  console.log(`Mid price: ${route.midPrice.toSignificant(6)}`);
  
  // Protocol-specific handling
  switch (route.protocol) {
    case Protocol.V2:
      console.log(`V2 pairs: ${route.pools.length}`);
      break;
    case Protocol.V3:
      console.log(`V3 pools: ${route.pools.length}`);
      break;
    case Protocol.MIXED:
      console.log(`Mixed pools: ${route.pools.length}`);
      break;
  }
}

// Use with any route type
analyzeRoute(v2Route);
analyzeRoute(v3Route);
analyzeRoute(mixedRoute);
// Helper function usage
const pathToken = getPathToken(ETH, pool);
console.log(`Path token: ${pathToken.symbol}`); // May be WETH if pool uses wrapped version

// Route comparison
function compareRoutes(routeA: IRoute<Currency, Currency, any>, routeB: IRoute<Currency, Currency, any>) {
  const priceA = routeA.midPrice;
  const priceB = routeB.midPrice;
  
  if (priceA.greaterThan(priceB)) {
    return `Route A (${routeA.protocol}) offers better price`;
  } else if (priceB.greaterThan(priceA)) {
    return `Route B (${routeB.protocol}) offers better price`;
  } else {
    return "Routes offer same price";
  }
}
// Create routes for trade construction
const routes: IRoute<Currency, Currency, any>[] = [];

// Add V2 routes
v2RoutesSDK.forEach(routeSDK => {
  routes.push(new RouteV2(routeSDK));
});

// Add V3 routes  
v3RoutesSDK.forEach(routeSDK => {
  routes.push(new RouteV3(routeSDK));
});

// Filter by protocol
const v3Routes = routes.filter(route => route.protocol === Protocol.V3);
const allProtocols = routes.map(route => route.protocol);

Install with Tessl CLI

npx tessl i tessl/npm-uniswap--router-sdk

docs

approval-liquidity.md

index.md

mixed-routes.md

route-handling.md

swap-routing.md

trade-management.md

utilities.md

tile.json