Increase Liquidity
Introduction
Once you have created a new Elastic position, you can add additional liquidity to the existing position. Position liquidity addition is handled by the AntiSnipAttackPositionManager contract which extends the base position manager contract by adding an anti-sniping feature for liquidity additions and removals.
The logic for creating a new position can be found in the increaseLiquidity.ts
file linked below:
Signer configuration
In order to sign the transaction to be processed by the network, this example requires an Ethers Signer to be configured. Please view Provider and Signer Setup for more information.
Flow
Increasing Position Liquidity
Open Positions
This guide requires an existing position to have been created. Each position is assigned a position ID which enables further management of the position after creation.
Please refer to Create A New Position for the developer guide on how to create a new Elastic position.
Step 1: Get the position data
In order to add liquidity to an existing position, we must first get the position data which includes its positionId
(the positions unique identifier).
Due to gas considerations, the ownerAddress
-> positionId
mapping is not stored on-chain. Nonetheless, to aid with Elastic integrations, KyberSwap exposes a subgraph for Elastic protocols across all supported chains. The addresses for each subgraph can be found on Elastic Subgraphs.
For the purposes of this guide, we will be using the Matic subgraph which is defined under the getSignerPositions()
function:
The subgraph URL above allows us to query the positions belonging to the signerAddress
. We are also leveraging the Axios package to easily handle promise-based HTTP requests:
You can play around with the query by opening the explorer client in your browser using the links provided under Elastic Subgraphs.
We then filter the positions to return only the positions with liquidity, signerOpenPositions
. As the positions are ordered based on their positionId
we will choose to add liquidity to the oldest position, targetOpenPosition
.
Step 2: Create a position instance with additional liquidity amount
Similar to the position creation guide, we will add the value of 1 unit of token0
as the input token amount:
With the increment amount calculated, we can then create the new target position:
Step 3: Calculate token amounts required for the mint
Based on the targetPositionNew
, we will need to calculate the token0
and token1
amounts that are required for minting the position.
As part of position creation, token0
and token1
amounts will be sent from the signer's address to the position manager contract in exchange for a NFT representing the position. Hence, the signer must have the required token amounts returned in the tokenMintAmounts
.
Step 4: Allow contract to manage signer tokens
In addition to the signer balances, the position manager contract must also have the necessary allowances to spend token0
and token1
from the signer's address.
We can query the existing allowances via the token contracts:
If there are insufficient token allowances, we will then send an approve transaction from the signer's address:
Once we have the necessary allowances, we can then proceed to prepare the add liquidity transaction.
Step 5: Get the add liquidity call parameters
Note that unlike the position creation, we specify the positionId
when configuring the increaseLiquidityOptions
for the transaction. By including a positionId
, the Elastic SDK knows that we are trying to add liquidity to an existing position:
As part of the call parameters, we will also need to pass in the nearest initialized ticks that corresponds to the position's lower and upper range. To get the updated tick data, we will be making use of the TicksFeesReader contract:
We can then get the call parameters by utilizing the NonfungiblePositionManager
helper class:
This will return the encoded calldata that will be sent to the network.
Step 6: Execute the add liquidity transaction
We are finally ready to execute the transaction by sending the transaction from the signer's address:
A transaction hash will be returned once the trade has been executed. You can copy this hash into a scanner (i.e. PolygonScan) and see that your transaction has been successfully completed by the network.
Last updated