/
token.rs
232 lines (221 loc) · 6.68 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
use crate::error::ErrorCode;
use anchor_lang::prelude::*;
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,
},
};
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_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
}
};
let lamports = Rent::get()?.minimum_balance(space);
let cpi_accounts = anchor_lang::system_program::CreateAccount {
from: payer.to_account_info(),
to: token_account.to_account_info(),
};
let cpi_context = CpiContext::new(system_program.to_account_info(), cpi_accounts);
anchor_lang::system_program::create_account(
cpi_context.with_signer(signer_seeds),
lamports,
space as u64,
token_program.key,
)?;
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(),
},
))
}