-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlock_tokens.rs
62 lines (52 loc) · 2.03 KB
/
unlock_tokens.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
use crate::{clock_unix_timestamp, cpi_instructions::withdraw_mining, Stake};
use anchor_lang::prelude::*;
use mplx_staking_states::{error::MplStakingError, state::COOLDOWN_SECS};
pub fn unlock_tokens(ctx: Context<Stake>, deposit_entry_index: u8) -> Result<()> {
let registrar = ctx.accounts.registrar.load()?;
require!(
registrar.reward_pool == ctx.accounts.reward_pool.key(),
MplStakingError::InvalidRewardPool
);
let voter = &mut ctx.accounts.voter.load_mut()?;
let curr_ts = clock_unix_timestamp();
let deposit_entry = voter.active_deposit_mut(deposit_entry_index)?;
// Check whether unlock request is allowed
require!(
!deposit_entry.lockup.cooldown_requested,
MplStakingError::UnlockAlreadyRequested
);
require!(
curr_ts >= deposit_entry.lockup.end_ts,
MplStakingError::DepositStillLocked
);
ctx.accounts.verify_delegate(deposit_entry)?;
deposit_entry.lockup.cooldown_requested = true;
deposit_entry.lockup.cooldown_ends_at = curr_ts
.checked_add(COOLDOWN_SECS)
.ok_or(MplStakingError::InvalidTimestampArguments)?;
let rewards_program = ctx.accounts.rewards_program.to_account_info();
let reward_pool = ctx.accounts.reward_pool.to_account_info();
let mining = ctx.accounts.deposit_mining.to_account_info();
let delegate_mining = ctx.accounts.delegate_mining.to_account_info();
let owner = ctx.accounts.voter_authority.to_account_info();
let deposit_authority = ctx.accounts.registrar.to_account_info();
let signers_seeds = &[
registrar.realm.as_ref(),
b"registrar".as_ref(),
registrar.realm_governing_token_mint.as_ref(),
&[registrar.bump][..],
];
let delegate_wallet_addr = &ctx.accounts.delegate.key();
withdraw_mining(
rewards_program,
reward_pool,
mining,
deposit_authority,
delegate_mining,
deposit_entry.amount_deposited_native,
owner.key,
signers_seeds,
delegate_wallet_addr,
)?;
Ok(())
}