Skip to content

feat: after simulate hook #5503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions packages/transaction-controller/src/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import type {
TransactionBatchRequest,
TransactionBatchResult,
BatchTransactionParams,
AfterSimulateHook,
} from './types';
import {
TransactionEnvelopeType,
Expand Down Expand Up @@ -350,6 +351,8 @@ export type TransactionControllerOptions = {
signedTx: TypedTransaction,
) => boolean;

afterSimulate?: AfterSimulateHook;

/**
* Additional logic to execute before checking pending transactions.
* Return false to prevent the broadcast of the transaction.
Expand Down Expand Up @@ -706,6 +709,8 @@ export class TransactionController extends BaseController<

private readonly beforePublish: (transactionMeta: TransactionMeta) => boolean;

readonly #afterSimulate: AfterSimulateHook;

private readonly publish: (
transactionMeta: TransactionMeta,
rawTx: string,
Expand Down Expand Up @@ -850,6 +855,8 @@ export class TransactionController extends BaseController<
this.#trace = trace ?? (((_request, fn) => fn?.()) as TraceCallback);

this.afterSign = hooks?.afterSign ?? (() => true);
this.#afterSimulate =
hooks?.afterSimulate ?? (() => Promise.resolve(undefined));
this.beforeCheckPendingTransaction =
hooks?.beforeCheckPendingTransaction ??
/* istanbul ignore next */
Expand Down Expand Up @@ -1227,6 +1234,7 @@ export class TransactionController extends BaseController<

if (requireApproval !== false) {
this.#updateSimulationData(addedTransactionMeta, {
ethQuery,
traceContext,
}).catch((error) => {
log('Error while updating simulation data', error);
Expand Down Expand Up @@ -3811,9 +3819,11 @@ export class TransactionController extends BaseController<
transactionMeta: TransactionMeta,
{
blockTime,
ethQuery,
traceContext,
}: {
blockTime?: number;
ethQuery?: EthQuery;
traceContext?: TraceContext;
} = {},
) {
Expand Down Expand Up @@ -3864,7 +3874,7 @@ export class TransactionController extends BaseController<
}
}

const finalTransactionMeta = this.getTransaction(transactionId);
let finalTransactionMeta = this.getTransaction(transactionId);

/* istanbul ignore if */
if (!finalTransactionMeta) {
Expand All @@ -3877,7 +3887,7 @@ export class TransactionController extends BaseController<
return;
}

this.#updateTransactionInternal(
finalTransactionMeta = this.#updateTransactionInternal(
{
transactionId,
note: 'TransactionController#updateSimulationData - Update simulation data',
Expand All @@ -3888,7 +3898,38 @@ export class TransactionController extends BaseController<
},
);

log('Updated simulation data', transactionId, simulationData);
if (ethQuery) {
log('Calling afterSimulate hook', finalTransactionMeta);

const result = await this.#afterSimulate({
transactionMeta: finalTransactionMeta,
});

if (result?.updateTransaction) {
log('Updating transaction with afterSimulate data');

finalTransactionMeta = this.#updateTransactionInternal(
{ transactionId },
result.updateTransaction,
);

const { estimatedGas } = await estimateGas(
{ ...finalTransactionMeta.txParams, gas: undefined },
ethQuery,
);

log('Updating gas following afterSimulate hook', estimatedGas);

finalTransactionMeta = this.#updateTransactionInternal(
{ transactionId },
(txMeta) => {
txMeta.txParams.gas = estimatedGas;
},
);
}
}

log('Updated simulation data', transactionId, finalTransactionMeta);
}

#onGasFeePollerTransactionUpdate({
Expand Down
1 change: 1 addition & 0 deletions packages/transaction-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {
TransactionController,
} from './TransactionController';
export type {
AfterSimulateHook,
Authorization,
AuthorizationList,
BatchTransactionParams,
Expand Down
9 changes: 9 additions & 0 deletions packages/transaction-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1485,3 +1485,12 @@ export type TransactionBatchResult = {
/** ID of the batch to locate related transactions. */
batchId: Hex;
};

export type AfterSimulateHook = (request: {
transactionMeta: TransactionMeta;
}) => Promise<
| {
updateTransaction?: (transactionMeta: TransactionMeta) => void;
}
| undefined
>;
4 changes: 2 additions & 2 deletions packages/transaction-controller/src/utils/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { isValidSignature } from './signature';
import { projectLogger } from '../logger';
import type { TransactionControllerMessenger } from '../TransactionController';

export const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';
export const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';
export const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';
export const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';

const DEFAULT_BATCH_SIZE_LIMIT = 10;

Expand Down
Loading