@@ -117,6 +117,7 @@ import type {
117117 AfterAddHook ,
118118 GasFeeEstimateLevel as GasFeeEstimateLevelType ,
119119 TransactionBatchMeta ,
120+ AfterSimulateHook ,
120121} from './types' ;
121122import {
122123 GasFeeEstimateLevel ,
@@ -414,6 +415,9 @@ export type TransactionControllerOptions = {
414415 signedTx : TypedTransaction ,
415416 ) => boolean ;
416417
418+ /** Additional logic to execute after simulating a transaction. */
419+ afterSimulate ?: AfterSimulateHook ;
420+
417421 /**
418422 * Additional logic to execute before checking pending transactions.
419423 * Return false to prevent the broadcast of the transaction.
@@ -701,6 +705,8 @@ export class TransactionController extends BaseController<
701705 signedTx : TypedTransaction ,
702706 ) => boolean ;
703707
708+ readonly #afterSimulate: AfterSimulateHook ;
709+
704710 readonly #approvingTransactionIds: Set < string > = new Set ( ) ;
705711
706712 readonly #beforeCheckPendingTransaction: (
@@ -846,6 +852,7 @@ export class TransactionController extends BaseController<
846852
847853 this . #afterAdd = hooks ?. afterAdd ?? ( ( ) => Promise . resolve ( { } ) ) ;
848854 this . #afterSign = hooks ?. afterSign ?? ( ( ) => true ) ;
855+ this . #afterSimulate = hooks ?. afterSimulate ?? ( ( ) => Promise . resolve ( { } ) ) ;
849856 this . #beforeCheckPendingTransaction =
850857 /* istanbul ignore next */
851858 hooks ?. beforeCheckPendingTransaction ?? ( ( ) => Promise . resolve ( true ) ) ;
@@ -1305,6 +1312,7 @@ export class TransactionController extends BaseController<
13051312
13061313 if ( requireApproval !== false ) {
13071314 this . #updateSimulationData( addedTransactionMeta , {
1315+ ethQuery,
13081316 traceContext,
13091317 } ) . catch ( ( error ) => {
13101318 log ( 'Error while updating simulation data' , error ) ;
@@ -4051,9 +4059,11 @@ export class TransactionController extends BaseController<
40514059 transactionMeta : TransactionMeta ,
40524060 {
40534061 blockTime,
4062+ ethQuery,
40544063 traceContext,
40554064 } : {
40564065 blockTime ?: number ;
4066+ ethQuery ?: EthQuery ;
40574067 traceContext ?: TraceContext ;
40584068 } = { } ,
40594069 ) {
@@ -4092,7 +4102,7 @@ export class TransactionController extends BaseController<
40924102 }
40934103 }
40944104
4095- const finalTransactionMeta = this . #getTransaction( transactionId ) ;
4105+ let finalTransactionMeta = this . #getTransaction( transactionId ) ;
40964106
40974107 /* istanbul ignore if */
40984108 if ( ! finalTransactionMeta ) {
@@ -4105,7 +4115,7 @@ export class TransactionController extends BaseController<
41054115 return ;
41064116 }
41074117
4108- this . #updateTransactionInternal(
4118+ finalTransactionMeta = this . #updateTransactionInternal(
41094119 {
41104120 transactionId,
41114121 note : 'TransactionController#updateSimulationData - Update simulation data' ,
@@ -4117,7 +4127,39 @@ export class TransactionController extends BaseController<
41174127 } ,
41184128 ) ;
41194129
4120- log ( 'Updated simulation data' , transactionId , simulationData ) ;
4130+ log ( 'Calling afterSimulate hook' , finalTransactionMeta ) ;
4131+
4132+ if ( ethQuery ) {
4133+ const result = await this . #afterSimulate( {
4134+ transactionMeta : finalTransactionMeta ,
4135+ } ) ;
4136+
4137+ if ( result . updateTransaction ) {
4138+ log ( 'Updating transaction with afterSimulate data' ) ;
4139+
4140+ finalTransactionMeta = this . #updateTransactionInternal(
4141+ { transactionId } ,
4142+ result . updateTransaction ,
4143+ ) ;
4144+
4145+ const { estimatedGas } = await estimateGas ( {
4146+ chainId : finalTransactionMeta . chainId ,
4147+ ethQuery,
4148+ isSimulationEnabled : this . #isSimulationEnabled( ) ,
4149+ messenger : this . messagingSystem ,
4150+ txParams : { ...finalTransactionMeta . txParams , gas : undefined } ,
4151+ } ) ;
4152+
4153+ finalTransactionMeta = this . #updateTransactionInternal(
4154+ { transactionId } ,
4155+ ( txMeta ) => {
4156+ txMeta . txParams . gas = estimatedGas ;
4157+ } ,
4158+ ) ;
4159+ }
4160+ }
4161+
4162+ log ( 'Updated simulation data' , transactionId , finalTransactionMeta ) ;
41214163 }
41224164
41234165 #onGasFeePollerTransactionUpdate( {
0 commit comments