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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use anchor_lang::prelude::*;
use solana_program::{
program::invoke_signed,
program_memory::sol_memcmp,
program_pack::{IsInitialized, Pack},
pubkey::PUBKEY_BYTES,
};
use spl_associated_token_account::get_associated_token_address;
use crate::errors::CandyGuardError;
pub struct TokenBurnParams<'a: 'b, 'b> {
pub mint: AccountInfo<'a>,
pub source: AccountInfo<'a>,
pub amount: u64,
pub authority: AccountInfo<'a>,
pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
pub token_program: AccountInfo<'a>,
}
pub struct TokenTransferParams<'a: 'b, 'b> {
pub source: AccountInfo<'a>,
pub destination: AccountInfo<'a>,
pub amount: u64,
pub authority: AccountInfo<'a>,
pub authority_signer_seeds: &'b [&'b [u8]],
pub token_program: AccountInfo<'a>,
}
pub fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
sol_memcmp(a.as_ref(), b.as_ref(), PUBKEY_BYTES) == 0
}
pub fn assert_initialized<T: Pack + IsInitialized>(account_info: &AccountInfo) -> Result<T> {
let account: T = T::unpack_unchecked(&account_info.data.borrow())?;
if !account.is_initialized() {
err!(CandyGuardError::Uninitialized)
} else {
Ok(account)
}
}
pub fn assert_is_ata(
ata: &AccountInfo,
wallet: &Pubkey,
mint: &Pubkey,
) -> core::result::Result<spl_token::state::Account, ProgramError> {
assert_owned_by(ata, &spl_token::id())?;
let ata_account: spl_token::state::Account = assert_initialized(ata)?;
assert_keys_equal(&ata_account.owner, wallet)?;
assert_keys_equal(&ata_account.mint, mint)?;
assert_keys_equal(&get_associated_token_address(wallet, mint), ata.key)?;
Ok(ata_account)
}
pub fn assert_is_token_account(
ta: &AccountInfo,
wallet: &Pubkey,
mint: &Pubkey,
) -> core::result::Result<spl_token::state::Account, ProgramError> {
assert_owned_by(ta, &spl_token::id())?;
let token_account: spl_token::state::Account = assert_initialized(ta)?;
assert_keys_equal(&token_account.owner, wallet)?;
assert_keys_equal(&token_account.mint, mint)?;
Ok(token_account)
}
pub fn assert_keys_equal(key1: &Pubkey, key2: &Pubkey) -> Result<()> {
if !cmp_pubkeys(key1, key2) {
err!(CandyGuardError::PublicKeyMismatch)
} else {
Ok(())
}
}
pub fn assert_owned_by(account: &AccountInfo, owner: &Pubkey) -> Result<()> {
if !cmp_pubkeys(account.owner, owner) {
err!(CandyGuardError::IncorrectOwner)
} else {
Ok(())
}
}
pub fn spl_token_burn(params: TokenBurnParams) -> Result<()> {
let TokenBurnParams {
mint,
source,
authority,
token_program,
amount,
authority_signer_seeds,
} = params;
let mut seeds: Vec<&[&[u8]]> = vec![];
if let Some(seed) = authority_signer_seeds {
seeds.push(seed);
}
let result = invoke_signed(
&spl_token::instruction::burn(
token_program.key,
source.key,
mint.key,
authority.key,
&[],
amount,
)?,
&[source, mint, authority, token_program],
seeds.as_slice(),
);
result.map_err(|_| CandyGuardError::TokenBurnFailed.into())
}
pub fn spl_token_transfer(params: TokenTransferParams<'_, '_>) -> Result<()> {
let TokenTransferParams {
source,
destination,
authority,
token_program,
amount,
authority_signer_seeds,
} = params;
let mut signer_seeds = vec![];
if !authority_signer_seeds.is_empty() {
signer_seeds.push(authority_signer_seeds)
}
let result = invoke_signed(
&spl_token::instruction::transfer(
token_program.key,
source.key,
destination.key,
authority.key,
&[],
amount,
)?,
&[source, destination, authority, token_program],
&signer_seeds,
);
result.map_err(|_| CandyGuardError::TokenTransferFailed.into())
}