Saros
  • SAROS
  • SAROS DLMM
  • SAROS AMM
  • SAROS PERPS
  • SAROS MOBILE APP
  • SAROS DLMM
    • Onboarding and Guides
      • Saros DLMM: Introduction
      • Liquidity Lifecycle: Deployment, Management & Rebalancing
      • Permissionless Saros DLMM Pools
      • Understanding Your Risks as a Liquidity Provider on Saros DLMM
    • Shapes and Strategies
      • Liquidity Shapes
      • Getting Started with Basic Liquidity Strategies
      • Advanced Liquidity Strategies on Saros
      • Managing Out-of-Range Liquidity on Saros
      • Single-Sided Liquidity Strategies on Saros
    • Rewards
      • Concentrated Incentives (CI) on Saros
    • Technical Design
    • Technical Guides
      • Add liquidity
      • Create a pair
      • User Position
      • Swap tokens
      • Removing Liquidity
  • SAROS MOBILE APP
    • Mobile App
      • How to install Saros Super App
      • How to set up Saros Super App
      • How to add a new Hot Wallet
      • How to add View-only wallet
      • How to log in to Social Account
      • How to manage wallets
      • How to send or receive a token
      • How to send or receive NFTs
      • How to send multiple NFTs to an address
      • How to swap tokens
      • How to add a custom token
      • How to manage tokens
      • General Settings
      • How to use DApp browser
      • How to turn on Biometric and change Passcode
      • Migrate Ramper V2 on Super App
      • How to use Discover
      • How to buy crypto
    • Extension
      • How to install Saros Wallet Extension
      • How to set up Saros Extension Wallet
      • How to add a new Hot Wallet on Saros Extension
      • How to Add view-only wallet
      • How to manage wallets
      • How to send or receive a token
      • How to send or receive NFTs
      • How to add a custom token?
      • How to manage tokens
      • General settings
      • How to change the password?
      • Migrate Ramper V2 on Extension
      • How to log in to Social Account
      • How to use Discover
      • How to buy crypto
      • How to send multiple NFTs in one transaction with Multisend
      • How to Manage Notifications
      • How to swap on Saros Extension
      • Action on X
    • FAQs
  • SAROS AMM
    • SarosSwap
      • How To Swap on SarosSwap
    • SarosFarm
      • How to Add Liquidity
      • How to Remove Liquidity
      • How to Farm on SarosFarm
      • How to Claim the Reward
    • SarosStake
      • How to stake
      • How to unstake
      • How to harvest
    • SarosSnapShot
    • SarosPerpetual
      • Trading Basics
      • Getting Started
      • Deposit/Withdraw
      • Order Management
    • FAQs
      • Is Saros safe? Has Saros been audited?
      • Why did my transaction fail?
      • How to resolve the "No wallet" error when connecting wallet to Saros Finance
      • What is the difference between APY and APR?
      • How to get farming pool Txid
      • What are liquidity provider tokens?
      • When will you open more pools?
      • What wallets can I use with Saros? How do I connect my wallet to Saros?
      • My LP tokens aren’t showing up on the site or in my wallet?
      • How do I get airdrops?
      • Can I use Saros on my phone?
      • What is price impact?
      • What is slippage tolerance? How can I adjust it when swapping?
      • What fees do I pay when I exchange tokens?
      • Are there any fees associated with the SarosFarm?
      • How to calculate and distribute the reward on SarosFarm?
      • Can I withdraw my liquidity anytime?
      • Where can I check Saros Analytics?
      • I can't find an answer for my question. Where do I find an answer?
      • How could I report a bug?
      • How to check wallet information on Saros
    • Go to DEX
  • SAROS GARDEN
    • Introduction
    • How to stake on Saros Garden
    • How to unstake from Saros Garden
  • ALL ABOUT $SAROS
    • Saros Token ($SAROS)
  • INTEGRATION
    • Saros Super App
    • Saros DEX
  • Saros DLMM
  • LEGAL
    • TERMS OF SERVICE
    • Privacy Policy
  • AUDITS
    • SarosSwap
    • SarosFarm & SarosStake
  • OFFICIAL LINKS
    • Saros Community
    • Brand Assets
Powered by GitBook
On this page
  • Overview​
  • Decreasing a Position​
  • Closing a Position​
  • Calculating Shares to Remove​
  1. SAROS DLMM
  2. Technical Guides

Removing Liquidity

PreviousSwap tokensNextMobile App

Last updated 16 days ago

Overview

This guide details how to pull liquidity from a Saros DLMM pool via your frontend. You can either partially decrease an existing position or close it entirely in one transaction.

