forked from coal-digital/coal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
claim.rs
47 lines (41 loc) · 1.55 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
use coal_api::{consts::*, error::OreError, instruction::ClaimArgs, loaders::*, state::Proof};
use coal_utils::spl::transfer_signed;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
};
use crate::utils::AccountDeserialize;
/// Claim distributes claimable ORE from the treasury to a miner.
pub fn process_claim<'a, 'info>(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, 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)?;
proof.balance = proof
.balance
.checked_sub(amount)
.ok_or(OreError::ClaimTooLarge)?;
// Transfer tokens from treasury to beneficiary.
transfer_signed(
treasury_info,
treasury_tokens_info,
beneficiary_info,
token_program,
amount,
&[&[TREASURY, &[TREASURY_BUMP]]],
)?;
Ok(())
}