Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stake): enhance documentation and code quality in stake CPI module #3418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions spl/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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<()> {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down