-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithdraw.rs
More file actions
82 lines (74 loc) · 2.63 KB
/
withdraw.rs
File metadata and controls
82 lines (74 loc) · 2.63 KB
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
use ore_stake_api::prelude::*;
use steel::*;
/// Withdraws ORE from the staking contract.
pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
// Parse data.
let args = Withdraw::try_from_bytes(data)?;
let amount = u64::from_le_bytes(args.amount);
if amount == 0 {
return Err(OreStakeError::AmountZero.into());
}
// Load accounts.
let clock = Clock::get()?;
let [signer_info, mint_info, recipient_info, stake_info, stake_tokens_info, treasury_info, system_program, token_program, associated_token_program, ore_stake_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
mint_info.has_address(&MINT_ADDRESS)?.as_mint()?;
recipient_info.is_writable()?;
let stake = stake_info
.as_account_mut::<Stake>(&ore_stake_api::ID)?
.assert_mut(|s| s.authority == *signer_info.key)?;
// .assert_mut(|s| s.last_deposit_at + ONE_DAY < clock.unix_timestamp)?;
stake_tokens_info.as_associated_token_account(stake_info.key, mint_info.key)?;
let treasury = treasury_info.as_account_mut::<Treasury>(&ore_stake_api::ID)?;
system_program.is_program(&system_program::ID)?;
token_program.is_program(&spl_token::ID)?;
associated_token_program.is_program(&spl_associated_token_account::ID)?;
ore_stake_program.is_program(&ore_stake_api::ID)?;
// Open recipient token account.
if recipient_info.data_is_empty() {
create_associated_token_account(
signer_info,
signer_info,
recipient_info,
mint_info,
system_program,
token_program,
associated_token_program,
)?;
} else {
recipient_info.as_associated_token_account(&signer_info.key, &mint_info.key)?;
}
// Withdraw from stake account.
let amount = stake.withdraw(amount, &clock, treasury);
// Transfer ORE to recipient.
transfer_signed(
stake_info,
stake_tokens_info,
recipient_info,
token_program,
amount,
&[STAKE, &stake.authority.to_bytes()],
)?;
// Safety check.
let stake_tokens =
stake_tokens_info.as_associated_token_account(stake_info.key, mint_info.key)?;
if stake_tokens.amount() < stake.balance {
return Err(OreStakeError::InsufficientBalance.into());
}
// Log event.
program_log(
&[treasury_info.clone()],
WithdrawEvent {
disc: 3,
authority: *signer_info.key,
amount,
ts: clock.unix_timestamp,
}
.to_bytes(),
)?;
Ok(())
}