Skip to content

Commit d29c6d6

Browse files
committed
Add beforeSign hook
1 parent 7c7632b commit d29c6d6

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

packages/transaction-controller/src/TransactionController.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import type {
117117
AfterAddHook,
118118
GasFeeEstimateLevel as GasFeeEstimateLevelType,
119119
TransactionBatchMeta,
120+
AfterSimulateHook,
120121
} from './types';
121122
import {
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({

packages/transaction-controller/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export {
3232
} from './TransactionController';
3333
export type {
3434
AfterAddHook,
35+
AfterSimulateHook,
3536
Authorization,
3637
AuthorizationList,
3738
BatchTransactionParams,

packages/transaction-controller/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,3 +1817,13 @@ export type AfterAddHook = (request: {
18171817
}) => Promise<{
18181818
updateTransaction?: (transaction: TransactionMeta) => void;
18191819
}>;
1820+
1821+
/**
1822+
* Custom logic to be executed after a transaction is simulated.
1823+
* Can optionally update the transaction by returning the `updateTransaction` callback.
1824+
*/
1825+
export type AfterSimulateHook = (request: {
1826+
transactionMeta: TransactionMeta;
1827+
}) => Promise<{
1828+
updateTransaction?: (transaction: TransactionMeta) => void;
1829+
}>;

0 commit comments

Comments
 (0)