-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclaim.rs
86 lines (79 loc) · 2.86 KB
/
claim.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey,
};
use crate::{
error::MarsError,
instruction::ClaimArgs,
loaders::*,
state::{Proof, Treasury},
utils::AccountDeserialize,
MINT_ADDRESS, TREASURY,
};
/// Claim distributes owed token rewards from the treasury to the miner. Its responsibilies include:
/// 1. Transfer tokens from the treasury to the miner.
/// 2. Decrement the miner's claimable rewards counter by an appropriate amount.
/// 3. Update the program's lifetime stats.
///
/// Safety requirements:
/// - Claim is a permissionless instruction and can be called by any miner.
/// - Can only succeed if the claimed amount is less than or equal to the miner's claimable rewards.
/// - The provided beneficiary token account, treasury, treasury token account, and token program must be valid.
pub fn process_claim<'a, 'info>(
_program_id: &Pubkey,
accounts: &'a [AccountInfo<'info>],
data: &[u8],
) -> ProgramResult {
// Parse args
let args = ClaimArgs::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, true)?;
load_token_account(
treasury_tokens_info,
Some(treasury_info.key),
&MINT_ADDRESS,
true,
)?;
load_program(token_program, spl_token::id())?;
// Update claimable amount
let mut proof_data = proof_info.data.borrow_mut();
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
proof.claimable_rewards = proof
.claimable_rewards
.checked_sub(amount)
.ok_or(MarsError::ClaimTooLarge)?;
// Update lifetime status
let mut treasury_data = treasury_info.data.borrow_mut();
let treasury = Treasury::try_from_bytes_mut(&mut treasury_data)?;
treasury.total_claimed_rewards = treasury.total_claimed_rewards.saturating_add(amount);
// Distribute tokens from treasury to beneficiary
let treasury_bump = treasury.bump;
drop(treasury_data);
solana_program::program::invoke_signed(
&spl_token::instruction::transfer(
&spl_token::id(),
treasury_tokens_info.key,
beneficiary_info.key,
treasury_info.key,
&[treasury_info.key],
amount,
)?,
&[
token_program.clone(),
treasury_tokens_info.clone(),
beneficiary_info.clone(),
treasury_info.clone(),
],
&[&[TREASURY, &[treasury_bump as u8]]],
)?;
Ok(())
}