bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 59쪽
UserToTopHolder`, `RemoveUserFromTopHolder`, `ReclaimIndex` |
# Stake2Earn Program Instructions
Source: https://docs.meteora.ag/developer-guides/stake2earn/program/instructions
Explore Stake2Earn stake_for_fee instructions for vault creation, stake escrow creation, staking, fee claiming, unstaking, withdrawal, cranking, and admin updates.
<Note>
Stake2Earn transactions that touch fees must pass fresh DAMM v1 pool, lock escrow, Dynamic Vault, LP mint, and token vault accounts. The TypeScript SDK builds these account bundles automatically.
</Note>
## Instruction Index
| Instruction | Access | Arguments | Use |
| - | - | - | - |
| `initialize_vault` | Permissionless public build | `InitializeVaultParams` | Create a vault for a DAMM v1 constant-product pool and initialize list accounts. |
| `initialize_stake_escrow` | Permissionless | None | Create a user's stake escrow and grow the list accounts when needed. |
| `stake` | Stake escrow owner | `amount: u64` | Claim and distribute lock escrow fees, transfer stake tokens, and sync top/full lists. |
| `claim_fee` | Stake escrow owner | `max_fee: u64` | Claim quote token fees to the user and automatically restake base/stake token fees. |
| `request_unstake` | Stake escrow owner | `unstake_amount: u64` | Reserve stake for withdrawal and create an `Unstake` account with a release time. |
| `cancel_unstake` | Stake escrow owner | None | Close an `Unstake` account and return the amount to active stake. |
| `withdraw` | Stake escrow owner | None | Transfer released unstaked tokens back to the user and close the `Unstake` account. |
| `claim_fee_crank` | Permissionless | None | Claim fees from the DAMM v1 lock escrow and update fee drip state without a user claim. |
| `update_unstake_lock_duration` | Admin signer | `unstake_lock_duration: u64` | Update the cooldown for future unstake requests. |
| `update_seconds_to_full_unlock` | Admin signer | `seconds_to_full_unlock: u64` | Update fee drip duration after a preceding `claim_fee_crank` instruction. |
| `_dummy` | Internal | None | IDL-generation helper only. Do not call in integrations. |
All public user/admin instructions in the source use Anchor Event CPI, so generated clients include `event_authority` and `program` accounts.
## `initialize_vault`
Creates the root vault and list accounts for a DAMM v1 pool.
| Requirement | Details |
| - | - |
| Vault PDA | `["vault", pool]`. There can be one Stake2Earn vault per DAMM v1 pool. |
| Pool type | DAMM v1 constant product only. |
| Mints | `stake_mint` and `quote_mint` must both belong to the pool. `quote_mint` must be SOL or USDC; `stake_mint` must not be SOL or USDC. |
| Lock escrow | DAMM v1 lock escrow must have `owner == vault` and `pool == pool`. |
| List length | Public builds allow `top_list_length` from `5` to `1000`. |
| Drip duration | Public builds allow `seconds_to_full_unlock` from `6 hours` to `31 days`. |
| Unstake lock | Public builds allow `unstake_lock_duration` from `6 hours` to `31 days`. |
| Start timestamp | Optional. If supplied, it must be no more than `31 days` after the current on-chain timestamp. |
| Account | Role |
| - | - |
| `vault` | Initialized `FeeVault` PDA. |
| `stake_token_vault` | ATA for `stake_mint`, owned by the vault PDA. |
| `quote_token_vault` | ATA for `quote_mint`, owned by the vault PDA. |
| `top_staker_list` | Initialized `TopListMetadata` PDA. |
| `full_balance_list` | Initialized `FullBalanceListMetadata` PDA. |
| `pool` | DAMM v1 pool account. |
| `stake_mint`, `quote_mint` | Pool mints. |
| `lock_escrow` | DAMM v1 lock escrow owned by the vault PDA. |
| `payer` | Signer paying account rent. |
| `system_program`, `token_program`, `associated_token_program` | Required Solana programs. |
Emits `VaultCreated`.
## `initialize_stake_escrow`
Creates a user stake escrow for one vault.
| Account | Role |
| - | - |
| `vault` | Parent `FeeVault`. |
| `escrow` | Initialized PDA `["escrow", vault, owner]`. |
| `full_balance_list` | Reallocated to append the user if under the hard limit. |
| `top_staker_list` | Reallocated while `current_length < top_list_length`. |
| `owner` | Wallet that owns the stake escrow. |
| `payer` | Signer paying rent and realloc costs. |
| `system_program` | Required for account creation and realloc. |
The handler increments `total_stake_escrow_count`, initializes an empty stake escrow, and appends a full-balance entry when capacity remains. Emits `StakeEscrowCreated`.
## Fee Claim Account Bundle
These accounts are required by `stake`, `claim_fee`, `request_unstake`, `cancel_unstake`, and `claim_fee_crank` because each flow first claims and distributes fees from the DAMM v1 lock escrow.
| Account | Requirement |
| - | - |
| `pool` | DAMM v1 pool; must reference the supplied vaults, LP accounts, and LP mint. |
| `lp_mint` | DAMM v1 LP mint. |
| `lock_escrow` | Lock escrow stored on the `FeeVault`. |
| `escrow_vault` | Token account for locked LP; validated by the DAMM v1 program. |
| `a_token_vault`, `b_token_vault` | Token vault accounts for Dynamic Vault reserves. |
| `a_vault`, `b_vault` | Dynamic Vault accounts from the DAMM v1 pool. |
| `a_vault_lp`, `b_vault_lp` | DAMM v1 pool LP token accounts for each Dynamic Vault. |
| `a_vault_lp_mint`, `b_vault_lp_mint` | LP mints for the Dynamic Vault accounts. |
| `amm_program` | DAMM v1 program. |
| `vault_program` | Dynamic Vault program. |
| `token_program` | SPL Token program. |
## `stake`
Transfers stake tokens from the user into the vault and syncs list state.
| Behavior | Details |
| - | - |
| Amount | The handler stakes `min(amount, user_stake_token.amount)`. |
| Auto-create helper | The TypeScript SDK includes `initialize_stake_escrow` as a pre-instruction when the stake escrow does not exist. |
| Top list sync | Provide replaceable top staker accounts in `remaining_accounts` when the new stake can affect rankings. |
| Full list reclaim | Provide `smallest_stake_escrow` when reclaiming a full-balance index from the smallest tracked staker. |
| Events | Emits `FeeEmission`, optional list movement events, and `UserStake`. |
## `claim_fee`
Claims fees from a stake escrow.
| Behavior | Details |
| - | - |
| Fee update | Claims from the DAMM v1 lock escrow, releases dripped fees, updates the caller's stake escrow, then claims user fees. |
| Quote transfer | Only quote token fees are transferred to `user_quote_token`. |
| Base restake | Base/stake token fees are kept in the vault and added to the user's active `stake_amount`. |
| `max_fee` | Caps the quote token amount the user receives. The stake/base side is fully restaked. |
| SOL handling | The TypeScript SDK adds a post-instruction to close the user's wrapped SOL ATA after claiming. |
| Events | Emits `FeeEmission`, `ClaimFeeSucceed`, optional list movement events, and `UserStake`. |
## `request_unstake`
Creates a pending unstake request.
| Account or argument | Details |
| - | - |
| `unstake` | New arbitrary account keypair initialized by the instruction. |
| `unstake_amount` | Must be less than or equal to the active `stake_amount`. |
| Release time | `Clock::unix_timestamp + vault.configuration.unstake_lock_duration`. |
| List sync | Removes stake from active balances and updates top/full lists. |
| Events | Emits `FeeEmission`, optional top list movement events, and `UnstakeCreated`. |
## `cancel_unstake`
Closes an open `Unstake` account and returns the amount to active stake.
| Behavior | Details |
| - | - |
| Fee update | Claims and distributes pending lock escrow fees before mutating stake. |
| Stake update | Increases `stake_amount` and decreases ongoing unstake counters. |
| List sync | Re-ranks the user and replacement candidates. |
| Events | Emits `FeeEmission`, optional list movement events, and `CancelUnstakeSucceed`. |
## `withdraw`
Transfers stake tokens from the vault back to the owner after the release time.
| Requirement | Details |
| - | - |
| Release check | Fails until `Clock::unix_timestamp >= unstake.release_at`. |
| Transfer authority | The vault PDA signs for `stake_token_vault` with `["vault", pool, bump]`. |
| Close behavior | Closes the `Unstake` account to the owner. |
| Events | Emits `WithdrawSucceed`. |
## `claim_fee_crank`
Permissionlessly claims fees from the DAMM v1 lock escrow and updates drip accounting without a user claim.
| Behavior | Details |
| - | - |
| Minimum interval | Public builds require at least `5 minutes` between lock escrow fee claims. |
| User account | No stake escrow is required. |
| Events | Emits `FeeEmission` when fees are claimed/released. |
| Admin dependency | `update_seconds_to_full_unlock` requires the immediately preceding instruction to be `claim_fee_crank` for the same vault. |
## Admin Updates
| Instruction | Constraints | Event |
| - | - | - |
| `update_unstake_lock_duration` | Admin signer; value from `6 hours` to `31 days`; must differ from current value. | `UpdateUnstakeLockDuration` |
| `update_seconds_to_full_unlock` | Admin signer; value from `6 hours` to `31 days`; immediately preceding instruction must be `claim_fee_crank` for the same vault. | `UpdateSecondsToFullUnlock` |
Admin public keys are hardcoded in the program source for public builds.
## Gotchas
| Gotcha | Integration impact |
| - | - |
| Event CPI accounts | Include `event_authority` and `program` when using low-level clients. |
| DAMM v1 only | DAMM v2 and DLMM pools are not accepted by `initialize_vault`. |
| Quote mint restriction | Only SOL and USDC quote mints pass validation on public builds. |
| Remaining accounts | Top list replacements require candidate stake escrow accounts in `remaining_accounts`. |
| Open unstake keypair | `request_unstake` initializes a caller-provided account; it must sign the transaction. |
| Fee crank ordering | Admin `update_seconds_to_full_unlock` must be placed immediately after `claim_fee_crank` in the same transaction. |
# Stake2Earn Rust CPI Integration
Source: https://docs.meteora.ag/developer-guides/stake2earn/rust-integration/cpi
Learn how to integrate with Stake2Earn through Rust CPI, including required accounts, Event CPI accounts, generated invoke helpers, signer seeds, and instruction-specific constraints.
Rust CPI integrations should prefer the generated `stake_for_fee_interface` invoke helpers over hand-built discriminators. The helpers expose both `*_invoke` and `*_invoke_signed` variants for every instruction.
<Warning>
Stake2Earn instructions use Anchor Event CPI. CPI callers must include the generated `event_authority` account and the Stake2Earn program account where the generated key structs require them.
</Warning>
## CPI Helper Families
| Flow | Generated helper |
| - | - |
| Initialize vault | `initialize_vault_invoke` / `initialize_vault_invoke_signed` |
| Initialize stake escrow | `initialize_stake_escrow_invoke` / `initialize_stake_escrow_invoke_signed` |
| Stake | `stake_invoke` / `stake_invoke_signed` |
| Claim fee | `claim_fee_invoke` / `claim_fee_invoke_signed` |
| Request unstake | `request_unstake_invoke` / `request_unstake_invoke_signed` |
| Cancel unstake | `cancel_unstake_invoke` / `cancel_unstake_invoke_signed` |
| Withdraw | `withdraw_invoke` / `withdraw_invoke_signed` |
| Claim fee crank | `claim_fee_crank_invoke` / `claim_fee_crank_invoke_signed` |
| Admin updates | `update_unstake_lock_duration_invoke`, `update_seconds_to_full_unlock_invoke` |
## Initialize Vault CPI
`initialize_vault` creates the Stake2Earn vault and two list accounts.
| Account | Requirement |
| - | - |
| `vault` | PDA `["vault", pool]`, initialized by the instruction. |
| `stake_token_vault` | ATA for `stake_mint`, authority `vault`. |
| `quote_token_vault` | ATA for `quote_mint`, authority `vault`. |
| `top_staker_list` | PDA `["list", vault]`, initialized by the instruction. |
| `full_balance_list` | PDA `["balance", vault]`, initialized by the instruction. |
| `pool` | DAMM v1 constant-product pool containing the stake and quote mints. |
| `stake_mint`, `quote_mint` | Pool mints; quote must be SOL or USDC. |
| `lock_escrow` | DAMM v1 lock escrow owned by `vault`. |
| `payer` | Rent payer signer. |
| `event_authority`, `program` | Anchor Event CPI accounts. |
| Parameter | Constraint |
| - | - |
| `top_list_length` | Public range `5` to `1000`. |
| `seconds_to_full_unlock` | Public range `6 hours` to `31 days`. |
| `unstake_lock_duration` | Public range `6 hours` to `31 days`. |
| `start_fee_distribute_timestamp` | Optional; max join window is `31 days`. |
| `padding` | `[0; 64]` in current clients. |
## Stake And Claim CPI
`stake` and `claim_fee` are not isolated stake-account mutations. They first claim and distribute pending DAMM v1 lock escrow fees, so callers must include the full DAMM v1 and Dynamic Vault account bundle.
| Account group | Accounts |
| - | - |
| Stake2Earn state | `vault`, `stake_token_vault`, `quote_token_vault`, `top_staker_list`, `full_balance_list`, `stake_escrow`, optional `smallest_stake_escrow` |
| User accounts | `owner`, `user_stake_token` for `stake`, `user_quote_token` for `claim_fee` |
| DAMM v1 lock escrow | `pool`, `lp_mint`, `lock_escrow`, `escrow_vault` |
| Dynamic Vault accounts | `a_token_vault`, `b_token_vault`, `a_vault`, `b_vault`, `a_vault_lp`, `b_vault_lp`, `a_vault_lp_mint`, `b_vault_lp_mint` |
| Programs | DAMM v1 program, Dynamic Vault program, SPL Token program, Event CPI accounts |
Top-list synchronization may require additional stake escrow accounts in `remaining_accounts`. If the full balance list index must be reclaimed, pass the expected `smallest_stake_escrow`.
## Unstake CPI
| Instruction | CPI notes |
| - | - |
| `request_unstake` | Initializes a caller-provided `Unstake` account, claims/distributes lock escrow fees, decreases active stake, updates list state, and sets `release_at`. |
| `cancel_unstake` | Closes `Unstake` to the owner, claims/distributes fees, returns the amount to active stake, and syncs list state. |
| `withdraw` | Does not need the DAMM v1 fee-claim bundle. It checks `release_at`, transfers stake tokens from `stake_token_vault` to `user_stake_token`, updates ongoing unstake counters, and closes `Unstake`. |
## Vault PDA Signer Seeds
When the Stake2Earn program signs for its token vaults internally, it uses:
| PDA | Seeds |
| - | - |
| `FeeVault` signer | `["vault", pool, bump]` |
External CPI callers do not sign as the Stake2Earn vault PDA unless they are invoking another program directly. If your program composes DAMM v1 lock escrow setup before calling Stake2Earn, ensure the lock escrow owner is the Stake2Earn vault PDA.
## Crank And Admin CPI
| Instruction | Notes |
| - | - |
| `claim_fee_crank` | Permissionless. Requires the same DAMM v1 and Dynamic Vault fee-claim bundle but no user stake escrow. |
| `update_unstake_lock_duration` | Admin signer only. New value affects future unstake requests. |
| `update_seconds_to_full_unlock` | Admin signer only. The immediately preceding instruction in the same transaction must be `claim_fee_crank` for the same vault and program. |
## CPI Gotchas
| Gotcha | Resolution |
| - | - |
| Missing Event CPI accounts | Derive `["__event_authority"]` and include the `stake_for_fee` program account when the generated key struct requires them. |
| Stale DAMM v1 or Dynamic Vault accounts | Re-read account state before building fee-claim flows. |
| Wrong quote mint | Only SOL and USDC quote mints are accepted on public builds. |
| Missing replacement stake escrows | Supply remaining accounts for top-list promotion/removal when stake changes can affect rankings. |
| Withdraw before release | Read the `Unstake` account and compare `release_at` with the current clock before sending. |
# Stake2Earn Rust Integration Library
Source: https://docs.meteora.ag/developer-guides/stake2earn/rust-integration/library
Understand the Stake2Earn Rust integration surface, including the generated stake_for_fee_interface crate, common PDA helpers, account decoders, instruction builders, events, and errors.
The local Rust integration source contains two crates:
| Crate | Path | Use |
| - | - | - |
| `stake_for_fee_interface` | `stake-for-fee-sdk/stake_for_fee_interface` | Generated Rust interface for accounts, instructions, typedefs, events, and errors. |
| `common` | `stake-for-fee-sdk/common` | Small PDA helper crate that depends on `stake_for_fee_interface` as `m3m3`. |
<Note>
The inspected source does not include a high-level ergonomic Rust SDK like the TypeScript `StakeForFee` class. Rust integrations should use the generated interface crate, PDA helpers, and fresh on-chain account reads.
</Note>
## Workspace
```toml theme={"system"}
[workspace]
members = ["stake_for_fee_interface", "common"]
resolver = "2"
```
For local development against the source checkout:
```toml theme={"system"}
[dependencies]
stake_for_fee_interface = { path = "../stake-for-fee-sdk/stake_for_fee_interface" }
common = { path = "../stake-for-fee-sdk/common" }
```
## Program ID
`stake_for_fee_interface` declares the public program ID:
| Program | ID |
| - | - |
| `stake_for_fee` | `FEESngU3neckdwib9X3KWqdL7Mjmqk9XNp3uh5JbP4KP` |
## PDA Helpers
The `common::pda` module exports:
| Function | Derives |
| - | - |
| `derive_m3m3_vault_key(pool_key)` | `["vault", pool]` under `stake_for_fee_interface::ID`. |
| `derive_top_staker_list_key(vault_key)` | `["list", vault]`. |
| `derive_full_balance_list_key(vault_key)` | `["balance", vault]`. |
| `derive_m3m3_event_authority_key()` | Anchor Event CPI authority `["__event_authority"]`. |
```rust theme={"system"}
use common::pda::{
derive_full_balance_list_key, derive_m3m3_event_authority_key,
derive_m3m3_vault_key, derive_top_staker_list_key,
};
use solana_sdk::pubkey::Pubkey;
let pool = Pubkey::new_unique();
let vault = derive_m3m3_vault_key(pool);
let top_list = derive_top_staker_list_key(vault);
let full_balance_list = derive_full_balance_list_key(vault);
let event_authority = derive_m3m3_event_authority_key();
```
## Generated Modules
| Module | Exports |
| - | - |
| `accounts` | Account structs, discriminators, and `deserialize` helpers for `FeeVault`, `StakeEscrow`, `Unstake`, list metadata, and dummy account wrappers used for generated types. |
| `typedefs` | `InitializeVaultParams`, `StakerBalance`, `StakerMetadata`, `Configuration`, `Metrics`, `TopStakerInfo`, and `Rounding`. |
| `instructions` | Instruction enum, discriminators, key structs, argument structs, `*_ix`, `*_invoke`, and `*_invoke_signed` helpers. |
| `events` | Event structs matching the Anchor event definitions. |
| `errors` | `StakeForFeeError` enum with custom codes `6000` through `6022`. |
## Instruction Builders
| Helper | Args | Use |
| - | - | - |
| `initialize_vault_ix(keys, args)` | `InitializeVaultIxArgs { params }` | Create the root vault and list accounts. |
| `initialize_stake_escrow_ix(keys)` | None | Create a user stake escrow. |
| `stake_ix(keys, args)` | `StakeIxArgs { amount }` | Stake tokens and sync lists. |
| `claim_fee_ix(keys, args)` | `ClaimFeeIxArgs { max_fee }` | Claim quote fees and restake base fees. |
| `request_unstake_ix(keys, args)` | `RequestUnstakeIxArgs { unstake_amount }` | Create a pending unstake request. |
| `cancel_unstake_ix(keys)` | None | Cancel an open unstake request. |
| `withdraw_ix(keys)` | None | Withdraw released stake. |
| `claim_fee_crank_ix(keys)` | None | Permissionlessly claim and distribute lock escrow fees. |
| `update_unstake_lock_duration_ix(keys, args)` | `UpdateUnstakeLockDurationIxArgs { unstake_lock_duration }` | Admin update for future unstake cooldowns. |
| `update_seconds_to_full_unlock_ix(keys, args)` | `UpdateSecondsToFullUnlockIxArgs { seconds_to_full_unlock }` | Admin update for fee drip duration after a crank. |
Each instruction also has `*_ix_with_program_id`, `*_invoke`, `*_invoke_with_program_id`, `*_invoke_signed`, and verification helpers in the generated source.
## Account Decoding
| Account프로젝트로 돌아가기