|
1 | 1 | use anchor_lang::prelude::*;
|
2 |
| - |
3 |
| -use crate::ID; |
4 |
| -use anchor_spl::mint::USDC; |
5 | 2 | use anchor_spl::token::spl_token;
|
6 |
| -use solana_program::program_pack::Pack; |
7 | 3 | use solana_program::{
|
8 | 4 | entrypoint::ProgramResult,
|
9 | 5 | instruction::{AccountMeta, Instruction},
|
10 | 6 | program::invoke_signed_unchecked,
|
| 7 | + program_pack::Pack, |
11 | 8 | system_instruction,
|
12 | 9 | };
|
13 | 10 |
|
| 11 | +use crate::ID; |
| 12 | + |
14 | 13 | #[inline(always)]
|
15 |
| -pub fn require_min_account_infos_len(accounts: &[AccountInfo], len: usize) -> Result<()> { |
16 |
| - if accounts.len() < len { |
| 14 | +pub fn require_min_account_infos_len(accounts: &[AccountInfo], at_least_len: usize) -> Result<()> { |
| 15 | + if accounts.len() < at_least_len { |
17 | 16 | return Err(ErrorCode::AccountNotEnoughKeys.into());
|
18 | 17 | }
|
| 18 | + |
19 | 19 | Ok(())
|
20 | 20 | }
|
21 | 21 |
|
@@ -140,47 +140,43 @@ pub fn create_account_reliably(
|
140 | 140 | Ok(())
|
141 | 141 | }
|
142 | 142 |
|
143 |
| -/// Create a token account reliably |
| 143 | +/// Create a USDC token account reliably. |
144 | 144 | ///
|
145 |
| -/// This function creates a token account and initializes it with the given mint and owner. |
| 145 | +/// This function creates a USDC token account and initializes it with the given owner. |
146 | 146 | ///
|
147 | 147 | /// # Arguments
|
148 | 148 | ///
|
149 |
| -/// * `payer_pubkey` - The pubkey of the account that will pay for the token account. |
150 |
| -/// * `account_pubkey_to_create` - The pubkey of the account to create. |
151 |
| -/// * `owner_account_info` - The account info of the owner of the token account. |
152 |
| -/// * `mint_pubkey` - The pubkey of the mint. |
153 |
| -/// * `data_len` - The length of the data to be written to the token account. |
| 149 | +/// * `payer_key` - The pubkey of the account that will pay for the token account. |
| 150 | +/// * `token_account_key` - The pubkey of the account to create. |
| 151 | +/// * `token_account_owner_key` - The account info of the owner of the token account. |
| 152 | +/// * `token_account_lamports` - Current lamports on token account. |
154 | 153 | /// * `accounts` - The accounts to be used in the CPI.
|
155 | 154 | /// * `signer_seeds` - The signer seeds to be used in the CPI.
|
156 | 155 | pub fn create_usdc_token_account_reliably(
|
157 |
| - payer_pubkey: &Pubkey, |
158 |
| - account_pubkey_to_create: &Pubkey, |
159 |
| - owner_account_pubkey: &Pubkey, |
| 156 | + payer_key: &Pubkey, |
| 157 | + token_account_key: &Pubkey, |
| 158 | + token_account_owner_key: &Pubkey, |
160 | 159 | token_account_lamports: u64,
|
161 | 160 | accounts: &[AccountInfo],
|
162 | 161 | signer_seeds: &[&[&[u8]]],
|
163 | 162 | ) -> ProgramResult {
|
164 |
| - // Create the owner account |
165 | 163 | create_account_reliably(
|
166 |
| - payer_pubkey, |
167 |
| - account_pubkey_to_create, |
| 164 | + payer_key, |
| 165 | + token_account_key, |
168 | 166 | token_account_lamports,
|
169 | 167 | spl_token::state::Account::LEN,
|
170 | 168 | accounts,
|
171 | 169 | &spl_token::ID,
|
172 | 170 | signer_seeds,
|
173 | 171 | )?;
|
174 | 172 |
|
175 |
| - // Create the token account |
176 | 173 | let init_token_account_ix = spl_token::instruction::initialize_account3(
|
177 | 174 | &spl_token::ID,
|
178 |
| - account_pubkey_to_create, |
179 |
| - &USDC, |
180 |
| - owner_account_pubkey, |
181 |
| - )?; |
| 175 | + token_account_key, |
| 176 | + &common::USDC_MINT, |
| 177 | + token_account_owner_key, |
| 178 | + ) |
| 179 | + .unwrap(); |
182 | 180 |
|
183 |
| - solana_program::program::invoke_signed_unchecked(&init_token_account_ix, accounts, &[])?; |
184 |
| - |
185 |
| - Ok(()) |
| 181 | + solana_program::program::invoke_signed_unchecked(&init_token_account_ix, accounts, &[]) |
186 | 182 | }
|
0 commit comments