Skip to main content
Use CPI when your Solana program must call DLMM directly, for example to swap, add or remove liquidity, claim fees or rewards, or manage a DLMM position as part of a larger protocol instruction.
The stable integration contract is the published DLMM IDL and the official SDK behavior. This page is cross-checked against the lb_clmm source so the account ordering and remaining-account notes match the on-chain handlers.

Program ID

The public program ID is the same for mainnet and devnet.

Generated Bindings

Prefer generated CPI bindings from the DLMM IDL. The lb_clmm handlers use Anchor accounts, optional accounts, generated instruction args, and #[event_cpi] on many v2 instructions.
Do not hand-type account order from memory. Generate bindings or compare against SDK-built instructions. Most CPI failures come from one misplaced optional account, event authority account, bin array, or Token-2022 remaining account.
If you use Anchor-generated CPI modules, the handler usually looks like:
If your integration uses raw invoke / invoke_signed, build the same instruction data and account metas produced by the generated client.

CPI Instruction Scope

For the full lb_clmm instruction-family map, see DLMM Program Instructions. This page focuses on CPI implementation details for the v2 instructions most integrations call: swap2, swap_exact_out2, add_liquidity2, remove_liquidity2, claim_fee2, claim_reward2, and rebalance_liquidity. New CPI integrations should prefer v2 instructions because they support SPL Token, Token-2022, dynamic positions, and explicit remaining account metadata.

Event CPI Accounts

Several lb_clmm account structs use #[event_cpi], including Swap2, AddLiquidity2, RemoveLiquidity2, ClaimFee2, and ClaimReward2. Generated clients include extra event CPI accounts, commonly: When you inspect an SDK or generated Rust instruction, keep these accounts in the exact generated position. If they are missing or moved, Anchor account deserialization can fail before the DLMM handler reaches your intended logic.

Remaining Accounts Ordering

lb_clmm v2 handlers parse remaining accounts in two phases.
  1. They consume Token-2022 transfer-hook account groups according to RemainingAccountsInfo.slices.
  2. They consume the leftover accounts as instruction-specific accounts, usually bin arrays.
This ordering comes from src/utils/remaining_accounts_util.rs and calls like:
The order of ctx.remaining_accounts must match the order of RemainingAccountsInfo.slices.
If you have no transfer-hook accounts, pass RemainingAccountsInfo { slices: vec![] }, then put bin arrays first in remaining_accounts.

Instruction Account Notes

Swap2 CPI Shape

This shape mirrors swap2 on the program.
Then call the generated CPI and forward remaining accounts:
If your program signs for the DLMM user account with a PDA, use CpiContext::new_with_signer and pass your signer seeds. The token account owner must still match the authority DLMM expects for the transfer.

Token-2022 Transfer Hooks

For Token-2022 mints with transfer hooks, include the extra account metas in ctx.remaining_accounts and describe them with RemainingAccountsInfo.slices. Example ordering for swap2 with token X and token Y transfer hooks:
Then pass remaining accounts in this exact order:
  1. Token X transfer-hook accounts.
  2. Token Y transfer-hook accounts.
  3. Bin array accounts for the swap route.
For host/referral fee transfers, include TransferHookReferral when the host fee mint requires hook accounts.

Optional Accounts

Some generated clients represent optional accounts as Option<AccountInfo>. Others use an explicit placeholder account when constructing raw account metas. When using generated Anchor CPI bindings, follow the generated optional-account type. When using raw Instruction construction, compare against the TypeScript SDK or commons generated client output for the exact placeholder behavior.

Off-Chain Account Planning

Use the Typescript SDK or commons crate to plan accounts before calling your CPI program.
Mirror this account order in the accounts your program forwards to DLMM.

Common CPI Failures

Best Practices