@@ -38,7 +38,7 @@ interface StepExecutorOptions {
3838 /** Prepared quote returned by Bridge.prepare */
3939 request : BridgePrepareRequest ;
4040 /** Wallet instance providing getAccount() & sendTransaction */
41- wallet : Wallet ;
41+ wallet ? : Wallet ;
4242 /** Window adapter for opening on-ramp URLs (web / RN) */
4343 windowAdapter : WindowAdapter ;
4444 /** Thirdweb client for API calls */
@@ -417,89 +417,98 @@ export function useStepExecutor(
417417 ) ;
418418 }
419419
420- // Then execute transactions
421- const account = wallet . getAccount ( ) ;
422- if ( ! account ) {
423- throw new ApiError ( {
424- code : "INVALID_INPUT" ,
425- message : "Wallet not connected" ,
426- statusCode : 400 ,
427- } ) ;
428- }
429-
430- // Start from where we left off, or from the beginning
431- const startIndex = currentTxIndex ?? 0 ;
432-
433- for ( let i = startIndex ; i < flatTxs . length ; i ++ ) {
434- if ( abortController . signal . aborted ) {
435- break ;
420+ if ( flatTxs . length > 0 ) {
421+ // Then execute transactions
422+ if ( ! wallet ) {
423+ throw new ApiError ( {
424+ code : "INVALID_INPUT" ,
425+ message : "No wallet provided to execute transactions" ,
426+ statusCode : 400 ,
427+ } ) ;
436428 }
437-
438- const currentTx = flatTxs [ i ] ;
439- if ( ! currentTx ) {
440- continue ; // Skip invalid index
429+ const account = wallet . getAccount ( ) ;
430+ if ( ! account ) {
431+ throw new ApiError ( {
432+ code : "INVALID_INPUT" ,
433+ message : "Wallet not connected" ,
434+ statusCode : 400 ,
435+ } ) ;
441436 }
442437
443- setCurrentTxIndex ( i ) ;
444- const currentStepData = preparedQuote . steps [ currentTx . _stepIndex ] ;
445- if ( ! currentStepData ) {
446- throw new Error ( `Invalid step index: ${ currentTx . _stepIndex } ` ) ;
447- }
438+ // Start from where we left off, or from the beginning
439+ const startIndex = currentTxIndex ?? 0 ;
448440
449- // switch chain if needed
450- if ( currentTx . chainId !== wallet . getChain ( ) ?. id ) {
451- await wallet . switchChain ( getCachedChain ( currentTx . chainId ) ) ;
452- }
441+ for ( let i = startIndex ; i < flatTxs . length ; i ++ ) {
442+ if ( abortController . signal . aborted ) {
443+ break ;
444+ }
453445
454- // Check if we can batch transactions
455- const canBatch =
456- account . sendBatchTransaction !== undefined && i < flatTxs . length - 1 ; // Not the last transaction
457-
458- if ( canBatch ) {
459- // Find consecutive transactions on the same chain
460- const batchTxs : FlattenedTx [ ] = [ currentTx ] ;
461- let j = i + 1 ;
462- while ( j < flatTxs . length ) {
463- const nextTx = flatTxs [ j ] ;
464- if ( ! nextTx || nextTx . chainId !== currentTx . chainId ) {
465- break ;
466- }
467- batchTxs . push ( nextTx ) ;
468- j ++ ;
446+ const currentTx = flatTxs [ i ] ;
447+ if ( ! currentTx ) {
448+ continue ; // Skip invalid index
469449 }
470450
471- // Execute batch if we have multiple transactions
472- if ( batchTxs . length > 1 ) {
473- await executeBatch (
474- batchTxs ,
475- account ,
476- completedStatusResults ,
477- abortController . signal ,
478- ) ;
479-
480- // Mark all batched transactions as completed
481- for ( const tx of batchTxs ) {
482- setCompletedTxs ( ( prev ) => new Set ( prev ) . add ( tx . _index ) ) ;
451+ setCurrentTxIndex ( i ) ;
452+ const currentStepData = preparedQuote . steps [ currentTx . _stepIndex ] ;
453+ if ( ! currentStepData ) {
454+ throw new Error ( `Invalid step index: ${ currentTx . _stepIndex } ` ) ;
455+ }
456+
457+ // switch chain if needed
458+ if ( currentTx . chainId !== wallet . getChain ( ) ?. id ) {
459+ await wallet . switchChain ( getCachedChain ( currentTx . chainId ) ) ;
460+ }
461+
462+ // Check if we can batch transactions
463+ const canBatch =
464+ account . sendBatchTransaction !== undefined &&
465+ i < flatTxs . length - 1 ; // Not the last transaction
466+
467+ if ( canBatch ) {
468+ // Find consecutive transactions on the same chain
469+ const batchTxs : FlattenedTx [ ] = [ currentTx ] ;
470+ let j = i + 1 ;
471+ while ( j < flatTxs . length ) {
472+ const nextTx = flatTxs [ j ] ;
473+ if ( ! nextTx || nextTx . chainId !== currentTx . chainId ) {
474+ break ;
475+ }
476+ batchTxs . push ( nextTx ) ;
477+ j ++ ;
483478 }
484479
485- // Skip ahead
486- i = j - 1 ;
487- continue ;
480+ // Execute batch if we have multiple transactions
481+ if ( batchTxs . length > 1 ) {
482+ await executeBatch (
483+ batchTxs ,
484+ account ,
485+ completedStatusResults ,
486+ abortController . signal ,
487+ ) ;
488+
489+ // Mark all batched transactions as completed
490+ for ( const tx of batchTxs ) {
491+ setCompletedTxs ( ( prev ) => new Set ( prev ) . add ( tx . _index ) ) ;
492+ }
493+
494+ // Skip ahead
495+ i = j - 1 ;
496+ continue ;
497+ }
488498 }
489- }
490499
491- // Execute single transaction
492- await executeSingleTx (
493- currentTx ,
494- account ,
495- completedStatusResults ,
496- abortController . signal ,
497- ) ;
500+ // Execute single transaction
501+ await executeSingleTx (
502+ currentTx ,
503+ account ,
504+ completedStatusResults ,
505+ abortController . signal ,
506+ ) ;
498507
499- // Mark transaction as completed
500- setCompletedTxs ( ( prev ) => new Set ( prev ) . add ( currentTx . _index ) ) ;
508+ // Mark transaction as completed
509+ setCompletedTxs ( ( prev ) => new Set ( prev ) . add ( currentTx . _index ) ) ;
510+ }
501511 }
502-
503512 // All done - check if we actually completed everything
504513 if ( ! abortController . signal . aborted ) {
505514 setCurrentTxIndex ( undefined ) ;
0 commit comments