Skip to content

Commit

Permalink
rename the package && fix bug with change_delegate cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
kstepanovdev committed Aug 2, 2024
1 parent 26f19bb commit 209a320
Show file tree
Hide file tree
Showing 32 changed files with 216 additions and 221 deletions.
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion program-states/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anchor_lang::prelude::*;

#[error_code]
pub enum VsrError {
pub enum MplStakingError {
// 6000 / 0x1770
#[msg("")]
VotingMintNotFound,
Expand Down
20 changes: 14 additions & 6 deletions program-states/src/state/lockup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ impl Lockup {
require!(
(kind == LockupKind::None && period == LockupPeriod::None)
|| (kind == LockupKind::Constant && period != LockupPeriod::None),
VsrError::InvalidLockupKind
MplStakingError::InvalidLockupKind
);

let end_ts = start_ts
.checked_add(period.to_secs())
.ok_or(VsrError::InvalidTimestampArguments)?;
.ok_or(MplStakingError::InvalidTimestampArguments)?;

Ok(Self {
kind,
Expand Down Expand Up @@ -117,7 +117,11 @@ impl Lockup {
}

let lockup_secs = self.seconds_left(self.start_ts);
require_eq!(lockup_secs % period_secs, 0, VsrError::InvalidLockupPeriod);
require_eq!(
lockup_secs % period_secs,
0,
MplStakingError::InvalidLockupPeriod
);

Ok(lockup_secs.checked_div(period_secs).unwrap())
}
Expand All @@ -131,14 +135,18 @@ impl Lockup {
.checked_add(
periods
.checked_mul(period_secs)
.ok_or(VsrError::InvalidTimestampArguments)?,
.ok_or(MplStakingError::InvalidTimestampArguments)?,
)
.unwrap();
require_gte!(self.end_ts, self.start_ts, VsrError::InternalProgramError);
require_gte!(
self.end_ts,
self.start_ts,
MplStakingError::InternalProgramError
);
require_eq!(
self.period_current(curr_ts)?,
0,
VsrError::InternalProgramError
MplStakingError::InternalProgramError
);
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions program-states/src/state/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Registrar {
self.voting_mints
.iter()
.position(|r| r.mint == mint)
.ok_or_else(|| error!(VsrError::VotingMintNotFound))
.ok_or_else(|| error!(MplStakingError::VotingMintNotFound))
}

pub fn max_vote_weight(&self, mint_accounts: &[AccountInfo]) -> Result<u64> {
Expand All @@ -40,14 +40,14 @@ impl Registrar {
let mint_account = mint_accounts
.iter()
.find(|a| a.key() == voting_mint_config.mint)
.ok_or_else(|| error!(VsrError::VotingMintNotFound))?;
.ok_or_else(|| error!(MplStakingError::VotingMintNotFound))?;
let mint = Account::<Mint>::try_from(mint_account)?;
sum = sum
.checked_add(mint.supply)
.ok_or_else(|| error!(VsrError::VoterWeightOverflow))?;
.ok_or_else(|| error!(MplStakingError::VoterWeightOverflow))?;
sum = sum
.checked_add(mint.supply)
.ok_or_else(|| error!(VsrError::VoterWeightOverflow))?;
.ok_or_else(|| error!(MplStakingError::VoterWeightOverflow))?;
Ok(sum)
})
}
Expand Down
10 changes: 5 additions & 5 deletions program-states/src/state/voter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Voter {
/// The extra lockup vote weight that the user is guaranteed to have at `at_ts`, assuming
/// they withdraw and unlock as much as possible starting from `curr_ts`.
pub fn weight_locked_guaranteed(&self, curr_ts: i64, at_ts: i64) -> Result<u64> {
require_gte!(at_ts, curr_ts, VsrError::InvalidTimestampArguments);
require_gte!(at_ts, curr_ts, MplStakingError::InvalidTimestampArguments);
self.deposits
.iter()
.filter(|d| d.is_used)
Expand All @@ -50,10 +50,10 @@ impl Voter {
require_gt!(
self.deposits.len(),
index,
VsrError::OutOfBoundsDepositEntryIndex
MplStakingError::OutOfBoundsDepositEntryIndex
);
let d = &mut self.deposits[index];
require!(d.is_used, VsrError::UnusedDepositEntryIndex);
require!(d.is_used, MplStakingError::UnusedDepositEntryIndex);
Ok(d)
}

Expand All @@ -62,10 +62,10 @@ impl Voter {
require_gt!(
self.deposits.len(),
index,
VsrError::OutOfBoundsDepositEntryIndex
MplStakingError::OutOfBoundsDepositEntryIndex
);
let d = &self.deposits[index];
require!(d.is_used, VsrError::UnusedDepositEntryIndex);
require!(d.is_used, MplStakingError::UnusedDepositEntryIndex);
Ok(d)
}
}
Expand Down
6 changes: 3 additions & 3 deletions programs/voter-stake-registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cargo-features = ["workspace-inheritance"]
[package]
name = "voter-stake-registry"
version = "0.2.4"
name = "mpl-staking"
version = "0.1.0"
description = "a voter weight plugin for spl-governance"
license = "GPL-3.0-or-later"
homepage = "https://github.com/blockworks-foundation/voter-stake-registry"
edition = "2018"

[lib]
crate-type = ["cdylib", "lib"]
name = "voter_stake_registry"
name = "mpl_staking"
doctest = false

[features]
Expand Down
32 changes: 18 additions & 14 deletions programs/voter-stake-registry/src/instructions/change_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
};
use anchor_lang::prelude::*;
use mplx_staking_states::{
error::VsrError,
error::MplStakingError,
state::{Registrar, Voter},
};
use solana_program::clock::SECONDS_PER_DAY;
Expand Down Expand Up @@ -67,34 +67,38 @@ pub fn change_delegate(ctx: Context<ChangeDelegate>, deposit_entry_index: u8) ->
let voter = &mut ctx.accounts.voter.load_mut()?;
let voter_authority = voter.voter_authority;
let target = voter.active_deposit_mut(deposit_entry_index)?;
let curr_ts = clock_unix_timestamp();

let delegate_last_update_diff = curr_ts
.checked_sub(target.delegate_last_update_ts)
.ok_or(MplStakingError::ArithmeticOverflow)?;

require!(
delegate_last_update_diff > DELEGATE_UPDATE_DIFF_THRESHOLD,
MplStakingError::DelegateUpdateIsTooSoon
);

if &ctx.accounts.voter.key() == &ctx.accounts.delegate_voter.key() {
require!(target.delegate != voter_authority, VsrError::SameDelegate);
require!(
target.delegate != voter_authority,
MplStakingError::SameDelegate
);
target.delegate = voter_authority;
} else {
let delegate_voter = &ctx.accounts.delegate_voter.load()?;
require!(
&ctx.accounts.voter.key() != &ctx.accounts.delegate_voter.key()
&& delegate_voter.voter_authority != target.delegate,
VsrError::SameDelegate
MplStakingError::SameDelegate
);

let curr_ts = clock_unix_timestamp();
let delegate_voter_weighted_stake = delegate_voter
.deposits
.iter()
.fold(0, |acc, d| acc + d.weighted_stake(curr_ts));
require!(
delegate_voter_weighted_stake >= Voter::MIN_OWN_WEIGHTED_STAKE,
VsrError::InsufficientWeightedStake
);
let delegate_last_update_diff = curr_ts
.checked_sub(target.delegate_last_update_ts)
.ok_or(VsrError::ArithmeticOverflow)?;

require!(
delegate_last_update_diff > DELEGATE_UPDATE_DIFF_THRESHOLD,
VsrError::DelegateUpdateIsTooSoon
MplStakingError::InsufficientWeightedStake
);

let (reward_pool, _) = find_reward_pool_address(
Expand All @@ -109,7 +113,7 @@ pub fn change_delegate(ctx: Context<ChangeDelegate>, deposit_entry_index: u8) ->

require!(
delegate_mining == ctx.accounts.new_delegate_mining.key(),
VsrError::InvalidMining
MplStakingError::InvalidMining
);
target.delegate = delegate_voter.voter_authority;
}
Expand Down
4 changes: 2 additions & 2 deletions programs/voter-stake-registry/src/instructions/claim.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{borsh::BorshDeserialize, cpi_instructions};
use anchor_lang::prelude::*;
use anchor_spl::token::{Token, TokenAccount};
use mplx_staking_states::error::VsrError;
use mplx_staking_states::error::MplStakingError;
use solana_program::program::get_return_data;
use std::borrow::Borrow;

Expand Down Expand Up @@ -86,6 +86,6 @@ pub fn claim(
msg!("Rewards are clamed {:?}", claimed_rewards);
Ok(claimed_rewards)
} else {
Err(VsrError::CpiReturnDataIsAbsent.into())
Err(MplStakingError::CpiReturnDataIsAbsent.into())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anchor_lang::prelude::*;
use mplx_staking_states::{
error::VsrError,
error::MplStakingError,
state::{DepositEntry, Voter},
};

Expand All @@ -23,7 +23,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_eq!(d.amount_deposited_native, 0, VsrError::VotingTokenNonZero);
require_eq!(d.amount_deposited_native, 0, MplStakingError::VotingTokenNonZero);

*d = DepositEntry::default();
d.is_used = false;
Expand Down
8 changes: 4 additions & 4 deletions programs/voter-stake-registry/src/instructions/close_voter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anchor_lang::prelude::*;
use anchor_spl::token::{self, CloseAccount, Token, TokenAccount, Transfer};
use bytemuck::bytes_of_mut;
use mplx_staking_states::{
error::VsrError,
error::MplStakingError,
state::{Registrar, Voter},
voter_seeds,
};
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn close_voter<'key, 'accounts, 'remaining, 'info>(
let voter = ctx.accounts.voter.load()?;

let any_locked = voter.deposits.iter().any(|d| d.amount_locked() > 0);
require!(!any_locked, VsrError::DepositStillLocked);
require!(!any_locked, MplStakingError::DepositStillLocked);

let active_deposit_entries = voter.deposits.iter().filter(|d| d.is_used).count();
require_eq!(ctx.remaining_accounts.len(), active_deposit_entries);
Expand All @@ -82,10 +82,10 @@ pub fn close_voter<'key, 'accounts, 'remaining, 'info>(
require_keys_eq!(
token.owner,
ctx.accounts.voter.key(),
VsrError::InvalidAuthority
MplStakingError::InvalidAuthority
);
require_keys_eq!(token.mint, *mint);
require_eq!(token.amount, 0, VsrError::VaultTokenNonZero);
require_eq!(token.amount, 0, MplStakingError::VaultTokenNonZero);

// transfer to target_account
let cpi_transfer_accounts = Transfer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anchor_lang::prelude::*;
use anchor_spl::token::Mint;
use mplx_staking_states::{
error::VsrError,
error::MplStakingError,
state::{Registrar, VotingMintConfig},
};

Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn configure_voting_mint(
require_gt!(
registrar.voting_mints.len(),
idx,
VsrError::OutOfBoundsVotingMintConfigIndex
MplStakingError::OutOfBoundsVotingMintConfigIndex
);

// Either it's reconfiguring an existing mint with the correct index,
Expand All @@ -52,11 +52,11 @@ pub fn configure_voting_mint(
Ok(existing_idx) => require_eq!(
existing_idx,
idx,
VsrError::VotingMintConfiguredWithDifferentIndex
MplStakingError::VotingMintConfiguredWithDifferentIndex
),
Err(_) => require!(
!registrar.voting_mints[idx].in_use(),
VsrError::VotingMintConfigIndexAlreadyInUse
MplStakingError::VotingMintConfigIndexAlreadyInUse
),
};

Expand Down
Loading

0 comments on commit 209a320

Please sign in to comment.