-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathextension_helpers.rs
More file actions
279 lines (238 loc) · 9.31 KB
/
extension_helpers.rs
File metadata and controls
279 lines (238 loc) · 9.31 KB
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
use crate::common::USDCMintTestHelper;
use anyhow::Result;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
instruction::AccountMeta,
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction::create_account;
use spl_token_2022::{
extension::{interest_bearing_mint::instruction::initialize, transfer_hook, ExtensionType},
instruction as token_2022_instruction,
state::{Account as Token2022Account, Mint as Token2022Mint},
};
use spl_transfer_hook_interface::{
get_extra_account_metas_address, instruction::initialize_extra_account_meta_list,
};
use std::sync::Arc;
/// Helper functions for creating Token 2022 accounts with specific extensions for testing
pub struct ExtensionHelpers;
impl ExtensionHelpers {
/// Create a mint with InterestBearingConfig extension
pub async fn create_mint_with_interest_bearing(
rpc_client: &Arc<RpcClient>,
payer: &Keypair,
mint_keypair: &Keypair,
) -> Result<()> {
if (rpc_client.get_account(&mint_keypair.pubkey()).await).is_ok() {
return Ok(());
}
let decimals = USDCMintTestHelper::get_test_usdc_mint_decimals();
let space = ExtensionType::try_calculate_account_len::<Token2022Mint>(&[
ExtensionType::InterestBearingConfig,
])?;
let rent = rpc_client.get_minimum_balance_for_rent_exemption(space).await?;
let create_account_instruction = create_account(
&payer.pubkey(),
&mint_keypair.pubkey(),
rent,
space as u64,
&spl_token_2022::id(),
);
let initialize_interest_bearing_instruction =
initialize(&spl_token_2022::id(), &mint_keypair.pubkey(), Some(payer.pubkey()), 10)?;
let initialize_mint_instruction = token_2022_instruction::initialize_mint2(
&spl_token_2022::id(),
&mint_keypair.pubkey(),
&payer.pubkey(),
Some(&payer.pubkey()),
decimals,
)?;
let recent_blockhash = rpc_client.get_latest_blockhash().await?;
let transaction = Transaction::new_signed_with_payer(
&[
create_account_instruction,
initialize_interest_bearing_instruction,
initialize_mint_instruction,
],
Some(&payer.pubkey()),
&[payer, mint_keypair],
recent_blockhash,
);
rpc_client.send_and_confirm_transaction(&transaction).await?;
Ok(())
}
/// Create a manual token account with MemoTransfer extension
pub async fn create_token_account_with_memo_transfer(
rpc_client: &Arc<RpcClient>,
payer: &Keypair,
token_account_keypair: &Keypair,
mint: &Pubkey,
owner: &Keypair,
) -> Result<()> {
if (rpc_client.get_account(&token_account_keypair.pubkey()).await).is_ok() {
return Ok(());
}
// Calculate space for token accounts with MemoTransfer extension
// Also include TransferFeeAmount if the mint has TransferFeeConfig
// (The USDC mint 2022 has TransferFeeConfig, so we need to account for it)
let account_space = ExtensionType::try_calculate_account_len::<Token2022Account>(&[
ExtensionType::MemoTransfer,
ExtensionType::TransferFeeAmount,
])?;
let rent = rpc_client.get_minimum_balance_for_rent_exemption(account_space).await?;
let create_account_instruction = create_account(
&payer.pubkey(),
&token_account_keypair.pubkey(),
rent,
account_space as u64,
&spl_token_2022::id(),
);
// Initialize MemoTransfer account extension (requires memo for transfers)
let initialize_memo_transfer_instruction =
spl_token_2022::extension::memo_transfer::instruction::enable_required_transfer_memos(
&spl_token_2022::id(),
&token_account_keypair.pubkey(),
&owner.pubkey(),
&[&owner.pubkey()],
)?;
let initialize_account_instruction = token_2022_instruction::initialize_account3(
&spl_token_2022::id(),
&token_account_keypair.pubkey(),
mint,
&owner.pubkey(),
)?;
let recent_blockhash = rpc_client.get_latest_blockhash().await?;
let transaction = Transaction::new_signed_with_payer(
&[
create_account_instruction,
initialize_account_instruction,
initialize_memo_transfer_instruction,
],
Some(&payer.pubkey()),
&[payer, token_account_keypair, owner],
recent_blockhash,
);
rpc_client.send_and_confirm_transaction(&transaction).await?;
Ok(())
}
pub async fn mint_tokens_to_account(
rpc_client: &Arc<RpcClient>,
payer: &Keypair,
mint: &Pubkey,
token_account: &Pubkey,
mint_authority: &Keypair,
amount: Option<u64>,
) -> Result<()> {
let amount = amount.unwrap_or_else(|| {
1_000_000 * 10_u64.pow(USDCMintTestHelper::get_test_usdc_mint_decimals() as u32)
});
let instruction = token_2022_instruction::mint_to(
&spl_token_2022::id(),
mint,
token_account,
&mint_authority.pubkey(),
&[],
amount,
)?;
let recent_blockhash = rpc_client.get_latest_blockhash().await?;
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&payer.pubkey()),
&[payer, mint_authority],
recent_blockhash,
);
rpc_client.send_and_confirm_transaction(&transaction).await?;
Ok(())
}
/// Create a mint with TransferHook extension for testing
pub async fn create_mint_with_transfer_hook(
rpc_client: &Arc<RpcClient>,
payer: &Keypair,
mint_keypair: &Keypair,
hook_program_id: &Pubkey,
) -> Result<()> {
if (rpc_client.get_account(&mint_keypair.pubkey()).await).is_ok() {
return Ok(());
}
// Calculate space for mint with TransferHook extension
let space = ExtensionType::try_calculate_account_len::<Token2022Mint>(&[
ExtensionType::TransferHook,
])?;
let rent = rpc_client.get_minimum_balance_for_rent_exemption(space).await?;
let create_account_instruction = create_account(
&payer.pubkey(),
&mint_keypair.pubkey(),
rent,
space as u64,
&spl_token_2022::id(),
);
// Initialize the transfer hook extension
let initialize_hook_instruction = transfer_hook::instruction::initialize(
&spl_token_2022::id(),
&mint_keypair.pubkey(),
Some(payer.pubkey()),
Some(*hook_program_id),
)?;
let initialize_mint_instruction = token_2022_instruction::initialize_mint2(
&spl_token_2022::id(),
&mint_keypair.pubkey(),
&payer.pubkey(),
Some(&payer.pubkey()),
USDCMintTestHelper::get_test_usdc_mint_decimals(),
)?;
let recent_blockhash = rpc_client.get_latest_blockhash().await?;
let transaction = Transaction::new_signed_with_payer(
&[create_account_instruction, initialize_hook_instruction, initialize_mint_instruction],
Some(&payer.pubkey()),
&[payer, mint_keypair],
recent_blockhash,
);
rpc_client.send_and_confirm_transaction(&transaction).await?;
// After mint is created, we need to initialize the Extra Account Meta List
Self::initialize_extra_account_meta_list(
rpc_client,
payer,
&mint_keypair.pubkey(),
hook_program_id,
)
.await?;
Ok(())
}
/// Initialize Extra Account Meta List for transfer hook
async fn initialize_extra_account_meta_list(
rpc_client: &Arc<RpcClient>,
payer: &Keypair,
mint: &Pubkey,
hook_program_id: &Pubkey,
) -> Result<()> {
let extra_account_metas_address = get_extra_account_metas_address(mint, hook_program_id);
if rpc_client.get_account(&extra_account_metas_address).await.is_ok() {
return Ok(());
}
// Create an empty list of extra account metas (our simple hook doesn't need any)
let extra_account_metas = vec![];
let mut initialize_instruction = initialize_extra_account_meta_list(
hook_program_id,
&extra_account_metas_address,
mint,
&payer.pubkey(),
&extra_account_metas,
);
// Add the system program account which is needed for PDA creation
initialize_instruction
.accounts
.push(AccountMeta::new_readonly(solana_sdk::system_program::id(), false));
let recent_blockhash = rpc_client.get_latest_blockhash().await?;
let transaction = Transaction::new_signed_with_payer(
&[initialize_instruction],
Some(&payer.pubkey()),
&[payer],
recent_blockhash,
);
rpc_client.send_and_confirm_transaction(&transaction).await?;
Ok(())
}
}