testsvm_quarry/
test_mint_wrapper.rs

1//! # Test Mint Wrapper
2//!
3//! Testing utilities for the Quarry Mint Wrapper program.
4//!
5//! This module provides the `TestMintWrapper` struct which simplifies the creation
6//! and management of mint wrappers in test environments. It handles:
7//!
8//! - **Mint Wrapper Creation**: Initialize mint wrappers with proper configuration
9//! - **Token Management**: Create and manage reward token mints
10//! - **Minter Management**: Create and configure minters with allowances
11//! - **Authority Control**: Manage mint wrapper authority and permissions
12
13use crate::quarry_mint_wrapper;
14use anchor_lang::prelude::*;
15use anyhow::{Context, Result};
16use solana_sdk::signature::{Keypair, Signer};
17use testsvm::prelude::*;
18
19/// Test mint wrapper with labeled accounts
20pub struct TestMintWrapper {
21    pub label: String,
22    pub mint_wrapper: AccountRef<quarry_mint_wrapper::accounts::MintWrapper>,
23    pub mint_wrapper_base: Keypair,
24    pub reward_token_mint: AccountRef<anchor_spl::token::Mint>,
25    pub authority: Pubkey,
26}
27
28impl TestMintWrapper {
29    /// Create a new mint wrapper with the specified label and authority
30    pub fn new(env: &mut TestSVM, label: &str, authority: &Keypair) -> Result<Self> {
31        let mint_wrapper_base = env.new_wallet(&format!("mint_wrapper[{label}].base"))?;
32
33        // Calculate mint wrapper PDA
34        let mint_wrapper: AccountRef<quarry_mint_wrapper::accounts::MintWrapper> = env.get_pda(
35            &format!("mint_wrapper[{label}]"),
36            &[b"MintWrapper", mint_wrapper_base.pubkey().as_ref()],
37            quarry_mint_wrapper::ID,
38        )?;
39
40        // Create reward token mint with mint wrapper as authority
41        let reward_token_mint = env
42            .create_mint(
43                &format!("mint_wrapper[{label}].reward_token"),
44                6,
45                &mint_wrapper.key,
46            )
47            .context("Failed to create reward token mint")?;
48
49        // Create the mint wrapper
50        let create_wrapper_ix = anchor_instruction(
51            quarry_mint_wrapper::ID,
52            quarry_mint_wrapper::client::accounts::NewWrapperV2 {
53                base: mint_wrapper_base.pubkey(),
54                mint_wrapper: mint_wrapper.key,
55                admin: authority.pubkey(),
56                token_mint: reward_token_mint.key,
57                token_program: anchor_spl::token::ID,
58                payer: env.default_fee_payer(),
59                system_program: solana_sdk::system_program::ID,
60            },
61            quarry_mint_wrapper::client::args::NewWrapperV2 { hard_cap: u64::MAX },
62        );
63
64        env.execute_ixs_with_signers(&[create_wrapper_ix], &[&mint_wrapper_base])?;
65
66        Ok(TestMintWrapper {
67            label: label.to_string(),
68            mint_wrapper,
69            mint_wrapper_base,
70            reward_token_mint,
71            authority: authority.pubkey(),
72        })
73    }
74
75    /// Create a minter for the specified authority with the given allowance
76    pub fn create_minter(
77        &self,
78        env: &mut TestSVM,
79        minter_authority: &Pubkey,
80        allowance: u64,
81        admin: &Keypair,
82    ) -> Result<AccountRef<quarry_mint_wrapper::accounts::Minter>> {
83        // Calculate minter PDA
84        let minter = env.get_pda(
85            &format!("mint_wrapper[{}].minter[{}]", self.label, minter_authority),
86            &[
87                b"MintWrapperMinter",
88                self.mint_wrapper.key.as_ref(),
89                minter_authority.as_ref(),
90            ],
91            quarry_mint_wrapper::ID,
92        )?;
93
94        // Create the minter
95        let create_minter_ix = anchor_instruction(
96            quarry_mint_wrapper::ID,
97            quarry_mint_wrapper::client::accounts::NewMinterV2 {
98                new_minter_v2_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
99                    mint_wrapper: self.mint_wrapper.key,
100                    admin: admin.pubkey(),
101                },
102                new_minter_authority: *minter_authority,
103                minter: minter.key,
104                payer: env.default_fee_payer(),
105                system_program: solana_sdk::system_program::ID,
106            },
107            quarry_mint_wrapper::client::args::NewMinterV2 {},
108        );
109
110        // Set the allowance
111        let set_allowance_ix = anchor_instruction(
112            quarry_mint_wrapper::ID,
113            quarry_mint_wrapper::client::accounts::MinterUpdate {
114                minter: minter.key,
115                minter_update_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
116                    mint_wrapper: self.mint_wrapper.key,
117                    admin: admin.pubkey(),
118                },
119            },
120            quarry_mint_wrapper::client::args::MinterUpdate { allowance },
121        );
122
123        env.execute_ixs_with_signers(&[create_minter_ix, set_allowance_ix], &[admin])?;
124
125        Ok(minter)
126    }
127
128    /// Transfer mint wrapper authority to a new authority
129    pub fn transfer_authority(
130        &mut self,
131        env: &mut TestSVM,
132        new_authority: &Pubkey,
133        current_authority: &Keypair,
134    ) -> Result<()> {
135        let transfer_authority_ix = anchor_instruction(
136            quarry_mint_wrapper::ID,
137            quarry_mint_wrapper::client::accounts::TransferAdmin {
138                mint_wrapper: self.mint_wrapper.key,
139                admin: current_authority.pubkey(),
140                next_admin: *new_authority,
141            },
142            quarry_mint_wrapper::client::args::TransferAdmin {},
143        );
144
145        env.execute_ixs_with_signers(&[transfer_authority_ix], &[current_authority])?;
146
147        // Update the authority field
148        self.authority = *new_authority;
149
150        Ok(())
151    }
152
153    /// Accept mint wrapper authority transfer
154    pub fn accept_authority(&mut self, env: &mut TestSVM, new_authority: &Keypair) -> Result<()> {
155        let accept_authority_ix = anchor_instruction(
156            quarry_mint_wrapper::ID,
157            quarry_mint_wrapper::client::accounts::AcceptAdmin {
158                mint_wrapper: self.mint_wrapper.key,
159                pending_admin: new_authority.pubkey(),
160            },
161            quarry_mint_wrapper::client::args::AcceptAdmin {},
162        );
163
164        env.execute_ixs_with_signers(&[accept_authority_ix], &[new_authority])?;
165
166        // Update the authority field
167        self.authority = new_authority.pubkey();
168
169        Ok(())
170    }
171}