Depositing / withdrawing liquidity

Deposit tokens into a CPMM pool to receive LP tokens, or burn LP tokens to withdraw your share of the pool.


Fetching pool info

Both deposit and withdraw operations require pool information.

import {
  ApiV3PoolInfoStandardItemCpmm,
  CpmmKeys,
  CREATE_CPMM_POOL_PROGRAM,
  DEVNET_PROGRAM_ID,
} from '@raydium-io/raydium-sdk-v2'
import { initSdk } from '../config'

const VALID_PROGRAM_ID = new Set([
  CREATE_CPMM_POOL_PROGRAM.toBase58(),
  DEVNET_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM.toBase58(),
])
const isValidCpmm = (id: string) => VALID_PROGRAM_ID.has(id)

let poolInfo: ApiV3PoolInfoStandardItemCpmm
let poolKeys: CpmmKeys | undefined

const raydium = await initSdk()
const poolId = 'YOUR_POOL_ID'

if (raydium.cluster === 'mainnet') {
  const data = await raydium.api.fetchPoolById({ ids: poolId })
  poolInfo = data[0] as ApiV3PoolInfoStandardItemCpmm
  if (!isValidCpmm(poolInfo.programId)) throw new Error('target pool is not CPMM pool')
} else {
  const data = await raydium.cpmm.getPoolInfoFromRpc(poolId)
  poolInfo = data.poolInfo
  poolKeys = data.poolKeys
}

Depositing liquidity

Use raydium.cpmm.addLiquidity() to deposit tokens into a pool. You specify one token amount, and the SDK calculates the required amount of the other token based on the current pool ratio.

Computing the paired amount (optional)

Before depositing, you can preview the required amount of the other token for UI display:

Deposit parameters

Parameter
Type
Description

poolInfo

object

Pool info from API or RPC.

poolKeys

object

Pool keys, required for devnet. Optional on mainnet (SDK derives them).

inputAmount

BN

Amount to deposit for the base or quote token, in smallest units.

slippage

Percent

Slippage tolerance. new Percent(1, 100) = 1%.

baseIn

boolean

true if inputAmount is for token A, false for token B.

txVersion

TxVersion

Transaction version.


Withdrawing liquidity

Use raydium.cpmm.withdrawLiquidity() to burn LP tokens and receive back your proportional share of both tokens.

Withdraw parameters

Parameter
Type
Description

poolInfo

object

Pool info from API or RPC.

poolKeys

object

Pool keys, required for devnet.

lpAmount

BN

Amount of LP tokens to burn, in smallest units.

slippage

Percent

Slippage tolerance for minimum token amounts received.

txVersion

TxVersion

Transaction version.

circle-info

By default, the SDK automatically closes the wSOL account and returns native SOL. To keep the wSOL account open, pass closeWsol: false.

Last updated

Was this helpful?