Swapping

The swap flow has three steps: define a Route, build a Trade, then encode calldata with SwapRouter.


Route

A Route describes the path through one or more pools from input token to output token. Multi-hop routes chain pools together.

typescript
import { Route } from '@hydrexfi/hydrex-sdk';

// Single-hop route: USDC → WETH
const route = new Route([pool], USDC, WETH);

// Multi-hop route: USDC → WETH → TOKEN
const multiHopRoute = new Route([poolA, poolB], USDC, TOKEN);

Trade

A Trade pairs a route with specific input/output amounts and a trade type. Use async factories to simulate amounts via tick math, or use createUncheckedTrade when amounts are already known (e.g. from an aggregator quote).

typescript
import {
  Trade,
  TradeType,
  CurrencyAmount,
  Percent,
} from '@hydrexfi/hydrex-sdk';

// Exact input (sync — use pre-computed output amount)
const trade = Trade.createUncheckedTrade({
  route,
  inputAmount: CurrencyAmount.fromRawAmount(USDC, '1000000'), // 1 USDC
  outputAmount: CurrencyAmount.fromRawAmount(WETH, estimatedOut),
  tradeType: TradeType.EXACT_INPUT,
});

// Exact input (async — simulates output through tick math)
const simulatedTrade = await Trade.exactIn(route, CurrencyAmount.fromRawAmount(USDC, '1000000'));

// Best route exact-in across multiple pools
const [bestTrade] = await Trade.bestTradeExactIn(
  pools,
  CurrencyAmount.fromRawAmount(USDC, '1000000'),
  WETH,
  { maxHops: 3, maxNumResults: 1 }
);

SwapRouter Calldata

SwapRouter.swapCallParameters encodes the final transaction calldata and value. Send the result to the router contract.

typescript
import { SwapRouter, SWAP_ROUTER_ADDRESSES, ChainId } from '@hydrexfi/hydrex-sdk';

const { calldata, value } = SwapRouter.swapCallParameters(trade, {
  slippageTolerance: new Percent(50, 10_000), // 0.5%
  recipient: '0xYourWallet',
  deadline: Math.floor(Date.now() / 1000) + 1200,
});

// Send the transaction to:
const routerAddress = SWAP_ROUTER_ADDRESSES[ChainId.Base];
// 0x6f4bE24d7dC93b6ffcBAb3Fd0747c5817Cea3F9e

Native ETH swaps

When swapping native ETH as input, include the value field from the returned parameters as your transaction's msg.value. The router handles wrapping/unwrapping automatically.

Trade Factory Reference

All available Trade factory methods at a glance.

// All Trade factories
Trade.exactIn(route, amountIn)                  // async, simulates output
Trade.exactOut(route, amountOut)                // async, simulates input
Trade.fromRoute(route, amount, tradeType)       // async single-route
Trade.fromRoutes(routes, tradeType)             // async multi-route
Trade.createUncheckedTrade(options)             // sync, skip simulation
Trade.createUncheckedTradeWithMultipleRoutes()  // sync multi-route
Trade.bestTradeExactIn(pools, amountIn, tokenOut)
Trade.bestTradeExactOut(pools, tokenIn, amountOut)
FactoryAsyncDescription
exactIn(route, amountIn)asyncSimulates output amount through tick math
exactOut(route, amountOut)asyncSimulates required input amount through tick math
fromRoute(route, amount, tradeType)asyncSingle-route factory with trade type
fromRoutes(routes, tradeType)asyncMulti-route factory
createUncheckedTrade(options)syncSkips simulation — use pre-computed amounts
createUncheckedTradeWithMultipleRoutes(options)syncSync multi-route
bestTradeExactIn(pools, amountIn, tokenOut)asyncFinds best route for exact-in
bestTradeExactOut(pools, tokenIn, amountOut)asyncFinds best route for exact-out
Hydrex

Hydrex is an Omni-Liquidity MetaDEX built on Base for Base, designed to bring new users onchain.

Paragraph

© 2026 Hydrex. All rights reserved.