From 1f0b46489c25bb6d6a602e24f41af35961132b7f Mon Sep 17 00:00:00 2001 From: techvoyagerX Date: Mon, 9 Dec 2024 00:46:49 -0500 Subject: [PATCH] feat(stake): enhance documentation and code quality in stake CPI module --- spl/src/stake.rs | 57 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/spl/src/stake.rs b/spl/src/stake.rs index c776974da7..5cea502aeb 100644 --- a/spl/src/stake.rs +++ b/spl/src/stake.rs @@ -14,8 +14,14 @@ use anchor_lang::{ use borsh::BorshDeserialize; use std::ops::Deref; -// CPI functions - +// CPI Functions + +/// Authorizes a new authority for a stake account. +/// +/// # Parameters +/// - `ctx`: Context containing accounts required for the operation. +/// - `stake_authorize`: The type of authorization (Staker or Withdrawer). +/// - `custodian`: Optional custodian account. pub fn authorize<'info>( ctx: CpiContext<'_, '_, '_, 'info, Authorize<'info>>, stake_authorize: StakeAuthorize, @@ -37,9 +43,15 @@ pub fn authorize<'info>( account_infos.push(c); } anchor_lang::solana_program::program::invoke_signed(&ix, &account_infos, ctx.signer_seeds) - .map_err(Into::into) + .map_err(|e| anchor_lang::error!(format!("Authorization failed: {:?}", e))) } +/// Withdraws lamports from a stake account. +/// +/// # Parameters +/// - `ctx`: Context containing accounts required for the operation. +/// - `amount`: The amount to withdraw in lamports. +/// - `custodian`: Optional custodian account. pub fn withdraw<'info>( ctx: CpiContext<'_, '_, '_, 'info, Withdraw<'info>>, amount: u64, @@ -63,9 +75,13 @@ pub fn withdraw<'info>( account_infos.push(c); } anchor_lang::solana_program::program::invoke_signed(&ix, &account_infos, ctx.signer_seeds) - .map_err(Into::into) + .map_err(|e| anchor_lang::error!(format!("Withdraw failed: {:?}", e))) } +/// Deactivates a stake account. +/// +/// # Parameters +/// - `ctx`: Context containing accounts required for the operation. pub fn deactivate_stake<'info>( ctx: CpiContext<'_, '_, '_, 'info, DeactivateStake<'info>>, ) -> Result<()> { @@ -75,59 +91,63 @@ pub fn deactivate_stake<'info>( &[ctx.accounts.stake, ctx.accounts.clock, ctx.accounts.staker], ctx.signer_seeds, ) - .map_err(Into::into) + .map_err(|e| anchor_lang::error!(format!("Deactivation failed: {:?}", e))) } -// CPI accounts +// CPI Accounts #[derive(Accounts)] +/// Accounts required for the `authorize` CPI function. pub struct Authorize<'info> { - /// The stake account to be updated + /// The stake account to be updated. pub stake: AccountInfo<'info>, - /// The existing authority + /// The existing authority. pub authorized: AccountInfo<'info>, - /// The new authority to replace the existing authority + /// The new authority to replace the existing authority. pub new_authorized: AccountInfo<'info>, - /// Clock sysvar + /// Clock sysvar. pub clock: AccountInfo<'info>, } #[derive(Accounts)] +/// Accounts required for the `withdraw` CPI function. pub struct Withdraw<'info> { - /// The stake account to be updated + /// The stake account to be updated. pub stake: AccountInfo<'info>, - /// The stake account's withdraw authority + /// The stake account's withdraw authority. pub withdrawer: AccountInfo<'info>, - /// Account to send withdrawn lamports to + /// Account to send withdrawn lamports to. pub to: AccountInfo<'info>, - /// Clock sysvar + /// Clock sysvar. pub clock: AccountInfo<'info>, - /// StakeHistory sysvar + /// StakeHistory sysvar. pub stake_history: AccountInfo<'info>, } #[derive(Accounts)] +/// Accounts required for the `deactivate_stake` CPI function. pub struct DeactivateStake<'info> { - /// The stake account to be deactivated + /// The stake account to be deactivated. pub stake: AccountInfo<'info>, - /// The stake account's stake authority + /// The stake account's stake authority. pub staker: AccountInfo<'info>, - /// Clock sysvar + /// Clock sysvar. pub clock: AccountInfo<'info>, } // State #[derive(Clone)] +/// A wrapper around the Solana StakeState to enable Anchor deserialization. pub struct StakeAccount(StakeState); impl anchor_lang::AccountDeserialize for StakeAccount { @@ -157,6 +177,7 @@ impl Deref for StakeAccount { } #[derive(Clone)] +/// An Anchor ID wrapper for the Solana Stake program. pub struct Stake; impl anchor_lang::Id for Stake {