@@ -207,6 +207,18 @@ export async function sendTransactionWithRetry(
207
207
transaction . sign ( ...signers ) ;
208
208
}
209
209
210
+ if ( transaction . recentBlockhash === undefined ) {
211
+ console . log ( "No blockhash provided. Setting recent blockhash" ) ;
212
+ const { blockhash } = await connection . getLatestBlockhash ( commitment ) ;
213
+ transaction . recentBlockhash = blockhash ;
214
+ }
215
+ if ( transaction . feePayer === undefined ) {
216
+ if ( signers . length === 0 ) {
217
+ throw new Error ( "No signers or fee payer provided" ) ;
218
+ }
219
+ transaction . feePayer = signers [ 0 ] . publicKey ;
220
+ }
221
+
210
222
onStatusUpdate ?.( { status : "signed" } ) ;
211
223
212
224
let signature : string | null = null ;
@@ -624,3 +636,72 @@ function formatData(data: any): any {
624
636
}
625
637
return data ;
626
638
}
639
+
640
+ /**
641
+ * Sends a transaction with compute unit optimization and automatic retries
642
+ *
643
+ * @param connection - The Solana connection object
644
+ * @param transaction - The transaction to send
645
+ * @param signers - Array of signers needed for the transaction
646
+ * @param priorityFee - Priority fee in microLamports
647
+ * @param options - Optional configuration for retry mechanism and compute units
648
+ * @returns Promise that resolves to the transaction signature
649
+ *
650
+ * @example
651
+ * ```typescript
652
+ * const signature = await sendTransactionWithRetryAndPriorityFees(
653
+ * connection,
654
+ * transaction,
655
+ * [payer],
656
+ * 1000,
657
+ * {
658
+ * computeUnitBuffer: { multiplier: 1.1 },
659
+ * onStatusUpdate: (status) => console.log(status),
660
+ * }
661
+ * );
662
+ * ```
663
+ */
664
+ export async function sendTransactionWithRetryAndPriorityFees (
665
+ connection : Connection ,
666
+ transaction : Transaction ,
667
+ signers : Keypair [ ] ,
668
+ priorityFee : number = 10000 ,
669
+ options : SendTransactionOptions & {
670
+ computeUnitBuffer ?: ComputeUnitBuffer ;
671
+ } = { } ,
672
+ ) : Promise < string > {
673
+ const {
674
+ computeUnitBuffer : userComputeBuffer , // Rename to make clear it's user provided
675
+ commitment = "confirmed" ,
676
+ ...sendOptions
677
+ } = options ;
678
+
679
+ // Use user provided buffer or default to 1.1 multiplier
680
+ const computeUnitBuffer = userComputeBuffer ?? { multiplier : 1.1 } ;
681
+
682
+ if ( transaction . recentBlockhash === undefined ) {
683
+ console . log ( "No blockhash provided. Setting recent blockhash" ) ;
684
+ const { blockhash } = await connection . getLatestBlockhash ( commitment ) ;
685
+ transaction . recentBlockhash = blockhash ;
686
+ }
687
+ if ( transaction . feePayer === undefined ) {
688
+ if ( signers . length === 0 ) {
689
+ throw new Error ( "No signers or fee payer provided" ) ;
690
+ }
691
+ transaction . feePayer = signers [ 0 ] . publicKey ;
692
+ }
693
+
694
+ await prepareTransactionWithCompute (
695
+ connection ,
696
+ transaction ,
697
+ transaction . feePayer ,
698
+ priorityFee ,
699
+ computeUnitBuffer ,
700
+ commitment ,
701
+ ) ;
702
+
703
+ return sendTransactionWithRetry ( connection , transaction , signers , {
704
+ commitment,
705
+ ...sendOptions ,
706
+ } ) ;
707
+ }
0 commit comments