Introduction
This guide will cover how to execute a swap-and-add operation in a single atomic transaction. It is based on the swap-and-add example, found in the Uniswap code examples repository. To run this example, check out the examples’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!
- Setup a router instance
- Configuring our ratio calculation
- Calculating our currency ratio
- Constructing and executing our swap-and-add transaction
swapAndAddLiquidity().
This guide assumes you are familiar with our Minting a Position guide. A minted position is required to add or remove liquidity from, so the buttons will be disabled until a position is minted.Also note that we do not need to give approval to the
RamsesV3PositionManager to transfer our tokens as we will have already done that when minting our position.Setup a router instance
The first step is to approve theSwapRouter smart contract to spend our tokens for us in order for us to add liquidity to our position:
getTokenTransferApproval function here.
Then we can setup our router, the AlphaRouter, which is part of the smart-order-router package. The router requires a chainId and a provider to be initialized. Note that routing is not supported for local forks, so we will use a mainnet provider even when swapping on a local fork:
Configuring our ratio calculation
Having created the router, we now need to construct the parameters required to make a call to itsrouteToRatio function, which will ensure the ratio of currency used matches the pool’s required ratio to add our total liquidity. This will require the following parameters:
The first two parameters are the currency amounts we use as input to the routeToRatio algorithm:
1 since liquidity is still unknown and will be set inside the call to routeToRatio:
SwapAndAddConfig which will set additional configuration parameters for the routeToRatio algorithm:
ratioErrorTolerancedetermines the margin of error the resulting ratio can have from the optimal ratio.maxIterationsdetermines the maximum times the algorithm will iterate to find a ratio within error tolerance. If max iterations is exceeded, an error is returned. The benefit of running the algorithm more times is that we have more chances to find a route, but more iterations will longer to execute. We’ve used a default of 6 in our example.
SwapAndAddOptions to configure which position we are adding liquidity to and our defined swapping parameters in two different objects:
swapConfigconfigures therecipientof leftover dust from swap,slippageToleranceand adeadlinefor the swap.addLiquidityOptionsmust contain atokenIdto add to an existing position
Calculating our currency ratio
Having constructed all the parameters we need to callrouteToRatio, we can now make the call to the function:
SwapToRatioRoute object. We check to make sure that both of those conditions hold true before we construct and submit the transaction:
Failed state for the transaction.
Constructing and executing our swap-and-add transaction
After making sure that a route was successfully found, we can now construct and send the transaction. The response (SwapToRatioRoute) will have the properties we need to construct our transaction object:
