Skip to content

Commit

Permalink
add basic test for change_authorized_agent
Browse files Browse the repository at this point in the history
  • Loading branch information
kstepanovdev committed Sep 27, 2024
1 parent cbfdf97 commit 0734a76
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 2 deletions.
103 changes: 103 additions & 0 deletions programs/voter-stake-registry/tests/change_authorized_agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use anchor_spl::token::TokenAccount;
use mplx_staking_states::state::{LockupKind, LockupPeriod};
use program_test::*;
use solana_program_test::*;
use solana_sdk::{signature::Keypair, signer::Signer, transport::TransportError};

mod program_test;

#[tokio::test]
async fn stake_with_delegate() -> 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 voter_authority = &context.users[1].key;
let token_owner_record = realm
.create_token_owner_record(voter_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 (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 voter_account = voter.get_voter(&context.solana).await;
assert_eq!(voter_account.authorized_agent, voter.authority.pubkey());

// CREATE AGENT
let authorized_agent = Keypair::new();

context
.addin
.change_authorized_agent(&registrar, &voter, authorized_agent.pubkey())
.await?;

let voter_account = voter.get_voter(&context.solana).await;
assert_eq!(voter_account.authorized_agent, authorized_agent.pubkey());

Ok(())
}
39 changes: 37 additions & 2 deletions programs/voter-stake-registry/tests/program_test/addin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,37 @@ impl AddinCookie {
.process_transaction(&instructions, Some(&[mining_owner]))
.await
}

pub async fn change_authorized_agent(
&self,
registrar: &RegistrarCookie,
voter: &VoterCookie,
agent: Pubkey,
) -> std::result::Result<(), BanksClientError> {
let data =
anchor_lang::InstructionData::data(&mpl_staking::instruction::ChangeAuthorizedAgent {
agent,
});

let accounts = anchor_lang::ToAccountMetas::to_account_metas(
&mpl_staking::accounts::ChangeAuthorizedAgent {
registrar: registrar.address,
voter: voter.address,
voter_authority: voter.authority.pubkey(),
},
None,
);

let instructions = vec![Instruction {
program_id: self.program_id,
accounts,
data,
}];

self.solana
.process_transaction(&instructions, Some(&[&voter.authority]))
.await
}
}

impl VotingMintConfigCookie {
Expand All @@ -829,9 +860,13 @@ impl VotingMintConfigCookie {
}

impl VoterCookie {
pub async fn get_voter(&self, solana: &SolanaCookie) -> Voter {
solana.get_account::<Voter>(self.address).await
}

pub async fn deposit_amount(&self, solana: &SolanaCookie, deposit_id: u8) -> u64 {
solana.get_account::<Voter>(self.address).await.deposits[deposit_id as usize]
.amount_deposited_native
let voter = self.get_voter(solana).await;
voter.deposits[deposit_id as usize].amount_deposited_native
}

pub fn vault_address(&self, mint: &VotingMintConfigCookie) -> Pubkey {
Expand Down

0 comments on commit 0734a76

Please sign in to comment.