Documentation Index
Fetch the complete documentation index at: https://tech.ramses.xyz/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
This guide will cover how to modify a liquidity position by adding or removing liquidity on the Ramses V3 protocol. It is based on the modifying a position code 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!
NonfungiblePositionManager class to help us mint a liquidity position and then modify the provided liquidity for the USDC - DAI pair. The inputs to our guide are the two tokens that we are pooling for, the amount of each token we are pooling for, the Pool fee and the fraction by which to add and remove from our position.
The guide will cover:
- Adding liquidity to our position
- Removing liquidity from our position
addLiquidity() and removeLiquidity()
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.Configuration and utils
The example can be configured in theconfig.ts file.
The CurrentConfig object has this structure:
rpc, wallet and token parameters, they are used in the same way as in the guides earlier in our v3-sdk series.
The fractionToAdd variable is the multiplicator by which we will increase the Position. A fraction of 0.5 means we increase the liquidity by 50%.
The fractionToRemove variable is the fraction of the Position that we want to remove later in the guide. A fraction of 1 means we remove 100% of the liquidity.
Adding liquidity to our position
Assuming we have already minted a position, our first step is to construct the modified position using our original position to calculate the amount by which we want to increase our current position:fromReadableAmount() function calculates the amount of tokens in their smallest unit, so for example 1 ETH would be 1000000000000000000 Wei as ETH has 18 decimals.
A better way to get the amounts might be to fetch them with the positionId directly from the blockchain.
We demonstrated how to do that in the first guide of this series.
tokenAmount by the parameterized fractionToAdd since the new liquidity position will be added on top of the already minted liquidity position.
We then need to construct an options object of type AddLiquidityOptions similar to how we did in the minting case. In this case, we will use IncreaseOptions:
recipient parameter and instead passed in the tokenId of the position we previously minted.
As the Position already exists, the recipient doesn’t change, instead the RamsesV3PositionManager contract can modify the existing Position by accessing it with its id.
The tokenId can be fetched with the tokenOfOwnerByIndex function of the RamsesV3PositionManager Contract as described here.
The newly created position along with the options object are then passed to the RamsesV3PositionManager’s addCallParameters:
addCallParameters are the calldata and value of the transaction we need to submit to increase our position’s liquidity. We can now build and execute the transaction:
Removing liquidity from our position
TheremoveLiquidity function is the mirror action of adding liquidity and will be somewhat similar as a result, requiring a position to already be minted.
To start, we create a position identical to the one we minted:
RemoveLiquidityOptions:
recipient parameter and instead passed in the tokenId of the position we previously minted.
We have also provide two additional parameters:
liquidityPercentagedetermines how much liquidity is removed from our initial position (as aPercentage), and transfers the removed liquidity back to our address. We set this percentage from our guide configuration ranging from 0 (0%) to 1 (100%). In this example we would remove 50% of the liquidity.collectOptionsgives us the option to collect the fees, if any, that we have accrued for this position. In this example, we won’t collect any fees, so we provide zero values. If you’d like to see how to collect fees without modifying your position, check out our collecting fees guide!
RamsesV3PositionManager’s removeCallParameters, similar to how we did in the adding liquidity case:
removeCallParameters are the calldata and value that are needed to construct the transaction to remove liquidity from our position. We can build the transaction and send it for execution:
