-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathtoken.rs
295 lines (280 loc) · 8.83 KB
/
token.rs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::error::ErrorCode;
use anchor_lang::{prelude::*, system_program};
use anchor_spl::{
token::{Token, TokenAccount},
token_2022::{
self,
spl_token_2022::{
self,
extension::{
transfer_fee::{TransferFeeConfig, MAX_FEE_BASIS_POINTS},
ExtensionType, StateWithExtensions,
},
},
},
token_interface::{
initialize_account3, spl_token_2022::extension::BaseStateWithExtensions,
InitializeAccount3, Mint,
},
};
use std::collections::HashSet;
const MINT_WHITELIST: [&'static str; 4] = [
"HVbpJAQGNpkgBaYBZQBR1t7yFdvaYVp2vCQQfKKEN4tM",
"Crn4x1Y2HUKko7ox2EZMT6N2t2ZyH7eKtwkBGVnhEq1g",
"FrBfWJ4qE5sCzKm3k3JaAtqZcXUh4LvJygDeketsrsH4",
"2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo",
];
pub fn transfer_from_user_to_pool_vault<'a>(
authority: AccountInfo<'a>,
from: AccountInfo<'a>,
to_vault: AccountInfo<'a>,
mint: AccountInfo<'a>,
token_program: AccountInfo<'a>,
amount: u64,
mint_decimals: u8,
) -> Result<()> {
if amount == 0 {
return Ok(());
}
token_2022::transfer_checked(
CpiContext::new(
token_program.to_account_info(),
token_2022::TransferChecked {
from,
to: to_vault,
authority,
mint,
},
),
amount,
mint_decimals,
)
}
pub fn transfer_from_pool_vault_to_user<'a>(
authority: AccountInfo<'a>,
from_vault: AccountInfo<'a>,
to: AccountInfo<'a>,
mint: AccountInfo<'a>,
token_program: AccountInfo<'a>,
amount: u64,
mint_decimals: u8,
signer_seeds: &[&[&[u8]]],
) -> Result<()> {
if amount == 0 {
return Ok(());
}
token_2022::transfer_checked(
CpiContext::new_with_signer(
token_program.to_account_info(),
token_2022::TransferChecked {
from: from_vault,
to,
authority,
mint,
},
signer_seeds,
),
amount,
mint_decimals,
)
}
/// Issue a spl_token `MintTo` instruction.
pub fn token_mint_to<'a>(
authority: AccountInfo<'a>,
token_program: AccountInfo<'a>,
mint: AccountInfo<'a>,
destination: AccountInfo<'a>,
amount: u64,
signer_seeds: &[&[&[u8]]],
) -> Result<()> {
token_2022::mint_to(
CpiContext::new_with_signer(
token_program,
token_2022::MintTo {
to: destination,
authority,
mint,
},
signer_seeds,
),
amount,
)
}
pub fn token_burn<'a>(
authority: AccountInfo<'a>,
token_program: AccountInfo<'a>,
mint: AccountInfo<'a>,
from: AccountInfo<'a>,
amount: u64,
signer_seeds: &[&[&[u8]]],
) -> Result<()> {
token_2022::burn(
CpiContext::new_with_signer(
token_program.to_account_info(),
token_2022::Burn {
from,
authority,
mint,
},
signer_seeds,
),
amount,
)
}
/// Calculate the fee for output amount
pub fn get_transfer_inverse_fee(mint_info: &AccountInfo, post_fee_amount: u64) -> Result<u64> {
if *mint_info.owner == Token::id() {
return Ok(0);
}
if post_fee_amount == 0 {
return err!(ErrorCode::InvalidInput);
}
let mint_data = mint_info.try_borrow_data()?;
let mint = StateWithExtensions::<spl_token_2022::state::Mint>::unpack(&mint_data)?;
let fee = if let Ok(transfer_fee_config) = mint.get_extension::<TransferFeeConfig>() {
let epoch = Clock::get()?.epoch;
let transfer_fee = transfer_fee_config.get_epoch_fee(epoch);
if u16::from(transfer_fee.transfer_fee_basis_points) == MAX_FEE_BASIS_POINTS {
u64::from(transfer_fee.maximum_fee)
} else {
transfer_fee_config
.calculate_inverse_epoch_fee(epoch, post_fee_amount)
.unwrap()
}
} else {
0
};
Ok(fee)
}
/// Calculate the fee for input amount
pub fn get_transfer_fee(mint_info: &AccountInfo, pre_fee_amount: u64) -> Result<u64> {
if *mint_info.owner == Token::id() {
return Ok(0);
}
let mint_data = mint_info.try_borrow_data()?;
let mint = StateWithExtensions::<spl_token_2022::state::Mint>::unpack(&mint_data)?;
let fee = if let Ok(transfer_fee_config) = mint.get_extension::<TransferFeeConfig>() {
transfer_fee_config
.calculate_epoch_fee(Clock::get()?.epoch, pre_fee_amount)
.unwrap()
} else {
0
};
Ok(fee)
}
pub fn is_supported_mint(mint_account: &InterfaceAccount<Mint>) -> Result<bool> {
let mint_info = mint_account.to_account_info();
if *mint_info.owner == Token::id() {
return Ok(true);
}
let mint_whitelist: HashSet<&str> = MINT_WHITELIST.into_iter().collect();
if mint_whitelist.contains(mint_account.key().to_string().as_str()) {
return Ok(true);
}
let mint_data = mint_info.try_borrow_data()?;
let mint = StateWithExtensions::<spl_token_2022::state::Mint>::unpack(&mint_data)?;
let extensions = mint.get_extension_types()?;
for e in extensions {
if e != ExtensionType::TransferFeeConfig
&& e != ExtensionType::MetadataPointer
&& e != ExtensionType::TokenMetadata
{
return Ok(false);
}
}
Ok(true)
}
pub fn create_token_account<'a>(
authority: &AccountInfo<'a>,
payer: &AccountInfo<'a>,
token_account: &AccountInfo<'a>,
mint_account: &AccountInfo<'a>,
system_program: &AccountInfo<'a>,
token_program: &AccountInfo<'a>,
signer_seeds: &[&[u8]],
) -> Result<()> {
let space = {
let mint_info = mint_account.to_account_info();
if *mint_info.owner == token_2022::Token2022::id() {
let mint_data = mint_info.try_borrow_data()?;
let mint_state =
StateWithExtensions::<spl_token_2022::state::Mint>::unpack(&mint_data)?;
let mint_extensions = mint_state.get_extension_types()?;
let required_extensions =
ExtensionType::get_required_init_account_extensions(&mint_extensions);
ExtensionType::try_calculate_account_len::<spl_token_2022::state::Account>(
&required_extensions,
)?
} else {
TokenAccount::LEN
}
};
create_or_allocate_account(
token_program.key,
payer.to_account_info(),
system_program.to_account_info(),
token_account.to_account_info(),
signer_seeds,
space,
)?;
initialize_account3(CpiContext::new(
token_program.to_account_info(),
InitializeAccount3 {
account: token_account.to_account_info(),
mint: mint_account.to_account_info(),
authority: authority.to_account_info(),
},
))
}
pub fn create_or_allocate_account<'a>(
program_id: &Pubkey,
payer: AccountInfo<'a>,
system_program: AccountInfo<'a>,
target_account: AccountInfo<'a>,
siger_seed: &[&[u8]],
space: usize,
) -> Result<()> {
let rent = Rent::get()?;
let current_lamports = target_account.lamports();
if current_lamports == 0 {
let lamports = rent.minimum_balance(space);
let cpi_accounts = system_program::CreateAccount {
from: payer,
to: target_account.clone(),
};
let cpi_context = CpiContext::new(system_program.clone(), cpi_accounts);
system_program::create_account(
cpi_context.with_signer(&[siger_seed]),
lamports,
u64::try_from(space).unwrap(),
program_id,
)?;
} else {
let required_lamports = rent
.minimum_balance(space)
.max(1)
.saturating_sub(current_lamports);
if required_lamports > 0 {
let cpi_accounts = system_program::Transfer {
from: payer.to_account_info(),
to: target_account.clone(),
};
let cpi_context = CpiContext::new(system_program.clone(), cpi_accounts);
system_program::transfer(cpi_context, required_lamports)?;
}
let cpi_accounts = system_program::Allocate {
account_to_allocate: target_account.clone(),
};
let cpi_context = CpiContext::new(system_program.clone(), cpi_accounts);
system_program::allocate(
cpi_context.with_signer(&[siger_seed]),
u64::try_from(space).unwrap(),
)?;
let cpi_accounts = system_program::Assign {
account_to_assign: target_account.clone(),
};
let cpi_context = CpiContext::new(system_program.clone(), cpi_accounts);
system_program::assign(cpi_context.with_signer(&[siger_seed]), program_id)?;
}
Ok(())
}