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
use anchor_lang::solana_program::account_info::AccountInfo;
use anchor_lang::solana_program::pubkey::Pubkey;
use anchor_lang::Result;
use anchor_lang::{context::CpiContext, Accounts};
use spl_token;
pub use spl_associated_token_account::{get_associated_token_address, ID};
pub fn create<'info>(ctx: CpiContext<'_, '_, '_, 'info, Create<'info>>) -> Result<()> {
let ix = spl_associated_token_account::instruction::create_associated_token_account(
ctx.accounts.payer.key,
ctx.accounts.authority.key,
ctx.accounts.mint.key,
&spl_token::ID,
);
solana_program::program::invoke_signed(
&ix,
&[
ctx.accounts.payer,
ctx.accounts.associated_token,
ctx.accounts.authority,
ctx.accounts.mint,
ctx.accounts.system_program,
ctx.accounts.token_program,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn create_idempotent<'info>(
ctx: CpiContext<'_, '_, '_, 'info, CreateIdemptotent<'info>>,
) -> Result<()> {
let ix = spl_associated_token_account::instruction::create_associated_token_account_idempotent(
ctx.accounts.payer.key,
ctx.accounts.authority.key,
ctx.accounts.mint.key,
&spl_token::ID,
);
solana_program::program::invoke_signed(
&ix,
&[
ctx.accounts.payer,
ctx.accounts.associated_token,
ctx.accounts.authority,
ctx.accounts.mint,
ctx.accounts.system_program,
ctx.accounts.token_program,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct Create<'info> {
pub payer: AccountInfo<'info>,
pub associated_token: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
}
type CreateIdemptotent<'info> = Create<'info>;
#[derive(Clone)]
pub struct AssociatedToken;
impl anchor_lang::Id for AssociatedToken {
fn id() -> Pubkey {
ID
}
}