pub trait TestSVMSPLHelpers {
// Required methods
fn create_mint(
&mut self,
name: &str,
decimals: u8,
authority: &Pubkey,
) -> Result<AccountRef<Mint>>;
fn create_ata_ix(
&mut self,
label: &str,
owner: &Pubkey,
mint: &Pubkey,
) -> Result<(Instruction, AccountRef<TokenAccount>)>;
}
Expand description
SPL Token helper functions for TestSVM
Required Methods§
Sourcefn create_mint(
&mut self,
name: &str,
decimals: u8,
authority: &Pubkey,
) -> Result<AccountRef<Mint>>
fn create_mint( &mut self, name: &str, decimals: u8, authority: &Pubkey, ) -> Result<AccountRef<Mint>>
Create a mint with the test SVM’s payer and add to address book
§Arguments
name
- Name for the mint in the address bookdecimals
- Number of decimals for the tokenauthority
- Mint and freeze authority for the token
§Example
use testsvm_core::prelude::*;
use testsvm_spl::TestSVMSPLHelpers;
let mut svm = TestSVM::init()?;
let authority = Keypair::new();
// Create a mint with 6 decimals (like USDC)
let mint = svm.create_mint("usdc", 6, &authority.pubkey())?;
// The mint is automatically added to the address book
assert!(svm.address_book.contains(&mint.key));
// Read the mint information from chain
let mint_data: anchor_spl::token::Mint = mint.load(&svm)?;
assert_eq!(mint_data.decimals, 6);
assert_eq!(mint_data.mint_authority.unwrap(), authority.pubkey());
assert_eq!(mint_data.supply, 0);
assert!(mint_data.is_initialized);
§Example - Minting tokens
use testsvm_core::prelude::*;
use testsvm_spl::TestSVMSPLHelpers;
use anchor_spl::token;
let mut svm = TestSVM::init()?;
let authority = svm.new_wallet("authority")?;
let user = svm.new_wallet("user")?;
// Create a mint
let mint = svm.create_mint("my_token", 9, &authority.pubkey())?;
// Create an ATA for the user
let (create_ata_ix, user_ata) = svm.create_ata_ix(
"user_ata",
&user.pubkey(),
&mint.key
)?;
svm.execute_ixs(&[create_ata_ix])?;
// Mint 1000 tokens to the user (with 9 decimals)
let mint_amount = 1000 * 10u64.pow(9);
let mint_ix = token::spl_token::instruction::mint_to(
&token::ID,
&mint.key,
&user_ata.key,
&authority.pubkey(),
&[],
mint_amount,
)?;
svm.execute_ixs_with_signers(&[mint_ix], &[&authority])?;
// Verify the mint supply was updated
let mint_data: token::Mint = mint.load(&svm)?;
assert_eq!(mint_data.supply, mint_amount);
// Verify the user received the tokens
let ata_data: token::TokenAccount = user_ata.load(&svm)?;
assert_eq!(ata_data.amount, mint_amount);
Sourcefn create_ata_ix(
&mut self,
label: &str,
owner: &Pubkey,
mint: &Pubkey,
) -> Result<(Instruction, AccountRef<TokenAccount>)>
fn create_ata_ix( &mut self, label: &str, owner: &Pubkey, mint: &Pubkey, ) -> Result<(Instruction, AccountRef<TokenAccount>)>
Create an associated token account instruction and add to address book
Returns the instruction and the ATA address. The instruction must be executed separately to actually create the account on-chain.
§Arguments
label
- Label for the ATA in the address bookowner
- Owner of the associated token accountmint
- Mint for which to create the ATA
§Example
use testsvm_core::prelude::*;
use testsvm_spl::TestSVMSPLHelpers;
let mut svm = TestSVM::init()?;
let authority = Keypair::new();
let user = Keypair::new();
// Create a mint first
let mint = svm.create_mint("token", 9, &authority.pubkey())?;
// Create an ATA instruction for the user
let (ix, ata) = svm.create_ata_ix("user_token_ata", &user.pubkey(), &mint.key)?;
// Execute the instruction to create the ATA on-chain
svm.execute_ixs(&[ix])?;
// The ATA is automatically added to the address book
assert!(svm.address_book.contains(&ata.key));
// Read the ATA information from chain
let ata_data: anchor_spl::token::TokenAccount = ata.load(&svm)?;
assert_eq!(ata_data.owner, user.pubkey());
assert_eq!(ata_data.mint, mint.key);
assert_eq!(ata_data.amount, 0);
assert_eq!(ata_data.state, anchor_spl::token::spl_token::state::AccountState::Initialized);