Meteora 백서

Meteora

원문

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

, computes trade, protocol, partner, and optional host fees, then withdraws output through the opposite vault. | Parameter | Meaning | | - | - | | `in_amount` | Exact source token amount. | | `minimum_out_amount` | Minimum destination token amount accepted by the user. | | Remaining depeg accounts | Required for stable LST/depeg pools. | | Optional host fee account | Referral token account that receives the host fee share when provided. | ## Liquidity | Instruction | Use | | - | - | | `add_balance_liquidity` | Adds balanced liquidity by minting a target amount of pool LP, bounded by max token A and B inputs. | | `add_imbalance_liquidity` | Adds liquidity with imbalanced token A/B inputs. Only supported by stable pools. | | `remove_balance_liquidity` | Burns pool LP and withdraws token A/B proportionally. This remains available when a pool is disabled. | | `remove_liquidity_single_side` | Burns pool LP and withdraws one token. Only supported by stable pools. | | `bootstrap_liquidity` | Re-seeds a depleted pool with token A and token B liquidity. | | `get_pool_info` | Emits pool token amounts, virtual price, and timestamp for read-style integrations. | Balanced operations are the normal constant-product path. Stable pools additionally support imbalanced deposits and single-sided withdrawals. ## LP Locks | Instruction | Use | | - | - | | `create_lock_escrow` | Initializes a lock escrow PDA for an owner and pool. | | `lock` | Transfers LP tokens into the escrow and updates the virtual-price checkpoint. | | `claim_fee` | Claims token A/B fees accrued by locked LP, bounded by `max_amount`. | Lock fee accounting is LP-denominated internally. Claiming converts claimable LP value into token A/B by using the pool's balanced withdrawal logic. ## Configs And Activation | Instruction | Use | | - | - | | `create_config` | Admin-only instruction that creates a config PDA from `config` + `index`. | | `close_config` | Admin-only instruction that closes a config account and returns rent. | | `update_activation_point` | Admin-only instruction that updates a launch pool's activation point before the program-defined cutoff. | | `set_whitelisted_vault` | Admin-only instruction that updates the launch pool whitelisted alpha vault before pre-activation swaps begin. | Config-created pools inherit `pool_fees`, `activation_duration`, `vault_config_key`, `pool_creator_authority`, `activation_type`, and `partner_fee_numerator`. If `pool_creator_authority` is not the default pubkey, that authority must sign pool creation from the config. | `ConfigParameters` field | Meaning | | - | - | | `trade_fee_numerator` | Trade fee numerator using `FEE_DENOMINATOR = 100_000`. | | `protocol_trade_fee_numerator` | Protocol share numerator using `FEE_DENOMINATOR = 100_000`. | | `activation_duration` | Duration added to the current slot or timestamp to derive default activation. | | `vault_config_key` | Alpha Vault config/base key. Default pubkey means no alpha vault. | | `pool_creator_authority` | Public config when default; private config when non-default. | | `activation_type` | `0` for slot activation, `1` for timestamp activation. | | `index` | Config PDA index. | | `partner_fee_numerator` | Partner share of protocol fees using `FEE_DENOMINATOR = 100_000`. | ## Pool Controls And Fees | Instruction | Use | | - | - | | `enable_or_disable_pool` | Admin-only instruction that toggles pool enablement. Disabled pools still allow balanced withdrawals. | | `set_pool_fees` | Updates pool trade, protocol, and partner fee settings, subject to fee authority and validation rules. | | `override_curve_param` | Updates stable-swap curve parameters without changing the curve type. Only stable amp changes are honored. | | `partner_claim_fee` | Lets the configured partner authority claim pending partner token A/B fees. | `trade_fee_numerator`, `protocol_trade_fee_numerator`, host fee, and partner fee values use `FEE_DENOMINATOR = 100_000`, not direct basis points. ## Operator And Admin Instructions | Instruction | Use | | - | - | | `withdraw_protocol_fees` | Operator-gated instruction that withdraws protocol token A/B fees up to caller-provided maximums. | | `zap_protocol_fee` | Operator-gated instruction that zaps protocol fees through approved Jupiter v6 route accounts. | | `create_operator_account` | Admin-only instruction that creates an operator permission PDA for a whitelisted address. | | `close_operator_account` | Admin-only instruction that closes an operator account and returns rent. | Operator permissions are stored as a bitmap. Bit `0` allows protocol fee claiming, and bit `1` allows protocol fee zapping. ## Validation | Area | Rule | | - | - | | Dynamic Vaults | Pool quotes and transactions need vault state, vault token accounts, vault LP mints, and pool-held vault LP token accounts. Reading only the pool account is not enough. | | Activation | Launch pools can block swaps until `bootstrapping.activation_point`. Slot and timestamp activation use different current-point sources. | | Alpha Vault | Config or customizable pools with alpha-vault support apply stricter activation timing and quote-mint checks on mainnet builds. | | Stable pools | Single-sided withdrawal and imbalanced deposits are stable-pool features. Constant-product pools should use balanced liquidity operations. | | Depeg pools | LST/depeg stable pools need remaining stake/depeg accounts for quotes and swaps. | | Fees | Fee numerators use `FEE_DENOMINATOR = 100_000`. SDK config helpers that accept bps convert bps into this denominator. | | Referrals | Host fee routing depends on an optional referral token account passed as a swap remaining account. | | Disabled pools | Swaps and most liquidity operations are blocked, but balanced withdrawal remains available so LPs can exit. | # DAMM v1 CPI Integration Source: https://docs.meteora.ag/developer-guides/damm-v1/rust-integration/cpi Learn how to plan DAMM v1 CPI integrations with Anchor crates, account requirements, PDA helpers, swaps, liquidity, locks, and common constraints. DAMM v1 can be invoked from Anchor programs through the `dynamic-amm` program crate included in `damm-v1-sdk/programs/dynamic-amm`. CPI builders must pass the same pool, Dynamic Vault, token, mint, and remaining accounts required by the on-chain instruction. <Warning> Prefer the TypeScript SDK or Rust off-chain helpers for ordinary app transactions. Use CPI only when another on-chain program must compose directly with DAMM v1. </Warning> ## Dependency Setup The local program crate exposes CPI types through its default `cpi` feature. ```toml theme={"system"} [dependencies] dynamic-amm = { path = "../damm-v1-sdk/programs/dynamic-amm", features = ["cpi"] } dynamic-vault = { path = "../damm-v1-sdk/programs/dynamic-vault", features = ["cpi"] } anchor-lang = "0.28.0" anchor-spl = "0.28.0" ``` For a published or vendored integration, pin the exact source revision that matches the deployed DAMM v1 IDL you build against. ## Source Examples <CardGroup> <Card title="Public CPI Examples" icon="github" href="https://github.com/MeteoraAg/cpi-examples"> Meteora's public CPI examples include DAMM v1 swap, pool initialization, config-based initialization, lock liquidity, claim fee, and tests. </Card> </CardGroup> The local `damm-v1-sdk/common` crate also contains account builders that are useful when preparing CPI or off-chain instruction account sets. ## Account Planning Every DAMM v1 CPI has two account layers: | Layer | Accounts | | - | - | | DAMM v1 pool layer | `pool`, pool LP mint, pool-held Dynamic Vault LP token accounts, protocol fee accounts, lock escrow accounts when needed. | | Dynamic Vault layer | `a_vault`, `b_vault`, each vault token account, each vault LP mint, Dynamic Vault program. | Use `common::dynamic_amm::pda` and `common::dynamic_vault::pda` helpers to derive canonical addresses. ## Swap CPI `swap` takes `in_amount` and `minimum_out_amount`. | Account | Requirement | | - | - | | `pool` | DAMM v1 pool. Must be enabled and active. | | `user_source_token` | User source token account. Mint must match token A or token B. | | `user_destination_token` | User destination token account. Mint must be the opposite pool token. | | `a_vault`, `b_vault` | Dynamic Vault accounts stored in pool state. | | `a_token_vault`, `b_token_vault` | Token vault accounts stored in each Dynamic Vault. | | `a_vault_lp_mint`, `b_vault_lp_mint` | Dynamic Vault LP mints. | | `a_vault_lp`, `b_vault_lp` | Pool-held Dynamic Vault LP token accounts. | | `protocol_token_fee` | Protocol fee token account for the input mint. | | `user` | Signer and owner of `user_source_token`. | | `vault_program`, `token_program` | Dynamic Vault and SPL Token programs. | | Remaining accounts | Depeg accounts for LST pools, then optional host fee account for referral routing. | <Tip> Quote off-chain immediately before invoking CPI. Use the quoted minimum output as `minimum_out_amount`. </Tip> ## Liquidity CPI | Instruction | Use | Important constraints | | - | - | - | | `add_balance_liquidity` | Balanced deposit. | Constant-product pools use balanced liquidity after bootstrap. | | `add_imbalance_liquidity` | Stable imbalanced deposit. | Stable pools only. | | `remove_balance_liquidity` | Balanced withdrawal. | Allowed even when the pool is disabled. | | `remove_liquidity_single_side` | Stable single-sided withdrawal. | Stable pools only. | | `bootstrap_liquidity` | Re-seed depleted pool. | Pool must satisfy depleted-pool checks. | Liquidity instructions need pool LP mint, user token accounts, user pool LP account, both Dynamic Vault accounts, vault token accounts, vault LP mints, and pool-held vault LP token accounts. ## Lock CPI | Instruction | Use | Notes | | - | - | - | | `create_lock_escrow` | Initialize a lock escrow PDA. | PDA seeds are `lock_escrow`, pool, owner. | | `lock` | Transfer LP into the escrow vault. | `owner` can be any signer funding the lock amount. | | `claim_fee` | Claim token A/B fees accrued by locked LP. | Requires the lock escrow, escrow vault, user LP/token accounts, pool/vault accounts, and vault program. | Lock fee accounting depends on current pool virtual price. Pass fresh vault and LP account data so the instruction can compute the correct fee. ## Pool Creation CPI Pool creation CPI is possible but account-heavy because it initializes the pool, LP mint, pool-held vault LP accounts, protocol fee accounts, payer LP ATA, and LP metadata. | Instruction | Notes | | - | - | | `initialize_permissionless_pool` | Uses curve default fee and PDA from curve type plus sorted mints. | | `initialize_permissionless_pool_with_fee_tier` | Adds the fee-tier seed when the fee differs from the default. | | `initialize_permissionless_constant_product_pool_with_config` | Uses config PDA, sorted mints, and config-controlled activation. | | `initialize_customizable_permissionless_constant_product_pool` | Uses `CustomizableParams` and the `pool` seed with sorted mints. | For most CPI use cases, initialize pools off-chain first and CPI only into swap, liquidity, or lock flows. ## Unsupported Or Restricted CPI Paths | Area | Constraint | | - | - | | Operator zap | Source includes explicit `CpiDisabled` and strict transaction sysvar validation for zap-related protocol fee flows. Treat zap protocol fee as an operator off-chain transaction path. | | Admin config and operator creation | Admin signer restrictions apply. These are not ordinary CPI integration flows. | | Activation updates | Launch-pool activation changes are restricted by activation timing windows. | | Depeg pools | CPI callers must include the same depeg remaining accounts required by direct transactions. | ## Common CPI Errors | Error | Likely cause | | - | - | | `InvalidVaultAccount` | Dynamic Vault account or vault token account does not match pool state. | | `InvalidVaultLpAccount` | Pool-held vault LP token account is wrong. | | `MismatchedTokenMint` | User source/destination, protocol fee, or vault token mint is wrong. | | `InvalidProtocolFeeAccount` | Protocol fee token account does not match the input mint's fee PDA. | | `PoolDisabled` | Swap or restricted liquidity operation attempted while pool is disabled. | | `InvalidActivationPoint` / `PreActivationSwapStarted` | Launch-pool timing constraints are not satisfied. | | `InvalidRemainingAccountsLen` / `InvalidRemainingAccounts` | Depeg, referral, or zap remaining accounts are missing or ordered incorrectly. | | `ExceededSlippage` | CPI supplied minimum output or liquidity threshold is too strict for current state. | ## Checklist | Step | Action | | - | - | | 1 | Derive the pool, LP mint, protocol fee account, vault LP accounts, and lock escrow with helper functions. | | 2 | Fetch current pool and Dynamic Vault state off-chain for quoting and account validation. | | 3 | Include required depeg remaining accounts for LST stable pools. | | 4 | Simulate the outer transaction with realistic compute budget and fresh blockhash. | | 5 | Reconcile Anchor logs and token transfers after execution. | # DAMM v1 Rust Library Source: https://docs.meteora.ag/developer-guides/damm-v1/rust-integration/library Learn how DAMM v1 Rust helpers support quotes, PDA derivation, account building, CLI flows, and pool state reads. The `damm-v1-sdk` repository includes Rust crates for quote computation, PDA derivation, account construction, and CLI workflows. These are useful for routers, backends, indexers, and programs that need deterministic DAMM v1 account planning. ## Crates | Crate | Path | Use | | - | - | - | | `dynamic-amm-quote` | `dynamic-amm-quote` | Pure Rust quote and pool-token computation from fetched account data. | | `common` | `common` | PDA helpers and instruction account builders for DAMM v1 and Dynamic Vault. | | `cli` | `rust-client` | Example CLI for creating pools, quoting, swapping, depositing, withdrawing, and reading pool info. | | `dynamic-amm` | `programs/dynamic-amm` | Anchor client/CPI-compatible program crate used by the helper crates. | | `dynamic-vault` | `programs/dynamic-vault` | Dynamic Vault program crate used for vault state and account derivation. | ## Versions | Dependency | Version | | - | - | | Anchor | `0.28.0` | | Solana | `1.16.0` / `1.16` in tests | | `spl-token` | `3.5.0` | | `spl-token-swap` | `3.0.0` | | `spl-associated-token-account` | `2.2.0` | | `mpl-token-metadata` | `3.2.3` | ## Quote Library `dynamic_amm_quote::compute_quote` computes an exact-in swap quote from fully fetched account data. ```rust theme={"system"} use dynamic_amm_quote::{compute_quote, QuoteData}; use solana_sdk::pubkey::Pubkey; let quote = compute_quote(source_mint, in_amount, quote_data)?; println!("out amount: {}", quote.out_amount); println!("fee: {}", quote.fee); ``` `QuoteData` must include the pool, both Dynamic Vault accounts, the pool's vault LP token accounts, both vault LP mints, both vault token accounts, the clock sysvar, and any stake/depeg account data required by the curve. | `QuoteData` field | Account source | | - | - | | `pool` | DAMM v1 `Pool` account. | | `vault_a`, `vault_b` | Dynamic Vault state accounts from `pool.a_vault` and `pool.b_vault`. | | `pool_vault_a_lp_token`, `pool_vault_b_lp_token` | SPL token accounts at `pool.a_vault_lp` and `pool.b_vault_lp`. | | `vault_a_lp_mint`, `vault_b_lp_mint` | Vault LP mint accounts from each Dynamic Vault. | | `vault_a_token`, `vault_b_token` | Token vault accounts from each Dynamic Vault. | | `clock` | Solana clock sysvar. | | `stake_data` | Map of depeg or stake accounts for LST stable pools. Empty for non-depeg pools. | `compute_quote` validates pool status and activation state, updates depeg virtual price, computes the pool's underlying token A/B amounts through Dynamic Vault shares, applies trade and protocol fees, runs the curve calculation, and returns the quoted output amount and fee. ## Pool Token Computation ```rust theme={"system"} use dynamic_amm_quote::{compute_pool_tokens, VaultInfo}; let (token_a_amount, token_b_amount) = compute_pool_tokens( current_time, vault_a_info, vault_b_info, )?; ``` Use `compute_pool_tokens` when an indexer or backend needs the underlying token balances represented by the pool's Dynamic Vault LP shares. ## PDA Helpers The `common::dynamic_amm::pda` module mirrors the TypeScript PDA helpers. | Helper | Use | | - | - | | `derive_permissionless_pool_key(curve_type, token_a_mint, token_b_mint)` | Default-fee permissionless pool PDA. | | `derive_permissionless_pool_key_with_fee_tier(curve_type, token_a_mint, token_b_mint, trade_fee_bps)` | Fee-tier permissionless pool PDA. | | `derive_permissionless_constant_product_pool_with_config_key(mint_a, mint_b, config)` | Config-based constant-product pool PDA. | | `derive_customizable_permissionless_constant_product_pool_key(mint_a, mint_b)` | Customizable constant-product pool PDA. | | `derive_lp_mint_key(pool_key)` | Pool LP mint PDA, with compatibility lookup for known legacy non-PDA LP mints. | | `derive_protocol_fee_key(mint_key, pool_key)` | Protocol fee token account PDA. | | `derive_vault_lp_key(vault_key, pool_key)` | Pool-owned Dynamic Vault LP token account PDA. | | `derive_lock_escrow_key(pool_key, owner_key)` | Lock escrow PDA. | | `derive_metadata_key(lp_mint)` | LP mint metadata PDA. | The helper sorts mints before deriving permissionless and config-based pool addresses. ## Instruction Account Builder `common::dynamic_amm::ix_account_builder::IxAccountBuilder` builds account structs for common pool initialization paths. | Builder | Use | | - | - | | `initialize_permissionless_pool_accounts(...)` | Account set for default-fee permissionless pool creation. | | `initialize_permissionless_pool_with_fee_tier_accounts(...)` | Account set for fee-tier permissionless pool creation. | | `initialize_permissionless_constant_product_pool_with_config_accounts(...)` | Account set for config-based constant-product pool creation. | The builder derives Dynamic Vault keys, vault token accounts, vault LP mints, pool LP mint, protocol fee accounts, vault LP token accounts, LP metadata, and payer ATAs. ## CLI Examples The `cli` crate demonstrates off-chain instruction construction. ```bash theme={"system"} cd damm-v1-sdk cargo build -p cli ``` Create a constant-product pool: ```bash theme={"system"} target/debug/cli \ --rpc-url "$RPC_URL" \ --priority-fee "$PRIORITY_FEE" \ --keypair-path "$KEYPAIR_PATH" \ dynamic-amm create-pool \ --token-a-mint "$TOKEN_A_MINT" \ --token-b-mint "$TOKEN_B_MINT" \ --trade-fee-bps "$TRADE_FEE_BPS" \ --token-a-amount "$TOKEN_A_AMOUNT" \ --token-b-amount "$TOKEN_B_AMOUNT" ``` Allowed constant-product trade fee bps in the CLI example are `25`, `100`, `400`, and `600`. ## Commands | Task | Command | | - | - | | Run quote tests | `cd damm-v1-sdk && cargo t -p dynamic-amm-quote test_quote` | | Build CLI | `cd damm-v1-sdk && cargo build -p cli` | | Inspect CLI flags | `cd damm-v1-sdk && cargo run -p cli -- --help` | | Build helper crates | `cd damm-v1-sdk && cargo build -p dynamic-amm-quote -p common` | ## Integration Notes | Area | Note | | - | - | | Account freshness | Quote accuracy depends on current vault token balances, vault LP supply, pool-held vault LP balances, and clock. | | Activation | The quote crate rejects swaps before `pool.bootstrapping.activation_point`. | | Disabled pools | The quote crate rejects swaps when `pool.enabled` is false. | | Depeg pools | Populate `stake_data` for Marinade, Lido, or SPL stake-pool depeg curves. | | Legacy LP mints | `derive_lp_mint_key` has a compatibility map for known pools whose LP mint is not the standard PDA. | | Transactions | The Rust CLI examples are off-chain transaction builders, not program-side CPI wrappers. | # DAMM v1 TS SDK Examples Source: https://docs.meteora.ag/developer-guides/damm-v1/typescript-sdk/examples Explore TypeScript examples for DAMM v1 pool reads, swap quotes, s
프로젝트로 돌아가기