routing
Introduction
This guide will cover how to use SwapX's smart order router to compute optimal routes and execute swaps. Rather than trading between a single pool, smart routing may use multiple hops (as many as needed) to ensure that the end result of the swap is the optimal price. It is based on the routing code example, found in the SwapX code examples repository. To run this example, check out the guide's README and follow the setup instructions.
If you need a briefer on the SDK and to learn more about how these guides connect to the examples repository, please visit our background page!
In this example we will trade between WETH and USDC, but you can configure your example to use any two currencies and amount of input currency.
The guide will cover:
- Creating a router instance
- Creating a route
- Swapping using a route
At the end of the guide, we should be able to create a route and and execute a swap between any two currencies tokens using the example's included UI.
For this guide, the following SwapX packages are used:
The core code of this guide can be found in routing.ts
The config, which we will use in some code snippets in this guides has this structure:
import { Token } from '@swapx/sdk-core'
interface ExampleConfig {
env: Environment
rpc: {
local: string
mainnet: string
}
wallet: {
address: string
privateKey: string
}
tokens: {
in: Token
amountIn: number
out: Token
}
}
export const CurrentConfig: ExampleConfig = {...}
Creating a router instance
To compute our route, we will use the @swapx/smart-order-router
package, specifically the AlphaRouter
class which requires a chainId
and a provider
. Note that routing is not supported for local forks, so we will use a mainnet provider even when swapping on a local fork:
import { AlphaRouter, ChainId } from "@swapx/smart-order-router";
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const router = new AlphaRouter({
chainId: ChainId.MAINNET,
provider,
});
Creating a route
We will use the SwapRouter02 for our trade.
The smart-order-router
package provides us with a SwapOptionsSwapRouter02
interface, defining the wallet to use, slippage tolerance, and deadline for the transaction that we need to interact with the contract:
import { SwapOptionsSwapRouter02, SwapType } from "@swapx/smart-order-router";
import { Percent } from "@swapx/sdk-core";
const options: SwapOptionsSwapRouter02 = {
recipient: CurrentConfig.wallet.address,
slippageTolerance: new Percent(50, 10_000),
deadline: Math.floor(Date.now() / 1000 + 1800),
type: SwapType.SWAP_ROUTER_02,
};
Like explained in the previous guide, it is important to set the parameters to sensible values.
Using these options, we can now create a trade (TradeType.EXACT_INPUT
or TradeType.EXACT_OUTPUT
) with the currency and the input amount to use to get a quote. For this example, we'll use an EXACT_INPUT
trade to get a quote outputted in the quote currency.
import { CurrencyAmount, TradeType } from "@swapx/sdk-core";
const rawTokenAmountIn: JSBI = fromReadableAmount(
CurrentConfig.currencies.amountIn,
CurrentConfig.currencies.in.decimals
);
const route = await router.route(
CurrencyAmount.fromRawAmount(CurrentConfig.currencies.in, rawTokenAmountIn),
CurrentConfig.currencies.out,
TradeType.EXACT_INPUT,
options
);