|
| 1 | +use anyhow::Result; |
| 2 | +use solana_client::nonblocking::rpc_client::RpcClient; |
| 3 | +use solana_sdk::{ |
| 4 | + pubkey::Pubkey, |
| 5 | + signature::{Keypair, Signer}, |
| 6 | + transaction::Transaction, |
| 7 | +}; |
| 8 | +use spl_token_2022::{ |
| 9 | + extension::{interest_bearing_mint::instruction::initialize, ExtensionType}, |
| 10 | + instruction as token_2022_instruction, |
| 11 | + state::{Account as Token2022Account, Mint as Token2022Mint}, |
| 12 | +}; |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +use crate::common::USDCMintTestHelper; |
| 16 | + |
| 17 | +/// Helper functions for creating Token 2022 accounts with specific extensions for testing |
| 18 | +pub struct ExtensionHelpers; |
| 19 | + |
| 20 | +impl ExtensionHelpers { |
| 21 | + /// Create a mint with InterestBearingConfig extension |
| 22 | + pub async fn create_mint_with_interest_bearing( |
| 23 | + rpc_client: &Arc<RpcClient>, |
| 24 | + payer: &Keypair, |
| 25 | + mint_keypair: &Keypair, |
| 26 | + ) -> Result<()> { |
| 27 | + if (rpc_client.get_account(&mint_keypair.pubkey()).await).is_ok() { |
| 28 | + return Ok(()); |
| 29 | + } |
| 30 | + |
| 31 | + let decimals = USDCMintTestHelper::get_test_usdc_mint_decimals(); |
| 32 | + |
| 33 | + let space = ExtensionType::try_calculate_account_len::<Token2022Mint>(&[ |
| 34 | + ExtensionType::InterestBearingConfig, |
| 35 | + ])?; |
| 36 | + |
| 37 | + let rent = rpc_client.get_minimum_balance_for_rent_exemption(space).await?; |
| 38 | + |
| 39 | + let create_account_instruction = solana_sdk::system_instruction::create_account( |
| 40 | + &payer.pubkey(), |
| 41 | + &mint_keypair.pubkey(), |
| 42 | + rent, |
| 43 | + space as u64, |
| 44 | + &spl_token_2022::id(), |
| 45 | + ); |
| 46 | + |
| 47 | + let initialize_interest_bearing_instruction = |
| 48 | + initialize(&spl_token_2022::id(), &mint_keypair.pubkey(), Some(payer.pubkey()), 10)?; |
| 49 | + |
| 50 | + let initialize_mint_instruction = token_2022_instruction::initialize_mint2( |
| 51 | + &spl_token_2022::id(), |
| 52 | + &mint_keypair.pubkey(), |
| 53 | + &payer.pubkey(), |
| 54 | + Some(&payer.pubkey()), |
| 55 | + decimals, |
| 56 | + )?; |
| 57 | + |
| 58 | + let recent_blockhash = rpc_client.get_latest_blockhash().await?; |
| 59 | + |
| 60 | + let transaction = Transaction::new_signed_with_payer( |
| 61 | + &[ |
| 62 | + create_account_instruction, |
| 63 | + initialize_interest_bearing_instruction, |
| 64 | + initialize_mint_instruction, |
| 65 | + ], |
| 66 | + Some(&payer.pubkey()), |
| 67 | + &[payer, mint_keypair], |
| 68 | + recent_blockhash, |
| 69 | + ); |
| 70 | + |
| 71 | + rpc_client.send_and_confirm_transaction(&transaction).await?; |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | + |
| 75 | + /// Create a manual token account with MemoTransfer extension |
| 76 | + pub async fn create_token_account_with_memo_transfer( |
| 77 | + rpc_client: &Arc<RpcClient>, |
| 78 | + payer: &Keypair, |
| 79 | + token_account_keypair: &Keypair, |
| 80 | + mint: &Pubkey, |
| 81 | + owner: &Keypair, |
| 82 | + ) -> Result<()> { |
| 83 | + if (rpc_client.get_account(&token_account_keypair.pubkey()).await).is_ok() { |
| 84 | + return Ok(()); |
| 85 | + } |
| 86 | + |
| 87 | + // Calculate space for token accounts with MemoTransfer extension |
| 88 | + // Also include TransferFeeAmount if the mint has TransferFeeConfig |
| 89 | + // (The USDC mint 2022 has TransferFeeConfig, so we need to account for it) |
| 90 | + let account_space = ExtensionType::try_calculate_account_len::<Token2022Account>(&[ |
| 91 | + ExtensionType::MemoTransfer, |
| 92 | + ExtensionType::TransferFeeAmount, |
| 93 | + ])?; |
| 94 | + let rent = rpc_client.get_minimum_balance_for_rent_exemption(account_space).await?; |
| 95 | + |
| 96 | + let create_account_instruction = solana_sdk::system_instruction::create_account( |
| 97 | + &payer.pubkey(), |
| 98 | + &token_account_keypair.pubkey(), |
| 99 | + rent, |
| 100 | + account_space as u64, |
| 101 | + &spl_token_2022::id(), |
| 102 | + ); |
| 103 | + |
| 104 | + // Initialize MemoTransfer account extension (requires memo for transfers) |
| 105 | + let initialize_memo_transfer_instruction = |
| 106 | + spl_token_2022::extension::memo_transfer::instruction::enable_required_transfer_memos( |
| 107 | + &spl_token_2022::id(), |
| 108 | + &token_account_keypair.pubkey(), |
| 109 | + &owner.pubkey(), |
| 110 | + &[&owner.pubkey()], |
| 111 | + )?; |
| 112 | + |
| 113 | + let initialize_account_instruction = token_2022_instruction::initialize_account3( |
| 114 | + &spl_token_2022::id(), |
| 115 | + &token_account_keypair.pubkey(), |
| 116 | + mint, |
| 117 | + &owner.pubkey(), |
| 118 | + )?; |
| 119 | + |
| 120 | + let recent_blockhash = rpc_client.get_latest_blockhash().await?; |
| 121 | + let transaction = Transaction::new_signed_with_payer( |
| 122 | + &[ |
| 123 | + create_account_instruction, |
| 124 | + initialize_account_instruction, |
| 125 | + initialize_memo_transfer_instruction, |
| 126 | + ], |
| 127 | + Some(&payer.pubkey()), |
| 128 | + &[payer, token_account_keypair, owner], |
| 129 | + recent_blockhash, |
| 130 | + ); |
| 131 | + |
| 132 | + rpc_client.send_and_confirm_transaction(&transaction).await?; |
| 133 | + Ok(()) |
| 134 | + } |
| 135 | + |
| 136 | + pub async fn mint_tokens_to_account( |
| 137 | + rpc_client: &Arc<RpcClient>, |
| 138 | + payer: &Keypair, |
| 139 | + mint: &Pubkey, |
| 140 | + token_account: &Pubkey, |
| 141 | + mint_authority: &Keypair, |
| 142 | + amount: Option<u64>, |
| 143 | + ) -> Result<()> { |
| 144 | + let amount = amount.unwrap_or_else(|| { |
| 145 | + 1_000_000 * 10_u64.pow(USDCMintTestHelper::get_test_usdc_mint_decimals() as u32) |
| 146 | + }); |
| 147 | + |
| 148 | + let instruction = token_2022_instruction::mint_to( |
| 149 | + &spl_token_2022::id(), |
| 150 | + mint, |
| 151 | + token_account, |
| 152 | + &mint_authority.pubkey(), |
| 153 | + &[], |
| 154 | + amount, |
| 155 | + )?; |
| 156 | + |
| 157 | + let recent_blockhash = rpc_client.get_latest_blockhash().await?; |
| 158 | + let transaction = Transaction::new_signed_with_payer( |
| 159 | + &[instruction], |
| 160 | + Some(&payer.pubkey()), |
| 161 | + &[payer, mint_authority], |
| 162 | + recent_blockhash, |
| 163 | + ); |
| 164 | + |
| 165 | + rpc_client.send_and_confirm_transaction(&transaction).await?; |
| 166 | + Ok(()) |
| 167 | + } |
| 168 | +} |
0 commit comments