marsh-program 2.1.2

Marsh is a cryptocurrency for sovereign individuals living in Mirascape Horizon.
Documentation
use marsh_api::{consts::*, error::MarshError, instruction::*, loaders::*, state::Proof};
use marsh_utils::*;
use solana_program::{
    account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
};

/// Claim distributes claimable Marsh from the treasury to a miner.
pub fn process_claim(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
    // Parse args.
    let args = Claim::try_from_bytes(data)?;
    let amount = u64::from_le_bytes(args.amount);

    // Load accounts.
    let [signer, beneficiary_info, proof_info, treasury_info, treasury_tokens_info, token_program] =
        accounts
    else {
        return Err(ProgramError::NotEnoughAccountKeys);
    };
    load_signer(signer)?;
    load_token_account(beneficiary_info, None, &MINT_ADDRESS, true)?;
    load_proof(proof_info, signer.key, true)?;
    load_treasury(treasury_info, false)?;
    load_treasury_tokens(treasury_tokens_info, true)?;
    load_program(token_program, spl_token::id())?;

    // Update miner balance.
    let mut proof_data = proof_info.data.borrow_mut();
    let proof = Proof::try_from_bytes_mut(&mut proof_data)?;

    // MI
    // Subtract balance after successful transfer since we can't actually handle the error from invoke or invoke_signed.
    // If a CPI error occurred, the whole transaction aborts immediately which may cause potential state inconsistency
    if proof.balance < amount {
        return Err(MarshError::ClaimTooLarge.into());
    }

    // Transfer tokens from treasury to beneficiary.
    transfer_signed(
        treasury_info,
        treasury_tokens_info,
        beneficiary_info,
        token_program,
        amount,
        &[&[TREASURY, &[TREASURY_BUMP]]],
    )?;

    proof.balance = proof
        .balance
        .checked_sub(amount)
        .ok_or(MarshError::ClaimTooLarge)?;

    Ok(())
}