2021-12-02 07:28:12 -08:00
|
|
|
use crate::error::*;
|
2021-12-03 00:52:48 -08:00
|
|
|
use crate::state::*;
|
2021-12-02 07:28:12 -08:00
|
|
|
use anchor_lang::prelude::*;
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct CloseDepositEntry<'info> {
|
2021-12-04 00:02:20 -08:00
|
|
|
// 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)]
|
2021-12-02 07:28:12 -08:00
|
|
|
pub voter: AccountLoader<'info, Voter>,
|
|
|
|
pub voter_authority: Signer<'info>,
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:19:13 -08:00
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// If the deposit entry has `allow_clawback` set, it can only be closed once
|
|
|
|
/// the lockup period has expired.
|
2021-12-03 00:07:54 -08:00
|
|
|
pub fn close_deposit_entry(ctx: Context<CloseDepositEntry>, deposit_entry_index: u8) -> Result<()> {
|
2021-12-02 07:28:12 -08:00
|
|
|
let voter = &mut ctx.accounts.voter.load_mut()?;
|
2021-12-03 03:46:37 -08:00
|
|
|
let d = voter.active_deposit_mut(deposit_entry_index)?;
|
2022-04-25 11:50:02 -07:00
|
|
|
require_eq!(d.amount_deposited_native, 0, VsrError::VotingTokenNonZero);
|
2021-12-02 07:28:12 -08:00
|
|
|
|
|
|
|
// Deposits that have clawback enabled are guaranteed to live until the end
|
|
|
|
// of their locking period. That ensures a deposit can't be closed and reopenend
|
|
|
|
// with a different locking kind or locking end time before funds are deposited.
|
|
|
|
if d.allow_clawback {
|
|
|
|
require!(
|
2021-12-10 03:15:06 -08:00
|
|
|
d.lockup.expired(Clock::get()?.unix_timestamp),
|
2022-04-25 07:23:10 -07:00
|
|
|
VsrError::DepositStillLocked
|
2021-12-02 07:28:12 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:48:10 -08:00
|
|
|
*d = DepositEntry::default();
|
2021-12-02 07:28:12 -08:00
|
|
|
d.is_used = false;
|
2021-12-03 00:48:10 -08:00
|
|
|
|
2021-12-02 07:28:12 -08:00
|
|
|
Ok(())
|
|
|
|
}
|