Skip to main content
Helpful?

Routing a Swap

Introduction

This guide will cover how to use Uniswap'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 Uniswap code examples repository. To run this example, check out the guide's README and follow the setup instructions.

info

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 us any two currencies and amount of input currency.

The guide will cover:

  1. Creating a router instance
  2. Creating a route
  3. 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 Uniswap packages are used:

The core code of this guide can be found in routing.ts

Creating a router instance

To compute our route, we will use the @uniswap/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:

Instantiating an AlphaRouter
loading...

Creating a route

Next, we will create our options conforming to the SwapOptionsSwapRouter02 interface, defining the wallet to use, slippage tolerance, and deadline for the transaction:

Routing Options
loading...

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.

Creating a route
loading...

Swapping using a route

First, we need to give approval to the SwapRouter smart contract to spend our tokens for us:

Approving SwapRouter to spend our tokens
loading...

Once the approval has been granted and using the route, we can now execute the trade using the route's computed calldata, values, and gas values:

Using a route
loading...

After swapping, you should see the currency balances update in the UI shortly after the block is confirmed.

Next Steps

Now that you're familiar with trading, consider checking out our next guides on pooling liquidity to Uniswap!

Helpful?