88 type EngineErr ,
99} from "../../lib/errors" ;
1010import { execute as executeExternalBundler } from "../external-bundler" ;
11+ import { execute as executeExternalBundlerAsync } from "../external-bundler-async" ;
1112import { getChain } from "../../lib/chain" ;
1213import {
1314 getContract ,
@@ -32,11 +33,13 @@ import SuperJSON from "superjson";
3233import type {
3334 ExecutionParamsSerialized ,
3435 TransactionParamsSerialized ,
36+ ExecutionResult4337Serialized ,
3537} from "../../db/types" ;
3638import "./external-bundler-confirm-handler" ;
39+ import "./external-bundler-send-handler" ;
3740
3841function getExecutionAccountFromRequest (
39- request : EncodedExecutionRequest ,
42+ request : EncodedExecutionRequest
4043) : ResultAsync <
4144 | {
4245 signerAccount : Account ;
@@ -57,7 +60,7 @@ function getExecutionAccountFromRequest(
5760 yield * getEngineAccount ( {
5861 address : request . from ,
5962 encryptionPassword : request . encryptionPassword ,
60- } ) ,
63+ } )
6164 ) ;
6265 }
6366
@@ -241,7 +244,7 @@ export function execute({
241244 const chain = await getChain ( Number . parseInt ( request . chainId ) ) ;
242245
243246 const engineAccountResponse = yield * getExecutionAccountFromRequest (
244- request ,
247+ request
245248 ) ;
246249
247250 if ( "account" in engineAccountResponse ) {
@@ -278,26 +281,74 @@ export function execute({
278281
279282 const idempotencyKey = request . idempotencyKey ?? randomUUID ( ) . toString ( ) ;
280283
281- const executionResult = await executeExternalBundler ( {
282- id : idempotencyKey ,
283- chain,
284- client,
285- executionOptions,
286- transactionParams : resolvedTransactionParams ,
287- } ) ;
284+ // Determine which executor to use based on request parameters
285+ // Currently, we use the async executor when no encryption password is provided
286+ // In the future, this can be expanded to support more executor types
287+ const executorType = ! request . encryptionPassword ? "async" : "sync" ;
288+
289+ // Variables to track execution state
290+ let executionResult : ExecutionResult4337Serialized ;
291+ let userOpHash : string | undefined ;
292+
293+ // Execute using the appropriate executor
294+ if ( executorType === "async" ) {
295+ // For async executor, we need to convert the execution options to the format it expects
296+ const asyncExecutionOptions = {
297+ signerAddress : executionOptions . signer . address as Address ,
298+ entrypointAddress : executionOptions . entrypointAddress ,
299+ accountFactoryAddress : executionOptions . accountFactoryAddress ,
300+ sponsorGas : executionOptions . sponsorGas ,
301+ smartAccountAddress : executionOptions . smartAccountAddress ,
302+ accountSalt : executionOptions . accountSalt ,
303+ } ;
304+
305+ // Execute using the async executor
306+ const asyncResult = await executeExternalBundlerAsync ( {
307+ id : idempotencyKey ,
308+ executionOptions : asyncExecutionOptions ,
309+ chainId : request . chainId ,
310+ transactionParams : resolvedTransactionParams ,
311+ } ) ;
312+
313+ // Handle errors from the async executor
314+ if ( asyncResult . isErr ( ) ) {
315+ return errAsync ( asyncResult . error ) ;
316+ }
288317
289- let userOpHash : Hex ;
290- let didConfirmationQueueJobError = false ;
318+ // Async executor doesn't return userOpHash immediately, it's queued
319+ executionResult = {
320+ status : "QUEUED" ,
321+ } ;
322+ } else {
323+ // Execute using the sync executor
324+ const syncResult = await executeExternalBundler ( {
325+ id : idempotencyKey ,
326+ chain,
327+ client,
328+ executionOptions,
329+ transactionParams : resolvedTransactionParams ,
330+ } ) ;
291331
292- if ( executionResult . isErr ( ) ) {
293- if ( executionResult . error . kind === "queue" ) {
294- didConfirmationQueueJobError = true ;
295- userOpHash = executionResult . error . userOpHash ;
332+ // Handle errors from the sync executor
333+ if ( syncResult . isErr ( ) ) {
334+ if ( syncResult . error . kind === "queue" ) {
335+ userOpHash = syncResult . error . userOpHash ;
336+ executionResult = {
337+ status : "SUBMITTED" ,
338+ monitoringStatus : "CANNOT_MONITOR" ,
339+ userOpHash,
340+ } ;
341+ } else {
342+ return errAsync ( syncResult . error ) ;
343+ }
296344 } else {
297- return errAsync ( executionResult . error ) ;
345+ userOpHash = syncResult . value . data . userOpHash ;
346+ executionResult = {
347+ status : "SUBMITTED" ,
348+ monitoringStatus : "WILL_MONITOR" ,
349+ userOpHash,
350+ } ;
298351 }
299- } else {
300- userOpHash = executionResult . value . data . userOpHash ;
301352 }
302353
303354 const executionParams : ExecutionParamsSerialized = {
@@ -307,13 +358,9 @@ export function execute({
307358 signerAddress : executionOptions . signer . address ,
308359 } ;
309360
310- const executionResultSerialized = {
311- status : "SUBMITTED" as const ,
312- monitoringStatus : didConfirmationQueueJobError
313- ? "CANNOT_MONITOR"
314- : "WILL_MONITOR" ,
315- userOpHash,
316- } as const ;
361+ // For database insertion, we need to ensure userOpHash is a string if status is SUBMITTED
362+ // If it's undefined, we'll use an empty string
363+ const dbExecutionResult = executionResult ;
317364
318365 const dbTransactionEntry = yield * ResultAsync . fromPromise (
319366 db
@@ -325,23 +372,23 @@ export function execute({
325372 transactionParams : SuperJSON . serialize ( resolvedTransactionParams )
326373 . json as TransactionParamsSerialized [ ] ,
327374 executionParams,
328- executionResult : executionResultSerialized ,
375+ executionResult : dbExecutionResult ,
329376 from : executionParams . smartAccountAddress as Address ,
330377 } )
331378 . returning ( ) ,
332- mapDbError ,
379+ mapDbError
333380 ) . mapErr ( ( e ) =>
334381 buildTransactionDbEntryErr ( {
335382 error : e ,
336383 executionParams,
337- executionResult : executionResultSerialized ,
338- } ) ,
384+ executionResult : dbExecutionResult ,
385+ } )
339386 ) ;
340387
341388 return okAsync ( {
342- executionResult : executionResultSerialized ,
389+ executionResult,
343390 transactions : dbTransactionEntry ,
344391 executionOptions,
345392 } ) ;
346393 } ) ;
347- }
394+ }
0 commit comments