Meteora 백서

Meteora

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 44쪽

ng::prelude::*; #[derive(Accounts)] pub struct SwapThroughDlmm<'info> { /// CHECK: DLMM program account. pub dlmm_program: AccountInfo<'info>, /// CHECK: Validated by DLMM. #[account(mut)] pub lb_pair: AccountInfo<'info>, /// CHECK: Optional. Use the generated optional-account representation. #[account(mut)] pub bin_array_bitmap_extension: AccountInfo<'info>, /// CHECK: DLMM reserve token accounts. #[account(mut)] pub reserve_x: AccountInfo<'info>, /// CHECK: DLMM reserve token accounts. #[account(mut)] pub reserve_y: AccountInfo<'info>, /// CHECK: User token accounts. #[account(mut)] pub user_token_in: AccountInfo<'info>, /// CHECK: User token accounts. #[account(mut)] pub user_token_out: AccountInfo<'info>, /// CHECK: Pool token mints. pub token_x_mint: AccountInfo<'info>, /// CHECK: Pool token mints. pub token_y_mint: AccountInfo<'info>, /// CHECK: DLMM oracle. #[account(mut)] pub oracle: AccountInfo<'info>, /// CHECK: Optional host fee account. #[account(mut)] pub host_fee_in: AccountInfo<'info>, pub user: Signer<'info>, /// CHECK: SPL Token or Token-2022 program for token X. pub token_x_program: AccountInfo<'info>, /// CHECK: SPL Token or Token-2022 program for token Y. pub token_y_program: AccountInfo<'info>, /// CHECK: Memo program. pub memo_program: AccountInfo<'info>, /// CHECK: Event CPI program account generated by Anchor. pub event_program: AccountInfo<'info>, /// CHECK: Event authority PDA generated by Anchor. pub event_authority: AccountInfo<'info>, } ``` Then call the generated CPI and forward remaining accounts: ```rust theme={"system"} let remaining_accounts_info = RemainingAccountsInfo { slices: vec![] }; let cpi_ctx = CpiContext::new( ctx.accounts.dlmm_program.to_account_info(), dlmm::cpi::accounts::Swap2 { lb_pair: ctx.accounts.lb_pair.to_account_info(), bin_array_bitmap_extension: Some( ctx.accounts.bin_array_bitmap_extension.to_account_info(), ), reserve_x: ctx.accounts.reserve_x.to_account_info(), reserve_y: ctx.accounts.reserve_y.to_account_info(), user_token_in: ctx.accounts.user_token_in.to_account_info(), user_token_out: ctx.accounts.user_token_out.to_account_info(), token_x_mint: ctx.accounts.token_x_mint.to_account_info(), token_y_mint: ctx.accounts.token_y_mint.to_account_info(), oracle: ctx.accounts.oracle.to_account_info(), host_fee_in: None, user: ctx.accounts.user.to_account_info(), token_x_program: ctx.accounts.token_x_program.to_account_info(), token_y_program: ctx.accounts.token_y_program.to_account_info(), memo_program: ctx.accounts.memo_program.to_account_info(), program: ctx.accounts.event_program.to_account_info(), event_authority: ctx.accounts.event_authority.to_account_info(), }, ) .with_remaining_accounts(ctx.remaining_accounts.to_vec()); dlmm::cpi::swap2(cpi_ctx, amount_in, min_amount_out, remaining_accounts_info)?; ``` 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: ```rust theme={"system"} let remaining_accounts_info = RemainingAccountsInfo { slices: vec![ RemainingAccountsSlice { accounts_type: AccountsType::TransferHookX, length: x_hook_accounts_len, }, RemainingAccountsSlice { accounts_type: AccountsType::TransferHookY, length: y_hook_accounts_len, }, ], }; ``` 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. | Optional account | Required when | | - | - | | `bin_array_bitmap_extension` | The route may touch bin array indexes outside the default bitmap range. | | `host_fee_in` | A swap includes host/referral fee collection. | 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. ```typescript theme={"system"} const swapYtoX = true const binArrays = await pool.getBinArrayForSwap(swapYtoX, 4) const quote = pool.swapQuote(inAmount, swapYtoX, slippageBps, binArrays, false) const tx = await pool.swap({ inToken, outToken, inAmount: quote.consumedInAmount, minOutAmount: quote.minOutAmount, lbPair: pool.pubkey, user: user.publicKey, binArraysPubkey: quote.binArraysPubkey, }) const dlmmIx = tx.instructions.find((ix) => ix.programId.equals(new PublicKey("LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo")), ) console.log(dlmmIx?.keys.map((key) => ({ pubkey: key.pubkey.toBase58(), isSigner: key.isSigner, isWritable: key.isWritable, }))) ``` Mirror this account order in the accounts your program forwards to DLMM. ## Common CPI Failures | Error | Likely cause | | - | - | | `InvalidRemainingAccountSlice` | A `RemainingAccountsInfo` slice type is not valid for that instruction. | | `InsufficientRemainingAccounts` | Slice lengths require more accounts than were forwarded. | | `DuplicatedRemainingAccountTypes` | The same transfer-hook slice type was listed twice. | | `MissingRemainingAccountForTransferHook` | A Token-2022 transfer hook exists but the hook accounts were not provided. | | `NoTransferHookProgram` | Transfer-hook accounts were supplied for a mint without a transfer hook. | | `InvalidBinArray` / `NonContinuousBinArrays` | Bin arrays are missing, out of order, or not the arrays expected for the target bin range. | | `BitmapExtensionAccountIsNotProvided` | A route needs the bitmap extension account but the optional account was omitted. | | `ExceededAmountSlippageTolerance` | The quoted amount is stale or slippage bounds are too tight. | | `PoolDisabled` | The pool status or activation state does not allow the action. | ## Best Practices | Check | Detail | | - | - | | Quote before CPI | Quote off-chain with the TypeScript SDK or `commons`, then pass explicit bounds into your program. | | Forward generated order | Use generated bindings or compare against an SDK instruction. Main account order is not negotiable. | | Put transfer hooks before bin arrays | `parse_remaining_accounts` consumes hook slices first, then handlers consume leftover bin arrays. | | Include event CPI accounts | v2 handlers with `#[event_cpi]` expect generated event accounts. | | Handle Token-2022 per mint | Token X and token Y can use different token programs and different hook requirements. | | Budget compute | Swaps across many bin arrays, wide positions, claims across many bins, and Token-2022 hooks can be compute-heavy. | | Avoid stale state | Re-fetch pool, bitmap extension, bin arrays, mints, and clock before quoting. | | Test with real pools | Simulate on devnet and compare logs against a direct SDK-built transaction. | # DLMM Rust Integration Library Source: https://docs.meteora.ag/developer-guides/dlmm/rust-integration/library Learn how to use the DLMM Rust library for installation, account decoding, PDA helpers, bin arrays, swap quotes, and tests. Use the `commons` crate when you need Rust tooling around DLMM. ## Install If your project is in the same workspace as `dlmm-sdk`, depend on the local crate by path: ```toml theme={"system"} [dependencies] commons = { path = "../dlmm-sdk/commons" } ``` The crate uses the generated Anchor client module from the DLMM IDL: ```rust theme={"system"} use commons::dlmm; use commons::dlmm::accounts::{BinArray, BinArrayBitmapExtension, LbPair}; use commons::{derive_bin_array_pda, quote_exact_in, BinArrayExtension, LbPairExtension}; ``` ## 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 generated `dlmm` module points at the public DLMM program: ```rust theme={"system"} use commons::dlmm; let program_id = dlmm::ID; ``` Mainnet and devnet use the same public program ID: ```rust theme={"system"} pub const DLMM_PROGRAM_ID: &str = "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo"; ``` ## Account Decoding The generated `commons::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: ```rust theme={"system"} use commons::dlmm::accounts::LbPair; use commons::pod_read_unaligned_skip_disc; use solana_sdk::account::Account; fn decode_lb_pair(account: &Account) -> anyhow::Result<LbPair> { pod_read_unaligned_skip_disc::<LbPair>(&account.data) } ``` The helper checks that the account data is long enough, skips the discriminator, and reads exactly `size_of::<T>()` bytes. ## PDA Helpers Use `commons::pda` helpers instead of duplicating DLMM seeds: ```rust theme={"system"} use commons::{ derive_bin_array_pda, derive_bin_array_bitmap_extension, derive_event_authority_pda, derive_oracle_pda, derive_position_pda, derive_reserve_pda, }; use solana_sdk::pubkey::Pubkey; let lb_pair = Pubkey::new_unique(); let owner = Pubkey::new_unique(); let token_x_mint = Pubkey::new_unique(); let (oracle, _) = derive_oracle_pda(lb_pair); let (bitmap_extension, _) = derive_bin_array_bitmap_extension(lb_pair); let (bin_array, _) = derive_bin_array_pda(lb_pair, 0); let (reserve_x, _) = derive_reserve_pda(token_x_mint, lb_pair); let (position, _) = derive_position_pda(lb_pair, owner, -34, 69); let (event_authority, _) = derive_event_authority_pda(); ``` Common derivation helpers: | 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: ```rust theme={"system"} use commons::BinArrayExtension; use commons::dlmm::accounts::BinArray; let bin_array_index = BinArray::bin_id_to_bin_array_index(active_bin_id)?; let (lower_bin_id, upper_bin_id) = BinArray::get_bin_array_lower_upper_bin_id(bin_array_index)?; let account_metas = BinArray::get_bin_array_account_metas_coverage(lower_bin_id, upper_bin_id, lb_pair)?; ``` For swaps, use `get_bin_array_pubkeys_for_swap`. It walks the pool bitmap and, when supplied, the bitmap extension: ```rust theme={"system"} use commons::quote::get_bin_array_pubkeys_for_swap; let swap_for_y = true; let bin_arrays = get_bin_array_pubkeys_for_swap( lb_pair_pubkey, &lb_pair_state, bitmap_extension.as_ref(), swap_for_y, 3, )?; ``` `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 ```rust theme={"system"} use commons::quote::quote_exact_in; use std::collections::HashMap; let quote = quote_exact_in( lb_pair_pubkey, &lb_pair_state, amount_in, swap_for_y, bin_arrays_by_pubkey, bitmap_extension.as_ref(), &clock, &mint_x_account, &mint_y_account, )?; println!( "amount_out={} fee={} protocol_fee={}", quote.amount_out, quote.fee, quote.protocol_fee ); ``` ### Exact-Out ```rust theme={"system"} use commons::quote::quote_exact_out; let quote = quote_exact_out( lb_pair_pubkey, &lb_pair_state, amount_out, swap_for_y, bin_arrays_by_pubkey, bitmap_extension.as_ref(), &clock, &mint_x_account, &mint_y_account, )?; println!( "amount_in={} fee={} protocol_fee={}", quote.amount_in, quote.fee, quote.protocol_fee ); ``` `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 generated `dlmm::client` module can build account metas and instruction data. The integration tests in `commons/tests/integration/test_swap.rs` show the full pattern. ```rust theme={"system"} use anchor_lang::InstructionData; use anchor_lang::ToAccountMetas; use commons::dlmm; use commons::dlmm::types::RemainingAccountsInfo; use commons::derive_event_authority_pda; use solana_sdk::instruction::{AccountMeta, Instruction}; let (event_authority, _) = derive_event_authority_pda(); let mut accounts = dlmm::client::accounts::Swap2 { lb_pair, oracle, bin_array_bitmap_extension: Some(bitmap_extension), reserve_x, reserve_y, user_token_in, user_token_out, token_x_mint, token_y_mint, host_fee_in: None, user, token_x_program, token_y_program, program: dlmm::ID, event_authority, memo_program: spl_memo::ID, } .to_account_metas(None); accounts.extend(bin_array_pubkeys.into_iter().map(|pubkey| { AccountMeta::new(pubkey, false) })); let ix = Instruction { program_id: dlmm::ID, accounts, data: dlmm::client::args::Swap2 { amount_in, min_amount_out, remaining_accounts_info: RemainingAccountsInfo { slices: vec![] }, } .data(), }; ``` For Token-2022 transfer hooks, populate `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. | For v2 instructions that may transfer Token-2022 mints, pass the extra account metas after the main accounts and encode the matching `RemainingAccountsInfo`: ```rust theme={"system"} use commons::{ get_potential_token_2022_related_ix_data_and_accounts, ActionType, }; use commons::dlmm::types::RemainingAccountsInfo; let token_2022_accounts = get_potential_token_2022_related_ix_data_and_accounts( &lb_pair_state, rpc_client, ActionType::Liquidity, ) .await?; let remaining_accounts_info = if let Some((slices, metas)) = token_2022_accounts { accounts.extend(metas); RemainingAccountsInfo { slices } } else { RemainingAccountsInfo { slices: vec![] } }; ``` Use `ActionType::Reward(index)` when building remaining accounts for a reward claim or reward-aware flow. ## Position And Limit Order Reads Use `DynamicPosition::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. ```rust theme={"system"} use commons::dlmm::accounts::PositionV2; use commons::{pod_read_unaligned_skip_disc, DynamicPosition}; use std::collections::HashMap; let position = pod_read_unaligned_skip_disc::<PositionV2>(&position_account.data)?; let parsed = DynamicPosition::parse( &position, &position_account.data, &lb_pair_state, &bin_arrays_by_index, current_timestamp, )?; println!( "x={} y={} fee_x={} fee_y={}", parsed.total_x_amount, parsed.total_y_amount, parsed.fee_x, parsed.fee_y ); ``` Use account filters when scanning positions or limit orders: ```rust theme={"system"} use commons::{limit_order_filter_by_owner_and_pair, position_filter_by_wallet_and_pair}; let position_filters = position_filter_by_wallet_and_pair(owner, lb_pair); let limit_order_filters = limit_order_filter_by_owner_and_pair(owner, lb_pair); ``` ## Testing The `commons` 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. | Run the commons tests from the SDK repository: ```bash theme={"system"} cargo test -p commons --test integration ``` ## 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. | # DLMM TS SDK Examples Source: https:/
프로젝트로 돌아가기