Use anchor's new require_*!() macros
This commit is contained in:
parent
eb46a030d1
commit
233e5414f4
|
@ -72,8 +72,9 @@ pub fn clawback(ctx: Context<Clawback>, deposit_entry_index: u8) -> Result<()> {
|
|||
let locked_amount = deposit_entry.amount_locked(curr_ts);
|
||||
|
||||
// Update deposit book keeping.
|
||||
require!(
|
||||
locked_amount <= deposit_entry.amount_deposited_native,
|
||||
require_gte!(
|
||||
deposit_entry.amount_deposited_native,
|
||||
locked_amount,
|
||||
VsrError::InternalProgramError
|
||||
);
|
||||
deposit_entry.amount_deposited_native -= locked_amount;
|
||||
|
|
|
@ -24,7 +24,7 @@ pub struct CloseDepositEntry<'info> {
|
|||
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!(d.amount_deposited_native == 0, VsrError::VotingTokenNonZero);
|
||||
require_eq!(d.amount_deposited_native, 0, VsrError::VotingTokenNonZero);
|
||||
|
||||
// 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
|
||||
|
|
|
@ -43,16 +43,17 @@ pub fn close_voter<'key, 'accounts, 'remaining, 'info>(
|
|||
let amount = voter.deposits.iter().fold(0u64, |sum, d| {
|
||||
sum.checked_add(d.amount_deposited_native).unwrap()
|
||||
});
|
||||
require!(amount == 0, VsrError::VotingTokenNonZero);
|
||||
require_eq!(amount, 0, VsrError::VotingTokenNonZero);
|
||||
|
||||
let voter_seeds = voter_seeds!(voter);
|
||||
for account in &mut ctx.remaining_accounts.iter() {
|
||||
let token = Account::<TokenAccount>::try_from(&account.clone()).unwrap();
|
||||
require!(
|
||||
token.owner == ctx.accounts.voter.key(),
|
||||
require_keys_eq!(
|
||||
token.owner,
|
||||
ctx.accounts.voter.key(),
|
||||
VsrError::InvalidAuthority
|
||||
);
|
||||
require!(token.amount == 0, VsrError::VaultTokenNonZero);
|
||||
require_eq!(token.amount, 0, VsrError::VaultTokenNonZero);
|
||||
|
||||
let cpi_accounts = CloseAccount {
|
||||
account: account.to_account_info(),
|
||||
|
|
|
@ -78,23 +78,26 @@ pub fn configure_voting_mint(
|
|||
lockup_saturation_secs: u64,
|
||||
grant_authority: Option<Pubkey>,
|
||||
) -> Result<()> {
|
||||
require!(
|
||||
lockup_saturation_secs > 0,
|
||||
require_gt!(
|
||||
lockup_saturation_secs,
|
||||
0,
|
||||
VsrError::LockupSaturationMustBePositive
|
||||
);
|
||||
let registrar = &mut ctx.accounts.registrar.load_mut()?;
|
||||
let mint = ctx.accounts.mint.key();
|
||||
let idx = idx as usize;
|
||||
require!(
|
||||
idx < registrar.voting_mints.len(),
|
||||
require_gt!(
|
||||
registrar.voting_mints.len(),
|
||||
idx,
|
||||
VsrError::OutOfBoundsVotingMintConfigIndex
|
||||
);
|
||||
|
||||
// Either it's reconfiguring an existing mint with the correct index,
|
||||
// or configuring a new mint on an unused index.
|
||||
match registrar.voting_mint_config_index(mint) {
|
||||
Ok(existing_idx) => require!(
|
||||
existing_idx == idx,
|
||||
Ok(existing_idx) => require_eq!(
|
||||
existing_idx,
|
||||
idx,
|
||||
VsrError::VotingMintConfiguredWithDifferentIndex
|
||||
),
|
||||
Err(_) => require!(
|
||||
|
|
|
@ -72,8 +72,9 @@ pub fn create_deposit_entry(
|
|||
let mint_idx = registrar.voting_mint_config_index(ctx.accounts.deposit_mint.key())?;
|
||||
|
||||
// Get and set up the deposit entry.
|
||||
require!(
|
||||
voter.deposits.len() > deposit_entry_index as usize,
|
||||
require_gt!(
|
||||
voter.deposits.len(),
|
||||
deposit_entry_index as usize,
|
||||
VsrError::OutOfBoundsDepositEntryIndex
|
||||
);
|
||||
let d_entry = &mut voter.deposits[deposit_entry_index as usize];
|
||||
|
|
|
@ -62,8 +62,9 @@ pub fn create_registrar(ctx: Context<CreateRegistrar>, registrar_bump: u8) -> Re
|
|||
&ctx.accounts.realm.to_account_info(),
|
||||
®istrar.realm_governing_token_mint,
|
||||
)?;
|
||||
require!(
|
||||
realm.authority.unwrap() == ctx.accounts.realm_authority.key(),
|
||||
require_keys_eq!(
|
||||
realm.authority.unwrap(),
|
||||
ctx.accounts.realm_authority.key(),
|
||||
VsrError::InvalidRealmAuthority
|
||||
);
|
||||
|
||||
|
|
|
@ -60,8 +60,9 @@ pub fn create_voter(
|
|||
let ixns = ctx.accounts.instructions.to_account_info();
|
||||
let current_index = tx_instructions::load_current_index_checked(&ixns)? as usize;
|
||||
let current_ixn = tx_instructions::load_instruction_at_checked(current_index, &ixns)?;
|
||||
require!(
|
||||
current_ixn.program_id == *ctx.program_id,
|
||||
require_keys_eq!(
|
||||
current_ixn.program_id,
|
||||
*ctx.program_id,
|
||||
VsrError::ForbiddenCpi
|
||||
);
|
||||
}
|
||||
|
|
|
@ -73,8 +73,9 @@ pub fn deposit(ctx: Context<Deposit>, deposit_entry_index: u8, amount: u64) -> R
|
|||
|
||||
// Get the exchange rate entry associated with this deposit.
|
||||
let mint_idx = registrar.voting_mint_config_index(ctx.accounts.deposit_token.mint)?;
|
||||
require!(
|
||||
mint_idx == d_entry.voting_mint_config_idx as usize,
|
||||
require_eq!(
|
||||
mint_idx,
|
||||
d_entry.voting_mint_config_idx as usize,
|
||||
VsrError::InvalidMint
|
||||
);
|
||||
|
||||
|
|
|
@ -55,8 +55,9 @@ pub fn internal_transfer_locked(
|
|||
);
|
||||
|
||||
// Reduce source amounts
|
||||
require!(
|
||||
amount <= source.amount_initially_locked_native,
|
||||
require_gte!(
|
||||
source.amount_initially_locked_native,
|
||||
amount,
|
||||
VsrError::InsufficientLockedTokens
|
||||
);
|
||||
source.amount_deposited_native = source.amount_deposited_native.checked_sub(amount).unwrap();
|
||||
|
@ -66,16 +67,19 @@ pub fn internal_transfer_locked(
|
|||
// Check target compatibility
|
||||
let target = voter.active_deposit_mut(target_deposit_entry_index)?;
|
||||
target.resolve_vesting(curr_ts)?;
|
||||
require!(
|
||||
target.voting_mint_config_idx == source_mint_idx,
|
||||
require_eq!(
|
||||
target.voting_mint_config_idx,
|
||||
source_mint_idx,
|
||||
VsrError::InvalidMint
|
||||
);
|
||||
require!(
|
||||
target.lockup.seconds_left(curr_ts) >= source_seconds_left,
|
||||
require_gte!(
|
||||
target.lockup.seconds_left(curr_ts),
|
||||
source_seconds_left,
|
||||
VsrError::InvalidLockupPeriod
|
||||
);
|
||||
require!(
|
||||
target.lockup.kind.strictness() >= source_strictness,
|
||||
require_gte!(
|
||||
target.lockup.kind.strictness(),
|
||||
source_strictness,
|
||||
VsrError::InvalidLockupKind
|
||||
);
|
||||
|
||||
|
|
|
@ -39,16 +39,18 @@ pub fn internal_transfer_unlocked(
|
|||
let source_mint_idx = source.voting_mint_config_idx;
|
||||
|
||||
// Reduce source amounts
|
||||
require!(
|
||||
amount <= source.amount_unlocked(curr_ts),
|
||||
require_gte!(
|
||||
source.amount_unlocked(curr_ts),
|
||||
amount,
|
||||
VsrError::InsufficientUnlockedTokens
|
||||
);
|
||||
source.amount_deposited_native = source.amount_deposited_native.checked_sub(amount).unwrap();
|
||||
|
||||
// Check target compatibility
|
||||
let target = voter.active_deposit_mut(target_deposit_entry_index)?;
|
||||
require!(
|
||||
target.voting_mint_config_idx == source_mint_idx,
|
||||
require_eq!(
|
||||
target.voting_mint_config_idx,
|
||||
source_mint_idx,
|
||||
VsrError::InvalidMint
|
||||
);
|
||||
|
||||
|
|
|
@ -34,13 +34,14 @@ pub fn reset_lockup(
|
|||
let source = voter.active_deposit_mut(deposit_entry_index)?;
|
||||
|
||||
// Must not decrease duration or strictness
|
||||
require!(
|
||||
(periods as u64).checked_mul(kind.period_secs()).unwrap()
|
||||
>= source.lockup.seconds_left(curr_ts),
|
||||
require_gte!(
|
||||
(periods as u64).checked_mul(kind.period_secs()).unwrap(),
|
||||
source.lockup.seconds_left(curr_ts),
|
||||
VsrError::InvalidLockupPeriod
|
||||
);
|
||||
require!(
|
||||
kind.strictness() >= source.lockup.kind.strictness(),
|
||||
require_gte!(
|
||||
kind.strictness(),
|
||||
source.lockup.kind.strictness(),
|
||||
VsrError::InvalidLockupKind
|
||||
);
|
||||
|
||||
|
|
|
@ -15,8 +15,9 @@ pub struct SetTimeOffset<'info> {
|
|||
pub fn set_time_offset(ctx: Context<SetTimeOffset>, time_offset: i64) -> Result<()> {
|
||||
let allowed_program = Pubkey::from_str("GovernanceProgramTest1111111111111111111111").unwrap();
|
||||
let registrar = &mut ctx.accounts.registrar.load_mut()?;
|
||||
require!(
|
||||
registrar.governance_program_id == allowed_program,
|
||||
require_keys_eq!(
|
||||
registrar.governance_program_id,
|
||||
allowed_program,
|
||||
VsrError::DebugInstruction
|
||||
);
|
||||
registrar.time_offset = time_offset;
|
||||
|
|
|
@ -103,18 +103,21 @@ pub fn withdraw(ctx: Context<Withdraw>, deposit_entry_index: u8, amount: u64) ->
|
|||
// Get the deposit being withdrawn from.
|
||||
let curr_ts = registrar.clock_unix_timestamp();
|
||||
let deposit_entry = voter.active_deposit_mut(deposit_entry_index)?;
|
||||
require!(
|
||||
deposit_entry.amount_unlocked(curr_ts) >= amount,
|
||||
require_gte!(
|
||||
deposit_entry.amount_unlocked(curr_ts),
|
||||
amount,
|
||||
VsrError::InsufficientUnlockedTokens
|
||||
);
|
||||
require!(
|
||||
mint_idx == deposit_entry.voting_mint_config_idx as usize,
|
||||
require_eq!(
|
||||
mint_idx,
|
||||
deposit_entry.voting_mint_config_idx as usize,
|
||||
VsrError::InvalidMint
|
||||
);
|
||||
|
||||
// Bookkeeping for withdrawn funds.
|
||||
require!(
|
||||
amount <= deposit_entry.amount_deposited_native,
|
||||
require_gte!(
|
||||
deposit_entry.amount_deposited_native,
|
||||
amount,
|
||||
VsrError::InternalProgramError
|
||||
);
|
||||
deposit_entry.amount_deposited_native = deposit_entry
|
||||
|
|
|
@ -93,8 +93,9 @@ impl DepositEntry {
|
|||
max_locked_vote_weight,
|
||||
voting_mint_config.lockup_saturation_secs,
|
||||
)?;
|
||||
require!(
|
||||
locked_vote_weight <= max_locked_vote_weight,
|
||||
require_gte!(
|
||||
max_locked_vote_weight,
|
||||
locked_vote_weight,
|
||||
VsrError::InternalErrorBadLockupVoteWeight
|
||||
);
|
||||
baseline_vote_weight
|
||||
|
@ -362,8 +363,9 @@ impl DepositEntry {
|
|||
/// periods_total() = 1
|
||||
pub fn resolve_vesting(&mut self, curr_ts: i64) -> Result<()> {
|
||||
let vested_amount = self.vested(curr_ts)?;
|
||||
require!(
|
||||
vested_amount <= self.amount_initially_locked_native,
|
||||
require_gte!(
|
||||
self.amount_initially_locked_native,
|
||||
vested_amount,
|
||||
VsrError::InternalProgramError
|
||||
);
|
||||
self.amount_initially_locked_native = self
|
||||
|
@ -371,7 +373,7 @@ impl DepositEntry {
|
|||
.checked_sub(vested_amount)
|
||||
.unwrap();
|
||||
self.lockup.remove_past_periods(curr_ts)?;
|
||||
require!(self.vested(curr_ts)? == 0, VsrError::InternalProgramError);
|
||||
require_eq!(self.vested(curr_ts)?, 0, VsrError::InternalProgramError);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,11 +68,12 @@ impl Lockup {
|
|||
start_ts: i64,
|
||||
periods: u32,
|
||||
) -> Result<Self> {
|
||||
require!(
|
||||
start_ts < curr_ts + MAX_LOCKUP_IN_FUTURE_SECS,
|
||||
require_gt!(
|
||||
curr_ts + MAX_LOCKUP_IN_FUTURE_SECS,
|
||||
start_ts,
|
||||
VsrError::DepositStartTooFarInFuture
|
||||
);
|
||||
require!(periods <= MAX_LOCKUP_PERIODS, VsrError::InvalidLockupPeriod);
|
||||
require_gte!(MAX_LOCKUP_PERIODS, periods, VsrError::InvalidLockupPeriod);
|
||||
Ok(Self {
|
||||
kind,
|
||||
start_ts,
|
||||
|
@ -138,10 +139,7 @@ impl Lockup {
|
|||
}
|
||||
|
||||
let lockup_secs = self.seconds_left(self.start_ts);
|
||||
require!(
|
||||
lockup_secs % period_secs == 0,
|
||||
VsrError::InvalidLockupPeriod
|
||||
);
|
||||
require_eq!(lockup_secs % period_secs, 0, VsrError::InvalidLockupPeriod);
|
||||
|
||||
Ok(lockup_secs.checked_div(period_secs).unwrap())
|
||||
}
|
||||
|
@ -154,9 +152,10 @@ impl Lockup {
|
|||
.start_ts
|
||||
.checked_add(i64::try_from(periods.checked_mul(period_secs).unwrap()).unwrap())
|
||||
.unwrap();
|
||||
require!(self.start_ts <= self.end_ts, VsrError::InternalProgramError);
|
||||
require!(
|
||||
self.period_current(curr_ts)? == 0,
|
||||
require_gte!(self.end_ts, self.start_ts, VsrError::InternalProgramError);
|
||||
require_eq!(
|
||||
self.period_current(curr_ts)?,
|
||||
0,
|
||||
VsrError::InternalProgramError
|
||||
);
|
||||
Ok(())
|
||||
|
|
|
@ -53,7 +53,7 @@ impl Voter {
|
|||
curr_ts: i64,
|
||||
at_ts: i64,
|
||||
) -> Result<u64> {
|
||||
require!(at_ts >= curr_ts, VsrError::InvalidTimestampArguments);
|
||||
require_gte!(at_ts, curr_ts, VsrError::InvalidTimestampArguments);
|
||||
self.deposits
|
||||
.iter()
|
||||
.filter(|d| d.is_used)
|
||||
|
@ -73,8 +73,9 @@ impl Voter {
|
|||
|
||||
pub fn active_deposit_mut(&mut self, index: u8) -> Result<&mut DepositEntry> {
|
||||
let index = index as usize;
|
||||
require!(
|
||||
index < self.deposits.len(),
|
||||
require_gt!(
|
||||
self.deposits.len(),
|
||||
index,
|
||||
VsrError::OutOfBoundsDepositEntryIndex
|
||||
);
|
||||
let d = &mut self.deposits[index];
|
||||
|
@ -93,8 +94,9 @@ impl Voter {
|
|||
®istrar.realm,
|
||||
®istrar.realm_governing_token_mint,
|
||||
)?;
|
||||
require!(
|
||||
record.governing_token_owner == self.voter_authority,
|
||||
require_keys_eq!(
|
||||
record.governing_token_owner,
|
||||
self.voter_authority,
|
||||
VsrError::InvalidTokenOwnerRecord
|
||||
);
|
||||
Ok(record)
|
||||
|
|
Loading…
Reference in New Issue