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.
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).
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.
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];
// 0x6f4bE24d7dC93b6ffcBAb3Fd0747c5817Cea3F9eNative ETH swaps
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)| Factory | Async | Description |
|---|---|---|
exactIn(route, amountIn) | async | Simulates output amount through tick math |
exactOut(route, amountOut) | async | Simulates required input amount through tick math |
fromRoute(route, amount, tradeType) | async | Single-route factory with trade type |
fromRoutes(routes, tradeType) | async | Multi-route factory |
createUncheckedTrade(options) | sync | Skips simulation — use pre-computed amounts |
createUncheckedTradeWithMultipleRoutes(options) | sync | Sync multi-route |
bestTradeExactIn(pools, amountIn, tokenOut) | async | Finds best route for exact-in |
bestTradeExactOut(pools, tokenIn, amountOut) | async | Finds best route for exact-out |