Executing a Trade
Introduction
This guide will build off our quoting guide and show how to use a quote to construct and execute a trade on the Uniswap V3 protocol. It is based on the Trading 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 two ERC20 tokens: WETH and USDC. The tokens, amount of input token, and the fee level can be configured as inputs.
The guide will cover:
- Constructing a route from pool information
- Constructing an unchecked trade
- Executing a trade
At the end of the guide, we should be able to create and execute a trade between any two ERC20 tokens using the example's included UI.
note
Included in the example application is functionality to wrap/unwrap ETH as needed to fund the example WETH
to USDC
swap directly from an ETH
balance.
For this guide, the following Uniswap packages are used:
The core code of this guide can be found in trading.ts
Constructing a route from pool information
To construct our trade, we will first create an model instance of a Pool
. We will first extract the needed metadata from the relevant pool contract. Metadata includes both constant information about the pool as well as information about its current state stored in its first slot:
loading...
Using this metadata along with our inputs, we will then construct a Pool
:
loading...
With this Pool
, we can now construct a route to use in our trade. We will reuse our previous quoting code to calculate the output amount we expect from our trade:
loading...
Constructing an unchecked trade
Once we have constructed the route object, we now need to obtain a quote for the given inputAmount
of the example:
loading...
As shown below, the quote is obtained using the v3-sdk
's SwapQuoter
, in contrast to the previous quoting guide, where we directly accessed the smart contact:
loading...
The SwapQuoter
's quoteCallParameters
function, gives us the calldata needed to make the call to the Quoter
, and we then decode the returned quote:
loading...
With the quote and the route, we can now construct an unchecked trade using the route in addition to the output amount from a quote based on our input:
loading...
This example uses an exact input trade, but we can also construct a trade using exact output assuming we adapt our quoting code accordingly.
Executing a trade
Once we have created a trade, we can now execute this trade with our provider. First, we must give the SwapRouter
approval to spend our tokens for us:
loading...
Then, we set our options that define how much time and slippage can occur in our execution as well as the address to use for our wallet:
loading...
Next, we use the Uniswap SwapRouter
to get the associated call parameters for our trade and options:
loading...
Finally, we can construct a transaction from the method parameters and send the transaction:
loading...
Next Steps
The resulting example allows for trading between any two ERC20 tokens, but this can be suboptimal for the best pricing and fees. To achieve the best possible price, we use the Uniswap auto router to route through pools to get an optimal cost. Our routing guide will show you how to use this router and execute optimal swaps.