commons crate when you need Rust tooling around DLMM.
Install
If your project is in the same workspace asdlmm-sdk, depend on the local
crate by path:
What It Provides
| Module | Main exports | Use |
|---|---|---|
dlmm | Generated accounts, instructions, args, and types from the DLMM IDL. | Build off-chain instructions or decode IDL account layouts. |
quote | quote_exact_in, quote_exact_out, get_bin_array_pubkeys_for_swap. | Quote swaps in Rust before building a transaction. |
pda | derive_lb_pair_with_preset_parameter_key, derive_bin_array_pda, derive_position_pda, derive_reserve_pda, and related helpers. | Derive DLMM PDAs without duplicating seed logic. |
extensions | LbPairExtension, BinArrayExtension, BinExtension, PositionExtension, DynamicPosition, LimitOrderExtension. | Work with decoded account data using higher-level methods. |
token_2022 | Transfer-fee math and transfer-hook account helpers. | Build Token-2022-aware quotes and remaining account lists. |
account_filters | position_filter_by_wallet_and_pair, limit_order_filter_by_owner_and_pair. | Query user positions and limit orders with memcmp filters. |
rpc_client_extension | RpcClientExtension. | Fetch and deserialize accounts with one async helper. |
Program ID
The generateddlmm module points at the public DLMM program:
Account Decoding
The generatedcommons::dlmm module exposes IDL account types such as
LbPair, BinArray, BinArrayBitmapExtension, PositionV2, and
LimitOrder.
Use pod_read_unaligned_skip_disc for zero-copy account layouts that start with
the Anchor 8-byte discriminator:
size_of::<T>() bytes.
PDA Helpers
Usecommons::pda helpers instead of duplicating DLMM seeds:
| Helper | Derives |
|---|---|
derive_lb_pair_with_preset_parameter_key | Current v2 permissionless pool PDA using PresetParameter2. |
derive_lb_pair_pda2 | Legacy-style pool PDA from mints, bin_step, and base_factor. |
derive_customizable_permissionless_lb_pair | Customizable permissionless pool PDA. |
derive_permission_lb_pair_pda | Permissioned pool PDA. |
derive_position_pda | Position PDA from pool, base, lower bin ID, and width. |
derive_bin_array_pda | Bin array PDA by pool and bin array index. |
derive_bin_array_bitmap_extension | Bitmap extension PDA for out-of-default-range bin arrays. |
derive_reserve_pda | Pool reserve PDA for a token mint. |
derive_reward_vault_pda | Reward vault PDA for a reward index. |
derive_token_badge_pda | Token badge PDA for Token-2022 pool creation. |
Bin Arrays
DLMM liquidity is organized in bin arrays.BinArrayExtension gives you the
same index and coverage helpers used by the quote code:
get_bin_array_pubkeys_for_swap. It walks the pool bitmap and,
when supplied, the bitmap extension:
swap_for_y = true means token X is swapped for token Y. swap_for_y = false
means token Y is swapped for token X.
Swap Quotes
The quote functions simulate DLMM swap execution against decoded account state. They support:- Current pool status and activation checks.
- Base and variable fee updates.
- Bin traversal and bitmap extension lookup.
- Limit-order liquidity when the pool supports it.
- Token-2022 transfer fees on input and output mints.
Exact-In
Exact-Out
bin_arrays_by_pubkey is a HashMap<Pubkey, BinArray> keyed by the bin array
accounts you fetched for the route. If the quote returns Active bin array not found or Pool out of liquidity, fetch more bin arrays in the swap direction.
Building Swap Instructions
The generateddlmm::client module can build account metas and instruction
data. The integration tests in commons/tests/integration/test_swap.rs show the
full pattern.
remaining_accounts_info and append the
extra account metas described in the next section.
Token-2022
commons::token_2022 handles two separate concerns:
| Helper | Use |
|---|---|
get_epoch_transfer_fee | Read the active transfer-fee config for a mint account and epoch. |
calculate_transfer_fee_excluded_amount | Convert a transfer-fee-included amount into the amount that reaches the receiver. |
calculate_transfer_fee_included_amount | Compute the pre-fee amount required for a target post-fee amount. |
get_extra_account_metas_for_transfer_hook | Fetch transfer-hook extra account metas for one mint. |
get_potential_token_2022_related_ix_data_and_accounts | Build RemainingAccountsSlice data and account metas for liquidity or reward actions. |
RemainingAccountsInfo:
ActionType::Reward(index) when building remaining accounts for a reward
claim or reward-aware flow.
Position And Limit Order Reads
UseDynamicPosition::parse when you need a backend-friendly view of a
position. It combines the base PositionV2 account, dynamic extension bytes,
pool state, bin arrays, fee checkpoints, and reward checkpoints.
Testing
Thecommons crate includes integration tests that load DLMM program fixtures
and serialized account data into solana-program-test:
| Test | What it covers |
|---|---|
tests/integration/test_swap.rs | Exact-in and exact-out swap quotes and generated Swap2 / SwapExactOut2 instructions. |
tests/integration/test_swap_token2022.rs | Token-2022 swap behavior. |
tests/integration/test_swap_quote_with_limit_order.rs | Quote behavior when limit order liquidity is part of the pool. |
Best Practices
| Practice | Detail |
|---|---|
| Fetch fresh state before quoting | Quotes depend on active_id, fees, bin liquidity, limit orders, token transfer fees, and the current clock. |
| Fetch enough bin arrays | get_bin_array_pubkeys_for_swap takes a count. Larger swaps may need more arrays, but every extra array increases account and compute pressure. |
| Include bitmap extension when needed | Pass Some(&bitmap_extension) when route discovery can leave the default bitmap range. |
| Keep slippage explicit | Use quote.amount_out or quote.amount_in to derive user-facing min_amount_out or max_in_amount; do not send unbounded swaps. |
| Treat Token-2022 separately | Transfer fees change quoted amounts, and transfer hooks require extra account metas and matching RemainingAccountsInfo. |
| Compare with TypeScript SDK output | When in doubt, build the same transaction with @meteora-ag/dlmm and compare account order, remaining accounts, and instruction data. |

