forked from coal-digital/coal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stake.rs
45 lines (38 loc) · 1.54 KB
/
stake.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
use coal_api::{consts::*, instruction::StakeArgs, loaders::*, state::Proof};
use coal_utils::spl::transfer;
use solana_program::{
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
program_error::ProgramError, sysvar::Sysvar,
};
use crate::utils::AccountDeserialize;
/// Stake deposits ORE into a proof account to earn multiplier.
pub fn process_stake<'a, 'info>(accounts: &'a [AccountInfo<'info>], data: &[u8]) -> ProgramResult {
// Parse args.
let args = StakeArgs::try_from_bytes(data)?;
let amount = u64::from_le_bytes(args.amount);
// Load accounts.
let [signer, proof_info, sender_info, treasury_tokens_info, token_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_proof(proof_info, signer.key, true)?;
load_token_account(sender_info, Some(signer.key), &MINT_ADDRESS, true)?;
load_treasury_tokens(treasury_tokens_info, true)?;
load_program(token_program, spl_token::id())?;
// Update the proof 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_add(amount).unwrap();
// Update deposit timestamp.
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
proof.last_stake_at = clock.unix_timestamp;
// Transfer tokens from signer to treasury.
transfer(
signer,
sender_info,
treasury_tokens_info,
token_program,
amount,
)?;
Ok(())
}