Skip to main content
In the last tutorial, we created a pool for PYUSD with WSOL as the quote token. Now we are going to add liquidity to that pool. To add liquidity, we must check the existence of the required bins and vault accounts for the pair, or create them if they don’t exist. Then we will create positions for the user in the required bins and add liquidity to those positions.

Create Pool Metadata

Update your constants.ts file to include the pool metadata so we can reuse it throughout this tutorial.
constants.ts
Have a Keypair set up, or set up your custom wallet interface using the wallet provider of your choice. For this tutorial, I’m using a wallet object created with Keypair.generate() from @solana/web3.js.

Get Pair On-chain Metadata

Now let’s write the main logic. Create a new file called adding-liquidity.ts. Here we will add the following logic. Use the sarosDLMM instance to get the pair’s on-chain metadata. This provides the current active bin index of the pool, which is needed to construct our bin array.

Maintain a Global Transaction Queue

We will sign and send many transactions to add liquidity to the pool. For that, we’ll use a global transaction queue that stores all transactions.

Prepare Bin and Vault Accounts

  1. Create a temporary Transaction object that contains instructions to load bin and vault accounts.
  1. Select the bin range where we want to add liquidity.
  1. Bins are structured in arrays, with each array containing 256 bins. So we have to find the bin arrays containing the bins for the selected bin range.
Bin structure
  1. For the required bin arrays, check whether they exist on-chain. If not, create them. The getBinArray method checks if the bin array PDA exists on-chain. If not, it builds transaction instructions to create the bin array and updates binsAndVaultsTx.
adding-liquidity.ts
  1. Next, check the pair vault account (ATA) and user vault account (ATA) for the pair. These accounts don’t exist for a newly created pair or for a user interacting with this pair for the first time.
The getPairVaultInfo and getUserVaultInfo methods check whether the vault accounts exist on-chain. If not, they build transaction instructions to create the vault accounts and update binsAndVaultsTx.
adding-liquidity.ts
  1. Now it’s time to add the built instructions to the global transaction queue.
adding-liquidity.ts

Create User Positions

Now we need to create positions for the user in the required bins, and then add liquidity to those positions.

1. Calculate the required positions

  1. Previously, we represented 256 bins in a binArray. To add positions, we further divide the binArray into bin groups of 16 bins. A single position covers 16 bins (Saros DLMM standard).
Let’s use the getMaxPosition method to calculate the relative positions (with respect to the active bin) required to add liquidity.
adding-liquidity.ts
  1. Next, find all existing user positions in the pair.
adding-liquidity.ts
  1. Choose a distribution strategy to decide how the user should distribute liquidity across the required bins.
    We have 3 strategies to distribute liquidity: “BidAsk”, “Curve”, and “Spot”.
adding-liquidity.ts
The maxLiquidityDistribution is an array of objects with relativeBinId, distributionX, and distributionY token percentages.

2. Create positions

For each required relative position, find the mint address, the liquidity distribution of depositing tokens, and the lower and upper bin array index. With the position mint address and bin array indexes, the program will find the bins to add liquidity to. The liquidity distribution determines the percentage of tokens to deposit in that position.
  1. Find the user’s existing position in this bin group.
adding-liquidity.ts
  1. Calculate the liquidity distribution for this position.
adding-liquidity.ts
  1. Calculate the bin arrays in which the position is to be created.
adding-liquidity.ts
  1. Find the mint address of the position.
If there is no existing position, create a new position.
adding-liquidity.ts
Finally, return the calculated values.
adding-liquidity.ts

Add Liquidity into Positions

In the previous step, we created positions that will hold user liquidity. Now we will add liquidity to those positions.
adding-liquidity.ts

Sign and Send Transactions

We have built the instructions to add liquidity to the positions and added them to the global transaction queue. You can use your favorite wallet provider to sign and send the transactions.