Key steps:

  1. Calculating the shares to remove

  2. Calling the appropriate instruction

Decreasing a Position

To remove some liquidity from an existing position:

  1. Calculate the shares to remove from each bin

  2. Call the decrease_position instruction with these parameters

Example Code

import { PublicKey, SystemProgram } from "@solana/web3.js";
import { BN } from "@project-serum/anchor";
import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";

// Define the shares to remove from each bin
// The number of shares must match the position width (upper_bin_id - lower_bin_id + 1)
const sharesToRemove = [
  new BN(100000), // Shares to remove from bin 1
  new BN(200000), // Shares to remove from bin 2
  new BN(300000), // Shares to remove from bin 3
  // ... and so on for each bin in the position
];

// Derive bin array PDAs
const [binArrayLowerPda] = PublicKey.findProgramAddressSync(
  [Buffer.from("bin_array"), pairPda.toBuffer(), Buffer.from([0])],
  program.programId
);

const [binArrayUpperPda] = PublicKey.findProgramAddressSync(
  [Buffer.from("bin_array"), pairPda.toBuffer(), Buffer.from([1])],
  program.programId
);

await program.methods
  .decreasePosition(sharesToRemove)
  .accounts({
    pair: pairPda,
    position: positionPda,
    binArrayLower: binArrayLowerPda,
    binArrayUpper: binArrayUpperPda,
    tokenMintX: tokenMintX,
    tokenMintY: tokenMintY,
    tokenVaultX: tokenVaultXPda,
    tokenVaultY: tokenVaultYPda,
    userVaultX: userVaultXPda,
    userVaultY: userVaultYPda,
    positionTokenAccount: positionTokenAccount,
    tokenProgramX: TOKEN_PROGRAM_ID,
    tokenProgramY: TOKEN_PROGRAM_ID,
    positionTokenProgram: TOKEN_2022_PROGRAM_ID,
    user: wallet.publicKey,
  })
  .signers([wallet.payer])
  .rpc();

To completely close a position and remove all liquidity:

  1. Call the close_position instruction

import { PublicKey, SystemProgram } from "@solana/web3.js";
import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";

// Derive bin array PDAs
const [binArrayLowerPda] = PublicKey.findProgramAddressSync(
  [Buffer.from("bin_array"), pairPda.toBuffer(), Buffer.from([0])],
  program.programId
);

const [binArrayUpperPda] = PublicKey.findProgramAddressSync(
  [Buffer.from("bin_array"), pairPda.toBuffer(), Buffer.from([1])],
  program.programId
);

await program.methods
  .closePosition()
  .accounts({
    pair: pairPda,
    position: positionPda,
    binArrayLower: binArrayLowerPda,
    binArrayUpper: binArrayUpperPda,
    tokenMintX: tokenMintX,
    tokenMintY: tokenMintY,
    tokenVaultX: tokenVaultXPda,
    tokenVaultY: tokenVaultYPda,
    userVaultX: userVaultXPda,
    userVaultY: userVaultYPda,
    positionTokenAccount: positionTokenAccount,
    tokenProgramX: TOKEN_PROGRAM_ID,
    tokenProgramY: TOKEN_PROGRAM_ID,
    positionTokenProgram: TOKEN_2022_PROGRAM_ID,
    user: wallet.publicKey,
  })
  .signers([wallet.payer])
  .rpc();

When decreasing a position, you need to specify the number of shares to remove from each bin. The number of shares must match the position width (upper_bin_id - lower_bin_id + 1).

You can calculate the shares to remove based on:

  • The percentage of liquidity you want to remove

  • The current liquidity in each bin

Example Calculation

// Calculate shares to remove based on percentage
const calculateSharesToRemove = (position, percentageToRemove) => {
  const positionWidth = position.upper_bin_id - position.lower_bin_id + 1;
  const sharesToRemove = [];
  
  for (let i = 0; i < positionWidth; i++) {
    const currentShares = position.liquidity_shares[i];
    const sharesToRemoveFromBin = currentShares.mul(new BN(percentageToRemove)).div(new BN(10000));
    sharesToRemove.push(sharesToRemoveFromBin);
  }
  
  return sharesToRemove;
};

// Example: Remove 50% of liquidity from a position
const sharesToRemove = calculateSharesToRemove(position, 5000);
  • width: number of bins in the position

  • liquidity_shares: existing shares per bin

  • percentageToRemove: e.g. 5000 = 50%

Closing a Position

Example Code

Calculating Shares to Remove

​
​
​
​
​