Skip to content

Commit

Permalink
Write tests to verify whether an end-user is allowed withdraw with lo…
Browse files Browse the repository at this point in the history
…cked tokens (#19)

Write tests to verify whether an end-user is allowed withdraw with locked tokens
  • Loading branch information
rwwwx authored Jul 23, 2024
1 parent 51738bb commit 21530ec
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 4 deletions.
6 changes: 3 additions & 3 deletions programs/voter-stake-registry/src/instructions/close_voter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ pub fn close_voter<'key, 'accounts, 'remaining, 'info>(
{
let voter = ctx.accounts.voter.load()?;

let active_deposit_entries = voter.deposits.iter().filter(|d| d.is_used).count();
require_eq!(ctx.remaining_accounts.len(), active_deposit_entries);

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

let active_deposit_entries = voter.deposits.iter().filter(|d| d.is_used).count();
require_eq!(ctx.remaining_accounts.len(), active_deposit_entries);

let voter_seeds = voter_seeds!(voter);

let active_deposits = voter.deposits.iter().filter(|d| d.is_used);
Expand Down
154 changes: 153 additions & 1 deletion programs/voter-stake-registry/tests/test_basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use anchor_spl::token::TokenAccount;
use mplx_staking_states::state::{LockupKind, LockupPeriod};
use mplx_staking_states::{
error::VsrError,
state::{LockupKind, LockupPeriod},
};
use program_test::*;
use solana_program_test::*;
use solana_sdk::{signature::Keypair, signer::Signer, transport::TransportError};
Expand Down Expand Up @@ -236,3 +239,152 @@ async fn test_basic() -> Result<(), TransportError> {

Ok(())
}

#[tokio::test]
async fn close_voter_with_locked_tokens_should_fail() -> Result<(), TransportError> {
let context = TestContext::new().await;

let payer = &context.users[0].key;
let realm_authority = Keypair::new();
let realm = context
.governance
.create_realm(
"testrealm",
realm_authority.pubkey(),
&context.mints[0],
payer,
&context.addin.program_id,
)
.await;

let deposit_authority = &context.users[1].key;
let token_owner_record = realm
.create_token_owner_record(deposit_authority.pubkey(), payer)
.await;

let fill_authority = Keypair::from_bytes(&context.users[3].key.to_bytes()).unwrap();
let distribution_authority = Keypair::new();
let (registrar, rewards_pool) = context
.addin
.create_registrar(
&realm,
&realm_authority,
payer,
&fill_authority.pubkey(),
&distribution_authority.pubkey(),
&context.rewards.program_id,
)
.await;
context
.addin
.configure_voting_mint(
&registrar,
&realm_authority,
payer,
0,
&context.mints[0],
None,
None,
)
.await;
let mngo_voting_mint = context
.addin
.configure_voting_mint(
&registrar,
&realm_authority,
payer,
0,
&context.mints[0],
None,
None,
)
.await;

let voter_authority = deposit_authority;
let (deposit_mining, _) = find_deposit_mining_addr(
&context.rewards.program_id,
&voter_authority.pubkey(),
&rewards_pool,
);

let voter = context
.addin
.create_voter(
&registrar,
&token_owner_record,
voter_authority,
payer,
&rewards_pool,
&deposit_mining,
&context.rewards.program_id,
)
.await;

let reference_account = context.users[1].token_accounts[0];

context
.addin
.create_deposit_entry(
&registrar,
&voter,
&voter,
&mngo_voting_mint,
0,
LockupKind::None,
LockupPeriod::None,
)
.await?;

context
.addin
.create_deposit_entry(
&registrar,
&voter,
&voter,
&mngo_voting_mint,
1,
LockupKind::Constant,
LockupPeriod::ThreeMonths,
)
.await?;

context
.addin
.deposit(
&registrar,
&voter,
&mngo_voting_mint,
deposit_authority,
reference_account,
0,
10000,
)
.await?;

context
.addin
.stake(
&registrar,
&voter,
voter.authority.pubkey(),
&context.rewards.program_id,
0,
1,
10000,
)
.await?;

context
.addin
.close_voter(
&registrar,
&voter,
&mngo_voting_mint,
voter_authority,
&context.rewards.program_id,
)
.await
.assert_on_chain_err(VsrError::DepositStillLocked);

Ok(())
}

0 comments on commit 21530ec

Please sign in to comment.