-
Notifications
You must be signed in to change notification settings - Fork 0
/
close_deposit_entry.rs
36 lines (32 loc) · 1.11 KB
/
close_deposit_entry.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
use anchor_lang::prelude::*;
use mplx_staking_states::{
error::MplStakingError,
state::{DepositEntry, Voter},
};
#[derive(Accounts)]
pub struct CloseDepositEntry<'info> {
// checking the PDA address it just an extra precaution,
// the other constraints must be exhaustive
#[account(
mut,
seeds = [voter.load()?.registrar.key().as_ref(), b"voter".as_ref(), voter_authority.key().as_ref()],
bump = voter.load()?.voter_bump,
has_one = voter_authority)]
pub voter: AccountLoader<'info, Voter>,
pub voter_authority: Signer<'info>,
}
/// Close an empty deposit entry, allowing it to be reused in the future.
///
/// Deposit entries can only be closed when they don't hold any tokens.
pub fn close_deposit_entry(ctx: Context<CloseDepositEntry>, deposit_entry_index: u8) -> Result<()> {
let voter = &mut ctx.accounts.voter.load_mut()?;
let d = voter.active_deposit_mut(deposit_entry_index)?;
require_eq!(
d.amount_deposited_native,
0,
MplStakingError::VotingTokenNonZero
);
*d = DepositEntry::default();
d.is_used = false;
Ok(())
}