-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathoptimized_transaction.rs
698 lines (612 loc) · 28.3 KB
/
optimized_transaction.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use crate::error::{HeliusError, Result};
use crate::types::{
CreateSmartTransactionConfig, CreateSmartTransactionSeedConfig, GetPriorityFeeEstimateOptions,
GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse, SmartTransaction, SmartTransactionConfig, Timeout,
};
use crate::Helius;
use std::sync::Arc;
use bincode::{serialize, ErrorKind};
use reqwest::StatusCode;
use solana_client::{
rpc_client::SerializableTransaction,
rpc_config::{RpcSendTransactionConfig, RpcSimulateTransactionConfig},
rpc_response::{Response, RpcSimulateTransactionResult},
};
use solana_sdk::signature::keypair_from_seed;
use solana_sdk::{
address_lookup_table::AddressLookupTableAccount,
bs58::encode,
commitment_config::CommitmentConfig,
compute_budget::ComputeBudgetInstruction,
hash::Hash,
instruction::Instruction,
message::{v0, VersionedMessage},
pubkey::Pubkey,
signature::{Signature, Signer},
signer::keypair::Keypair,
transaction::{Transaction, VersionedTransaction},
};
use solana_transaction_status::TransactionConfirmationStatus;
use std::time::{Duration, Instant};
use tokio::time::sleep;
impl Helius {
/// Simulates a transaction to get the total compute units consumed
///
/// # Arguments
/// * `instructions` - The transaction instructions
/// * `payer` - The public key of the payer
/// * `lookup_tables` - The address lookup tables
/// * `signers` - The signers for the transaction
///
/// # Returns
/// The compute units consumed, or None if unsuccessful
pub async fn get_compute_units(
&self,
instructions: Vec<Instruction>,
payer: Pubkey,
lookup_tables: Vec<AddressLookupTableAccount>,
signers: Option<&[Arc<dyn Signer>]>,
) -> Result<Option<u64>> {
// Set the compute budget limit
let test_instructions: Vec<Instruction> = vec![ComputeBudgetInstruction::set_compute_unit_limit(1_400_000)]
.into_iter()
.chain(instructions)
.collect::<Vec<_>>();
// Fetch the latest blockhash
let recent_blockhash: Hash = self.connection().get_latest_blockhash()?;
// Create a v0::Message
let v0_message: v0::Message =
v0::Message::try_compile(&payer, &test_instructions, &lookup_tables, recent_blockhash)?;
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
// Create a VersionedTransaction (signed or unsigned)
let transaction: VersionedTransaction = if let Some(signers) = signers {
VersionedTransaction::try_new(versioned_message, signers)
.map_err(|e| HeliusError::InvalidInput(format!("Signing error: {:?}", e)))?
} else {
VersionedTransaction {
signatures: vec![],
message: versioned_message,
}
};
// Simulate the transaction
let config: RpcSimulateTransactionConfig = RpcSimulateTransactionConfig {
sig_verify: signers.is_some(),
..Default::default()
};
let result: Response<RpcSimulateTransactionResult> = self
.connection()
.simulate_transaction_with_config(&transaction, config)?;
// Return the units consumed or None if not available
Ok(result.value.units_consumed)
}
/// Poll a transaction to check whether it has been confirmed
///
/// * `txt-sig` - The transaction signature to check
///
/// # Returns
/// The confirmed transaction signature or an error if the confirmation times out
pub async fn poll_transaction_confirmation(&self, txt_sig: Signature) -> Result<Signature> {
// 15 second timeout
let timeout: Duration = Duration::from_secs(15);
// 5 second retry interval
let interval: Duration = Duration::from_secs(5);
let start: Instant = Instant::now();
loop {
if start.elapsed() >= timeout {
return Err(HeliusError::Timeout {
code: StatusCode::REQUEST_TIMEOUT,
text: format!("Transaction {}'s confirmation timed out", txt_sig),
});
}
let status = self.connection().get_signature_statuses(&[txt_sig])?;
match status.value[0].clone() {
Some(status) => {
if status.err.is_none()
&& (status.confirmation_status == Some(TransactionConfirmationStatus::Confirmed)
|| status.confirmation_status == Some(TransactionConfirmationStatus::Finalized))
{
return Ok(txt_sig);
}
if status.err.is_some() {
return Err(HeliusError::TransactionError(status.err.unwrap()));
}
}
None => {
sleep(interval).await;
}
}
}
}
/// Creates an optimized transaction based on the provided configuration
///
/// # Arguments
/// * `config` - The configuration for the smart transaction, which includes the transaction's instructions, signers, and lookup tables, depending on
/// whether it's a legacy or versioned smart transaction. The transaction's send configuration can also be changed, if provided
///
/// # Returns
/// An optimized `SmartTransaction` (i.e., `Transaction` or `VersionedTransaction`) and the `last_valid_block_height`
pub async fn create_smart_transaction(
&self,
config: &CreateSmartTransactionConfig,
) -> Result<(SmartTransaction, u64)> {
if config.signers.is_empty() {
return Err(HeliusError::InvalidInput(
"The fee payer must sign the transaction".to_string(),
));
}
let payer_pubkey: Pubkey = config
.fee_payer
.as_ref()
.map_or(config.signers[0].pubkey(), |signer| signer.pubkey());
let (recent_blockhash, last_valid_block_hash) = self
.connection()
.get_latest_blockhash_with_commitment(CommitmentConfig::confirmed())?;
let mut final_instructions: Vec<Instruction> = vec![];
// Check if any of the instructions provided set the compute unit price and/or limit, and throw an error if `true`
let existing_compute_budget_instructions: bool = config.instructions.iter().any(|instruction| {
instruction.program_id == ComputeBudgetInstruction::set_compute_unit_limit(0).program_id
|| instruction.program_id == ComputeBudgetInstruction::set_compute_unit_price(0).program_id
});
if existing_compute_budget_instructions {
return Err(HeliusError::InvalidInput(
"Cannot provide instructions that set the compute unit price and/or limit".to_string(),
));
}
// Determine if we need to use a versioned transaction
let is_versioned: bool = config.lookup_tables.is_some();
let mut legacy_transaction: Option<Transaction> = None;
let mut versioned_transaction: Option<VersionedTransaction> = None;
// Build the initial transaction based on whether lookup tables are present
if is_versioned {
let lookup_tables: &[AddressLookupTableAccount] = config.lookup_tables.as_deref().unwrap_or_default();
let v0_message: v0::Message =
v0::Message::try_compile(&payer_pubkey, &config.instructions, lookup_tables, recent_blockhash)?;
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
let all_signers: Vec<Arc<dyn Signer>> = if let Some(fee_payer) = &config.fee_payer {
let mut all_signers = config.signers.clone();
if !all_signers.iter().any(|signer| signer.pubkey() == fee_payer.pubkey()) {
all_signers.push(fee_payer.clone());
}
all_signers
} else {
config.signers.clone()
};
// Sign the versioned transaction
let signatures: Vec<Signature> = all_signers
.iter()
.map(|signer| signer.try_sign_message(versioned_message.serialize().as_slice()))
.collect::<std::result::Result<Vec<_>, _>>()?;
versioned_transaction = Some(VersionedTransaction {
signatures,
message: versioned_message,
});
} else {
// If no lookup tables are present, we build a regular transaction
let mut tx: Transaction = Transaction::new_with_payer(&config.instructions, Some(&payer_pubkey));
tx.try_partial_sign(&config.signers, recent_blockhash)?;
if let Some(fee_payer) = config.fee_payer.as_ref() {
tx.try_partial_sign(&[fee_payer], recent_blockhash)?;
}
legacy_transaction = Some(tx);
}
// Serialize the transaction
let serialized_tx: Vec<u8> = if let Some(tx) = &legacy_transaction {
serialize(&tx).map_err(|e: Box<ErrorKind>| HeliusError::InvalidInput(e.to_string()))?
} else if let Some(tx) = &versioned_transaction {
serialize(&tx).map_err(|e: Box<ErrorKind>| HeliusError::InvalidInput(e.to_string()))?
} else {
return Err(HeliusError::InvalidInput("No transaction available".to_string()));
};
// Encode the transaction
let transaction_base58: String = encode(&serialized_tx).into_string();
// Get the priority fee estimate based on the serialized transaction
let priority_fee_request: GetPriorityFeeEstimateRequest = GetPriorityFeeEstimateRequest {
transaction: Some(transaction_base58),
account_keys: None,
options: Some(GetPriorityFeeEstimateOptions {
recommended: Some(true),
..Default::default()
}),
};
let priority_fee_estimate: GetPriorityFeeEstimateResponse =
self.rpc().get_priority_fee_estimate(priority_fee_request).await?;
let priority_fee_recommendation: u64 =
priority_fee_estimate
.priority_fee_estimate
.ok_or(HeliusError::InvalidInput(
"Priority fee estimate not available".to_string(),
))? as u64;
let priority_fee: u64 = if let Some(provided_fee) = config.priority_fee_cap {
// Take the minimum between the estimate and the user-provided cap
std::cmp::min(priority_fee_recommendation, provided_fee)
} else {
priority_fee_recommendation
};
// Add the compute unit price instruction with the estimated fee
let compute_budget_ix: Instruction = ComputeBudgetInstruction::set_compute_unit_price(priority_fee);
let mut updated_instructions: Vec<Instruction> = config.instructions.clone();
updated_instructions.push(compute_budget_ix.clone());
final_instructions.push(compute_budget_ix);
// Get the optimal compute units
let units: Option<u64> = self
.get_compute_units(
updated_instructions,
payer_pubkey,
config.lookup_tables.clone().unwrap_or_default(),
Some(&config.signers),
)
.await?;
if units.is_none() {
return Err(HeliusError::InvalidInput(
"Error fetching compute units for the instructions provided".to_string(),
));
}
let compute_units: u64 = units.unwrap();
println!("{}", compute_units);
let customers_cu: u32 = if compute_units < 1000 {
1000
} else {
(compute_units as f64 * 1.1).ceil() as u32
};
// Add the compute unit limit instruction with a margin
let compute_units_ix: Instruction = ComputeBudgetInstruction::set_compute_unit_limit(customers_cu);
final_instructions.push(compute_units_ix);
// Add the original instructions back
final_instructions.extend(config.instructions.clone());
// Rebuild the transaction with the final instructions
if is_versioned {
let lookup_tables: &[AddressLookupTableAccount] = config.lookup_tables.as_deref().unwrap();
let v0_message: v0::Message =
v0::Message::try_compile(&payer_pubkey, &final_instructions, lookup_tables, recent_blockhash)?;
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
let all_signers: Vec<Arc<dyn Signer>> = if let Some(fee_payer) = config.fee_payer.as_ref() {
let mut all_signers = config.signers.clone();
if !all_signers.iter().any(|signer| signer.pubkey() == fee_payer.pubkey()) {
all_signers.push(fee_payer.clone());
}
all_signers
} else {
config.signers.clone()
};
let signatures: Vec<Signature> = all_signers
.iter()
.map(|signer| signer.try_sign_message(versioned_message.serialize().as_slice()))
.collect::<std::result::Result<Vec<_>, _>>()?;
versioned_transaction = Some(VersionedTransaction {
signatures,
message: versioned_message,
});
Ok((
SmartTransaction::Versioned(versioned_transaction.unwrap()),
last_valid_block_hash,
))
} else {
let mut tx: Transaction = Transaction::new_with_payer(&final_instructions, Some(&payer_pubkey));
tx.try_partial_sign(&config.signers, recent_blockhash)?;
if let Some(fee_payer) = config.fee_payer.as_ref() {
tx.try_partial_sign(&[fee_payer], recent_blockhash)?;
}
legacy_transaction = Some(tx);
Ok((
SmartTransaction::Legacy(legacy_transaction.unwrap()),
last_valid_block_hash,
))
}
}
/// Builds and sends an optimized transaction, and handles its confirmation status
///
/// # Arguments
/// * `config` - The configuration for the smart transaction, which includes the transaction's instructions, signers, and lookup tables, depending on
/// whether it's a legacy or versioned smart transaction. The transaction's send configuration can also be changed, if provided
///
/// # Returns
/// The transaction signature, if successful
pub async fn send_smart_transaction(&self, config: SmartTransactionConfig) -> Result<Signature> {
let (transaction, last_valid_block_height) = self.create_smart_transaction(&config.create_config).await?;
match transaction {
SmartTransaction::Legacy(tx) => {
self.send_and_confirm_transaction(
&tx,
config.send_options,
last_valid_block_height,
Some(config.timeout.into()),
)
.await
}
SmartTransaction::Versioned(tx) => {
self.send_and_confirm_transaction(
&tx,
config.send_options,
last_valid_block_height,
Some(config.timeout.into()),
)
.await
}
}
}
/// Sends a transaction and handles its confirmation status
///
/// # Arguments
/// * `transaction` - The transaction to be sent, which implements `SerializableTransaction`
/// * `send_transaction_config` - Configuration options for sending the transaction
/// * `last_valid_block_height` - The last block height at which the transaction is valid
/// * `timeout` - Optional duration for polling transaction confirmation, defaults to 60 seconds
///
/// # Returns
/// The transaction signature, if successful
pub async fn send_and_confirm_transaction(
&self,
transaction: &impl SerializableTransaction,
send_transaction_config: RpcSendTransactionConfig,
last_valid_block_height: u64,
timeout: Option<Duration>,
) -> Result<Signature> {
// Retry logic with a timeout
let timeout: Duration = timeout.unwrap_or(Duration::from_secs(60));
let start_time: Instant = Instant::now();
while Instant::now().duration_since(start_time) < timeout
|| self.connection().get_block_height()? <= last_valid_block_height
{
let result = self
.connection()
.send_transaction_with_config(transaction, send_transaction_config);
match result {
Ok(signature) => {
// Poll for transaction confirmation
match self.poll_transaction_confirmation(signature).await {
Ok(sig) => return Ok(sig),
// Retry on polling failure
Err(_) => continue,
}
}
// Retry on send failure
Err(_) => continue,
}
}
Err(HeliusError::Timeout {
code: StatusCode::REQUEST_TIMEOUT,
text: "Transaction failed to confirm in 60s".to_string(),
})
}
/// Thread safe version of get_compute_units to simulate a transaction to get the total compute units consumed
///
/// # Arguments
/// * `instructions` - The transaction instructions
/// * `payer` - The public key of the payer
/// * `lookup_tables` - The address lookup tables
/// * `keypairs` - The keypairs for the transaction
///
/// # Returns
/// The compute units consumed, or None if unsuccessful
pub async fn get_compute_units_thread_safe(
&self,
instructions: Vec<Instruction>,
payer: Pubkey,
lookup_tables: Vec<AddressLookupTableAccount>,
keypairs: Option<&[&Keypair]>,
) -> Result<Option<u64>> {
let test_instructions: Vec<Instruction> = vec![ComputeBudgetInstruction::set_compute_unit_limit(1_400_000)]
.into_iter()
.chain(instructions)
.collect::<Vec<_>>();
let recent_blockhash: Hash = self.connection().get_latest_blockhash()?;
let v0_message: v0::Message =
v0::Message::try_compile(&payer, &test_instructions, &lookup_tables, recent_blockhash)?;
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
let transaction: VersionedTransaction = if let Some(keypairs) = keypairs {
let mut tx = VersionedTransaction {
signatures: vec![Signature::default(); keypairs.len()],
message: versioned_message.clone(),
};
for (i, keypair) in keypairs.iter().enumerate() {
tx.signatures[i] = keypair.sign_message(&versioned_message.serialize());
}
tx
} else {
VersionedTransaction {
signatures: vec![],
message: versioned_message,
}
};
let config: RpcSimulateTransactionConfig = RpcSimulateTransactionConfig {
sig_verify: keypairs.is_some(),
..Default::default()
};
let result: Response<RpcSimulateTransactionResult> = self
.connection()
.simulate_transaction_with_config(&transaction, config)?;
Ok(result.value.units_consumed)
}
/// Creates a smart transaction using seed bytes for thread-safe transaction creation
///
/// Creates an optimized transaction using seed bytes instead of `Signers`` for thread-safe operations.
///
/// # Arguments
/// * `create_config` - Transaction configuration containing:
/// - `instructions`: Instructions to execute
/// - `signer_seeds`: Seed bytes for generating keypairs
/// - `fee_payer_seed`: Optional fee payer seed (defaults to first signer)
/// - `lookup_tables`: Optional address lookup tables for versioned transactions
/// - `priority_fee_cap`: Optional maximum priority fee
///
/// # Returns
/// A tuple containing:
/// - `SmartTransaction`: The created transaction (legacy or versioned)
/// - `u64`: Last valid block height for the transaction
///
/// # Errors
/// Returns `HeliusError` if:
/// - No signer seeds provided
/// - Failed to create keypairs from seeds
/// - Failed to get compute units
/// - Failed to estimate priority fees
/// - Transaction creation fails
pub async fn create_smart_transaction_with_seeds(
&self,
create_config: &CreateSmartTransactionSeedConfig,
) -> Result<(SmartTransaction, u64)> {
if create_config.signer_seeds.is_empty() {
return Err(HeliusError::InvalidInput(
"At least one signer seed must be provided".to_string(),
));
}
let keypairs: Vec<Keypair> = create_config
.signer_seeds
.iter()
.map(|seed| keypair_from_seed(seed).expect("Failed to create keypair from seed"))
.collect();
// Create the fee payer keypair if provided. Otherwise, we default to the first signer
let fee_payer: Keypair = if let Some(fee_payer_seed) = create_config.fee_payer_seed {
keypair_from_seed(&fee_payer_seed).expect("Failed to create keypair from seed")
} else {
Keypair::from_bytes(&keypairs[0].to_bytes()).unwrap()
};
let (recent_blockhash, last_valid_block_hash) = self
.connection()
.get_latest_blockhash_with_commitment(CommitmentConfig::confirmed())?;
let mut final_instructions: Vec<Instruction> = vec![];
// Get priority fee estimate
let transaction: Transaction = Transaction::new_signed_with_payer(
&create_config.instructions,
Some(&fee_payer.pubkey()),
&[&fee_payer],
recent_blockhash,
);
let serialized_tx: Vec<u8> = serialize(&transaction).map_err(|e| HeliusError::InvalidInput(e.to_string()))?;
let transaction_base58: String = encode(&serialized_tx).into_string();
let priority_fee_request: GetPriorityFeeEstimateRequest = GetPriorityFeeEstimateRequest {
transaction: Some(transaction_base58),
account_keys: None,
options: Some(GetPriorityFeeEstimateOptions {
recommended: Some(true),
..Default::default()
}),
};
let priority_fee_estimate: GetPriorityFeeEstimateResponse =
self.rpc().get_priority_fee_estimate(priority_fee_request).await?;
let priority_fee_recommendation: u64 =
priority_fee_estimate
.priority_fee_estimate
.ok_or(HeliusError::InvalidInput(
"Priority fee estimate not available".to_string(),
))? as u64;
let priority_fee: u64 = if let Some(provided_fee) = create_config.priority_fee_cap {
std::cmp::min(priority_fee_recommendation, provided_fee)
} else {
priority_fee_recommendation
};
// Add compute budget instructions
final_instructions.push(ComputeBudgetInstruction::set_compute_unit_price(priority_fee));
// Get optimal compute units
let mut test_instructions: Vec<Instruction> = final_instructions.clone();
test_instructions.extend(create_config.instructions.clone());
let units: Option<u64> = self
.get_compute_units_thread_safe(
test_instructions,
fee_payer.pubkey(),
create_config.lookup_tables.clone().unwrap_or_default(),
Some(&[&fee_payer]),
)
.await?;
let compute_units: u64 = units.ok_or(HeliusError::InvalidInput(
"Error fetching compute units for the instructions provided".to_string(),
))?;
let customers_cu: u32 = if compute_units < 1000 {
1000
} else {
(compute_units as f64 * 1.1).ceil() as u32
};
final_instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(customers_cu));
final_instructions.extend(create_config.instructions.clone());
// Create the final transaction
let transaction: SmartTransaction = if let Some(lookup_tables) = &create_config.lookup_tables {
let message: v0::Message = v0::Message::try_compile(
&fee_payer.pubkey(),
&final_instructions,
&lookup_tables,
recent_blockhash,
)?;
let versioned_message: VersionedMessage = VersionedMessage::V0(message);
let fee_payer_copy: Keypair = Keypair::from_bytes(&fee_payer.to_bytes()).unwrap();
let mut all_signers: Vec<Keypair> = vec![fee_payer_copy];
all_signers.extend(keypairs.into_iter().filter(|k| k.pubkey() != fee_payer.pubkey()));
let mut tx: VersionedTransaction = VersionedTransaction {
signatures: vec![Signature::default(); all_signers.len()],
message: versioned_message.clone(),
};
// Sign message with all keypairs
for (i, keypair) in all_signers.iter().enumerate() {
tx.signatures[i] = keypair.sign_message(&versioned_message.serialize());
}
SmartTransaction::Versioned(tx)
} else {
let mut tx: Transaction = Transaction::new_with_payer(&final_instructions, Some(&fee_payer.pubkey()));
let mut signers: Vec<&Keypair> = vec![&fee_payer];
signers.extend(keypairs.iter().filter(|k| k.pubkey() != fee_payer.pubkey()));
tx.sign(&signers, recent_blockhash);
SmartTransaction::Legacy(tx)
};
Ok((transaction, last_valid_block_hash))
}
/// Sends a smart transaction using seed bytes
///
/// This method allows for sending smart transactions in asynchronous contexts
/// where the Signer trait's lack of Send + Sync would otherwise cause issues.
/// It creates Keypairs from the provided seed bytes and uses them to sign the transaction.
///
/// # Arguments
///
/// * `create_config` - A `CreateSmartTransactionSeedConfig` containing:
/// - `instructions`: The instructions to be executed in the transaction.
/// - `signer_seeds`: Seed bytes for generating signer keypairs.
/// - `fee_payer_seed`: Optional seed bytes for generating the fee payer keypair.
/// - `lookup_tables`: Optional address lookup tables for the transaction.
/// * `send_options` - Optional `RpcSendTransactionConfig` for sending the transaction.
/// * `timeout` - Optional `Timeout` wait time for polling transaction confirmation.
///
/// # Returns
///
/// A `Result<Signature>` containing the transaction signature if successful, or an error if not.
///
/// # Errors
///
/// This function will return an error if keypair creation from seeds fails, the transaction sending fails,
/// or no signer seeds are provided
///
/// # Notes
///
/// If no `fee_payer_seed` is provided, the first signer (i.e., derived from the first seed in `signer_seeds`) will be used as the fee payer
pub async fn send_smart_transaction_with_seeds(
&self,
create_config: CreateSmartTransactionSeedConfig,
send_options: Option<RpcSendTransactionConfig>,
timeout: Option<Timeout>,
) -> Result<Signature> {
if create_config.signer_seeds.is_empty() {
return Err(HeliusError::InvalidInput(
"At least one signer seed required".to_string(),
));
}
let (transaction, last_valid_block_hash) = self.create_smart_transaction_with_seeds(&create_config).await?;
match transaction {
SmartTransaction::Legacy(tx) => {
self.send_and_confirm_transaction(
&tx,
send_options.unwrap_or_default(),
last_valid_block_hash,
Some(timeout.unwrap_or_default().into()),
)
.await
}
SmartTransaction::Versioned(tx) => {
self.send_and_confirm_transaction(
&tx,
send_options.unwrap_or_default(),
last_valid_block_hash,
Some(timeout.unwrap_or_default().into()),
)
.await
}
}
}
}