Executing a Trade
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.
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.
Included in the example application is functionality to wrap/unwrap ETH as needed to fund the example
USDC swap directly from an
For this guide, the following Uniswap packages are used:
The core code of this guide can be found in
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:
Using this metadata along with our inputs, we will then construct a
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:
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:
As shown below, the quote is obtained using the
SwapQuoter, in contrast to the previous quoting guide, where we directly accessed the smart contact:
quoteCallParameters function, gives us the calldata needed to make the call to the
Quoter, and we then decode the returned quote:
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:
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:
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:
Next, we use the Uniswap
SwapRouter to get the associated call parameters for our trade and options:
Finally, we can construct a transaction from the method parameters and send the transaction:
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.