1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::*;
use crate::{instruction::AssociatedTokenAccountInstruction, tools::account::create_pda_account};
use borsh::BorshDeserialize;
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::invoke,
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
sysvar::Sysvar,
};
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
let instruction = if input.is_empty() {
AssociatedTokenAccountInstruction::Create
} else {
AssociatedTokenAccountInstruction::try_from_slice(input)
.map_err(|_| ProgramError::InvalidInstructionData)?
};
msg!("{:?}", instruction);
match instruction {
AssociatedTokenAccountInstruction::Create {} => {
process_create_associated_token_account(program_id, accounts)
}
}
}
pub fn process_create_associated_token_account(
program_id: &Pubkey,
accounts: &[AccountInfo],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let funder_info = next_account_info(account_info_iter)?;
let associated_token_account_info = next_account_info(account_info_iter)?;
let wallet_account_info = next_account_info(account_info_iter)?;
let spl_token_mint_info = next_account_info(account_info_iter)?;
let system_program_info = next_account_info(account_info_iter)?;
let spl_token_program_info = next_account_info(account_info_iter)?;
let spl_token_program_id = spl_token_program_info.key;
let rent = Rent::get()?;
let (associated_token_address, bump_seed) = get_associated_token_address_and_bump_seed_internal(
wallet_account_info.key,
spl_token_mint_info.key,
program_id,
spl_token_program_id,
);
if associated_token_address != *associated_token_account_info.key {
msg!("Error: Associated address does not match seed derivation");
return Err(ProgramError::InvalidSeeds);
}
let associated_token_account_signer_seeds: &[&[_]] = &[
&wallet_account_info.key.to_bytes(),
&spl_token_program_id.to_bytes(),
&spl_token_mint_info.key.to_bytes(),
&[bump_seed],
];
create_pda_account(
funder_info,
&rent,
spl_token::state::Account::LEN,
spl_token_program_id,
system_program_info,
associated_token_account_info,
associated_token_account_signer_seeds,
)?;
msg!("Initialize the associated token account");
invoke(
&spl_token::instruction::initialize_account3(
spl_token_program_id,
associated_token_account_info.key,
spl_token_mint_info.key,
wallet_account_info.key,
)?,
&[
associated_token_account_info.clone(),
spl_token_mint_info.clone(),
wallet_account_info.clone(),
spl_token_program_info.clone(),
],
)
}