Opening a Position
Positions in Whirlpools are tracked with a minted NFT in the user's wallet.
The usual action of opening a position consists of two instruction calls
initialize_tick_array
to initialize the tick arrays that would host your desired ticks for your position if they do not exist yet.open_position
oropen_position_with_metadata
to mint the position and define the tick rangeincrease_liquidity
to transfer tokens from your wallet into a position.
Opening Position with Metadata
In either way of constructing a position, users have the option of appending Metaplex metadata onto the position NFT. Doing so will allow the token to be more identifiable in tracking websites or wallets as a Whirlpool NFT. The drawback is it will require more compute-budget.
To append, use openPositionWithMetadata
. Otherwise, use openPosition
.
Initialize Tick Array accounts if needed
For liquidity to exist in the Whirlpool, the tick-array that contains that particular tick must be initialized. Calculate the start_index of the required tick array and use the initialize_tick_array
instruction to initialize it.
More often than not, tick-arrays are already created. But if you want your code to be defensive, you should do a check prior to invoking open_position
. To understand more on how Tick-Arrays work in Whirlpools, read here.
// TODO: add tick-array code sample
Open Position with WhirlpoolClient
WhirlpoolClient's openPosition
method bundles the open and increase liquidity instructions into a single transaction for you. Below is a code sample to create a position for the SOL/USDC pool at the price between $98 - $150, with the intention to deposit 50 SOL into the position.
The Manual way
Follow the instructions below if you would like to have more control over your instruction building process. Note that open_position
does not add liquidity to a position. Follow the next article "Modify Liquidity" to add liquidity.
Determine position parameters
To open a position against a Whirlpool, you must first define certain parameters of your position to invoke the open_position
instruction.
WhirlpoolKey - The public key for the Whirlpool that the position will host liquidity in.
tickLowerIndex, tickUpperIndex - The tick index bounds for the position. Must be an initializable index.
positionMintAddress - A generated empty Keypair that will be initialized to a token mint.
positionPda - Derived address of the position account via getPositionPda
positionTokenAccountAddress - This is the account that will hold the minted position token. It is the associated token address of the position-mint.
Sample Code
Once your position is open, proceed to the next section to add liquidity.
Common Errors
InvalidTickIndex
(0x177a)tickLowerIndex
is higher than uppertickUpperIndex
Some tick indices is not an initializable index (not a multiple of tickSpacing). Use
TickUtil.getInitializableTickIndex
to get the closest initializable tick to your index.Some tick indices is out of bounds
NotRentExempt (0x0)
Usually, the TickArray that houses your
tickLowerIndex
ortickUpperIndex
has not been initialized. Use theWhirlpoolClient.initTickArrayForTicks
orWhirlpoolIx.initTickArrayIx
to initialize the array at the derived startTickIndex.Alternatively, if this failure is from
init_tick_array
, the tick array has already been initialized.
Last updated