-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathtoken_util.rs
More file actions
444 lines (373 loc) · 15.1 KB
/
token_util.rs
File metadata and controls
444 lines (373 loc) · 15.1 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use crate::{
error::KoraError,
state::{get_all_signers, get_request_signer_with_signer_key},
token::token::TokenType,
transaction::TransactionUtil,
};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_message::{Message, VersionedMessage};
use solana_sdk::{
compute_budget::ComputeBudgetInstruction, instruction::Instruction, pubkey::Pubkey,
};
use solana_signers::SolanaSigner;
use spl_associated_token_account::{
get_associated_token_address, instruction::create_associated_token_account,
};
use std::{fmt::Display, str::FromStr, sync::Arc};
#[cfg(not(test))]
use {crate::cache::CacheUtil, crate::state::get_config};
#[cfg(test)]
use {
crate::config::SplTokenConfig, crate::tests::cache_mock::MockCacheUtil as CacheUtil,
crate::tests::config_mock::mock_state::get_config,
};
/*
This funciton is tested via the makefile, as it's a CLI command and requires a validator running.
*/
const DEFAULT_CHUNK_SIZE: usize = 10;
pub struct ATAToCreate {
pub mint: Pubkey,
pub ata: Pubkey,
pub token_program: Pubkey,
}
impl Display for ATAToCreate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Token {}: ATA {} (Token program: {})", self.mint, self.ata, self.token_program)
}
}
/// Initialize ATAs for all allowed payment tokens for the paymaster
/// This function initializes ATAs for ALL signers in the pool
///
/// Order of priority is:
/// 1. Payment address provided in config
/// 2. All signers in pool
pub async fn initialize_atas(
rpc_client: &RpcClient,
compute_unit_price: Option<u64>,
compute_unit_limit: Option<u32>,
chunk_size: Option<usize>,
fee_payer_key: Option<String>,
) -> Result<(), KoraError> {
let config = get_config()?;
let fee_payer = get_request_signer_with_signer_key(fee_payer_key.as_deref())?;
let addresses_to_initialize_atas = if let Some(payment_address) = &config.kora.payment_address {
vec![Pubkey::from_str(payment_address)
.map_err(|e| KoraError::InternalServerError(format!("Invalid payment address: {e}")))?]
} else {
get_all_signers()?.iter().map(|signer| signer.signer.pubkey()).collect::<Vec<Pubkey>>()
};
initialize_atas_with_chunk_size(
rpc_client,
&fee_payer,
&addresses_to_initialize_atas,
compute_unit_price,
compute_unit_limit,
chunk_size.unwrap_or(DEFAULT_CHUNK_SIZE),
)
.await
}
/// Initialize ATAs for all allowed payment tokens for the provided addresses with configurable chunk size
/// This function does not use cache and directly checks on-chain
pub async fn initialize_atas_with_chunk_size(
rpc_client: &RpcClient,
fee_payer: &Arc<solana_signers::Signer>,
addresses_to_initialize_atas: &Vec<Pubkey>,
compute_unit_price: Option<u64>,
compute_unit_limit: Option<u32>,
chunk_size: usize,
) -> Result<(), KoraError> {
for address in addresses_to_initialize_atas {
println!("Initializing ATAs for address: {address}");
let atas_to_create = find_missing_atas(rpc_client, address).await?;
if atas_to_create.is_empty() {
println!("✓ All required ATAs already exist for address: {address}");
continue;
}
create_atas_for_signer(
rpc_client,
fee_payer,
address,
&atas_to_create,
compute_unit_price,
compute_unit_limit,
chunk_size,
)
.await?;
}
println!("✓ Successfully created all ATAs");
Ok(())
}
/// Helper function to create ATAs for a single signer
async fn create_atas_for_signer(
rpc_client: &RpcClient,
fee_payer: &Arc<solana_signers::Signer>,
address: &Pubkey,
atas_to_create: &[ATAToCreate],
compute_unit_price: Option<u64>,
compute_unit_limit: Option<u32>,
chunk_size: usize,
) -> Result<usize, KoraError> {
let instructions = atas_to_create
.iter()
.map(|ata| {
create_associated_token_account(
&fee_payer.pubkey(),
address,
&ata.mint,
&ata.token_program,
)
})
.collect::<Vec<Instruction>>();
// Process instructions in chunks
let total_atas = instructions.len();
let chunks: Vec<_> = instructions.chunks(chunk_size).collect();
let num_chunks = chunks.len();
println!(
"Creating {total_atas} ATAs in {num_chunks} transaction(s) (chunk size: {chunk_size})..."
);
let mut created_atas_idx = 0;
for (chunk_idx, chunk) in chunks.iter().enumerate() {
let chunk_num = chunk_idx + 1;
println!("Processing chunk {chunk_num}/{num_chunks}");
// Build instructions for this chunk with compute budget
let mut chunk_instructions = Vec::new();
// Add compute budget instructions to each chunk
if let Some(compute_unit_price) = compute_unit_price {
chunk_instructions
.push(ComputeBudgetInstruction::set_compute_unit_price(compute_unit_price));
}
if let Some(compute_unit_limit) = compute_unit_limit {
chunk_instructions
.push(ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit));
}
// Add the ATA creation instructions for this chunk
chunk_instructions.extend_from_slice(chunk);
let blockhash = rpc_client
.get_latest_blockhash()
.await
.map_err(|e| KoraError::RpcError(format!("Failed to get blockhash: {e}")))?;
let fee_payer_pubkey = fee_payer.pubkey();
let message = VersionedMessage::Legacy(Message::new_with_blockhash(
&chunk_instructions,
Some(&fee_payer_pubkey),
&blockhash,
));
let mut tx = TransactionUtil::new_unsigned_versioned_transaction(message);
let message_bytes = tx.message.serialize();
let signature = fee_payer
.sign_message(&message_bytes)
.await
.map_err(|e| KoraError::SigningError(e.to_string()))?;
tx.signatures = vec![signature];
match rpc_client.send_and_confirm_transaction_with_spinner(&tx).await {
Ok(signature) => {
println!(
"✓ Chunk {chunk_num}/{num_chunks} successful. Transaction signature: {signature}"
);
// Print the ATAs created in this chunk
let chunk_end = std::cmp::min(created_atas_idx + chunk.len(), atas_to_create.len());
(created_atas_idx..chunk_end).for_each(|i| {
let ATAToCreate { mint, ata, token_program } = &atas_to_create[i];
println!(" - Token {mint}: ATA {ata} (Token program: {token_program})");
});
created_atas_idx = chunk_end;
}
Err(e) => {
println!("✗ Chunk {chunk_num}/{num_chunks} failed: {e}");
if created_atas_idx > 0 {
println!("\nSuccessfully created ATAs ({created_atas_idx}/{total_atas}):");
println!(
"{}",
atas_to_create[0..created_atas_idx]
.iter()
.map(|ata| format!(" ✓ {ata}"))
.collect::<Vec<String>>()
.join("\n")
);
println!("\nRemaining ATAs to create: {}", total_atas - created_atas_idx);
} else {
println!("No ATAs were successfully created.");
}
println!("This may be a temporary network issue. Please re-run the command to retry ATA creation.");
return Err(KoraError::RpcError(format!(
"Failed to send ATA creation transaction for chunk {chunk_num}/{num_chunks}: {e}"
)));
}
}
}
// Show summary of all successfully created ATAs
println!("\n🎉 All ATA creation completed successfully!");
println!("Successfully created ATAs ({total_atas}/{total_atas}):");
println!(
"{}",
atas_to_create.iter().map(|ata| format!(" ✓ {ata}")).collect::<Vec<String>>().join("\n")
);
Ok(total_atas)
}
pub async fn find_missing_atas(
rpc_client: &RpcClient,
payment_address: &Pubkey,
) -> Result<Vec<ATAToCreate>, KoraError> {
let config = get_config()?;
// Parse all allowed SPL paid token mints
let mut token_mints = Vec::new();
for token_str in &config.validation.allowed_spl_paid_tokens {
match Pubkey::from_str(token_str) {
Ok(mint) => token_mints.push(mint),
Err(_) => {
println!("⚠️ Skipping invalid token mint: {token_str}");
continue;
}
}
}
if token_mints.is_empty() {
println!("✓ No SPL payment tokens configured");
return Ok(Vec::new());
}
let mut atas_to_create = Vec::new();
// Check each token mint for existing ATA
for mint in &token_mints {
let ata = get_associated_token_address(payment_address, mint);
match CacheUtil::get_account(rpc_client, &ata, false).await {
Ok(_) => {
println!("✓ ATA already exists for token {mint}: {ata}");
}
Err(_) => {
// Fetch mint account to determine if it's SPL or Token2022
let mint_account =
CacheUtil::get_account(rpc_client, mint, false).await.map_err(|e| {
KoraError::RpcError(format!("Failed to fetch mint account for {mint}: {e}"))
})?;
let token_program = TokenType::get_token_program_from_owner(&mint_account.owner)?;
println!("Creating ATA for token {mint}: {ata}");
atas_to_create.push(ATAToCreate {
mint: *mint,
ata,
token_program: token_program.program_id(),
});
}
}
}
Ok(atas_to_create)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{
common::{
create_mock_rpc_client_account_not_found, create_mock_spl_mint_account,
create_mock_token_account, setup_or_get_test_signer, RpcMockBuilder,
},
config_mock::{ConfigMockBuilder, ValidationConfigBuilder},
};
use std::{
collections::VecDeque,
sync::{Arc, Mutex},
};
#[tokio::test]
async fn test_find_missing_atas_no_spl_tokens() {
let _m = ConfigMockBuilder::new()
.with_validation(
ValidationConfigBuilder::new()
.with_allowed_spl_paid_tokens(SplTokenConfig::Allowlist(vec![]))
.build(),
)
.build_and_setup();
let rpc_client = create_mock_rpc_client_account_not_found();
let payment_address = Pubkey::new_unique();
let result = find_missing_atas(&rpc_client, &payment_address).await.unwrap();
assert!(result.is_empty(), "Should return empty vec when no SPL tokens configured");
}
#[tokio::test]
async fn test_find_missing_atas_with_spl_tokens() {
let allowed_spl_tokens = [Pubkey::new_unique(), Pubkey::new_unique()];
let _m = ConfigMockBuilder::new()
.with_validation(
ValidationConfigBuilder::new()
.with_allowed_spl_paid_tokens(SplTokenConfig::Allowlist(
allowed_spl_tokens.iter().map(|p| p.to_string()).collect(),
))
.build(),
)
.build_and_setup();
let cache_ctx = CacheUtil::get_account_context();
cache_ctx.checkpoint(); // Clear any previous expectations
let payment_address = Pubkey::new_unique();
let rpc_client = create_mock_rpc_client_account_not_found();
// First call: Found in cache (Ok)
// Second call: ATA account not found (Err)
// Third call: mint account found (Ok)
let responses = Arc::new(Mutex::new(VecDeque::from([
Ok(create_mock_token_account(&Pubkey::new_unique(), &Pubkey::new_unique())),
Err(KoraError::RpcError("ATA not found".to_string())),
Ok(create_mock_spl_mint_account(6)),
])));
let responses_clone = responses.clone();
cache_ctx
.expect()
.times(3)
.returning(move |_, _, _| responses_clone.lock().unwrap().pop_front().unwrap());
let result = find_missing_atas(&rpc_client, &payment_address).await;
assert!(result.is_ok(), "Should handle SPL tokens with proper mocking");
let atas = result.unwrap();
assert_eq!(atas.len(), 1, "Should return 1 missing ATAs");
}
#[tokio::test]
async fn test_create_atas_for_signer_calls_rpc_correctly() {
let _m = ConfigMockBuilder::new().build_and_setup();
let _ = setup_or_get_test_signer();
let address = Pubkey::new_unique();
let mint1 = Pubkey::new_unique();
let mint2 = Pubkey::new_unique();
let atas_to_create = vec![
ATAToCreate {
mint: mint1,
ata: spl_associated_token_account::get_associated_token_address(&address, &mint1),
token_program: spl_token::id(),
},
ATAToCreate {
mint: mint2,
ata: spl_associated_token_account::get_associated_token_address(&address, &mint2),
token_program: spl_token::id(),
},
];
let rpc_client = RpcMockBuilder::new().with_blockhash().with_send_transaction().build();
let result = create_atas_for_signer(
&rpc_client,
&get_request_signer_with_signer_key(None).unwrap(),
&address,
&atas_to_create,
Some(1000),
Some(100_000),
2,
)
.await;
// Should fail with signature validation error since mock signature doesn't match real transaction
match result {
Ok(_) => {
panic!("Expected signature validation error, but got success");
}
Err(e) => {
let error_msg = format!("{e:?}");
// Check if it's a signature validation error (the mocked signature doesn't match the real transaction signature)
assert!(
error_msg.contains("signature")
|| error_msg.contains("Signature")
|| error_msg.contains("invalid")
|| error_msg.contains("mismatch"),
"Expected signature validation error, got: {error_msg}"
);
}
}
}
#[tokio::test]
async fn test_initialize_atas_when_all_tokens_are_allowed() {
let _m = ConfigMockBuilder::new()
.with_allowed_spl_paid_tokens(SplTokenConfig::All)
.build_and_setup();
let _ = setup_or_get_test_signer();
let rpc_client = RpcMockBuilder::new().build();
let result = initialize_atas(&rpc_client, None, None, None, None).await;
assert!(result.is_ok(), "Expected atas init to succeed");
}
}