Skip to content

feat: after simulate and before sign hooks #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

Merged
merged 8 commits into from
Jun 12, 2025
Merged
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
7 changes: 7 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add optional `afterSimulate` and `beforeSign` hooks to constructor ([#5503](https://github.com/MetaMask/core/pull/5503))
- Add `AfterSimulateHook` type.
- Add `BeforeSignHook` type.
- Add `TransactionContainerType` enum.
- Add `TransactionControllerEstimateGasAction` type.
- Add optional `containerTypes` property to `TransactionMeta`.
- Add optional `ignoreDelegationSignatures` boolean to `estimateGas` method.
- Add `gasFeeEstimates` property to `TransactionBatchMeta`, populated using `DefaultGasFeeFlow` ([#5886](https://github.com/MetaMask/core/pull/5886))

## [57.3.0]
Expand Down
233 changes: 233 additions & 0 deletions packages/transaction-controller/src/TransactionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,239 @@ describe('TransactionController', () => {
});
});

describe('with afterSimulate hook', () => {
it('calls afterSimulate hook', async () => {
const afterSimulateHook = jest.fn().mockResolvedValueOnce({});

const { controller } = setupController({
options: {
hooks: {
afterSimulate: afterSimulateHook,
},
},
});

await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

expect(afterSimulateHook).toHaveBeenCalledTimes(1);
});

it('updates transaction if update callback returned', async () => {
const updateTransactionMock = jest.fn();

const afterSimulateHook = jest
.fn()
.mockResolvedValueOnce({ updateTransaction: updateTransactionMock });

const { controller } = setupController({
options: {
hooks: {
afterSimulate: afterSimulateHook,
},
},
});

await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

expect(updateTransactionMock).toHaveBeenCalledTimes(1);
});

it('saves original transaction params if update callback returned', async () => {
const updateTransactionMock = jest.fn();

const afterSimulateHook = jest
.fn()
.mockResolvedValueOnce({ updateTransaction: updateTransactionMock });

const { controller } = setupController({
options: {
hooks: {
afterSimulate: afterSimulateHook,
},
},
});

await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

expect(controller.state.transactions[0].txParamsOriginal).toStrictEqual(
expect.objectContaining({
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
}),
);
});

it('will re-simulate balance changes if hook returns skipSimulation as false', async () => {
const afterSimulateHook = jest
.fn()
.mockResolvedValue({ skipSimulation: false });

const { controller } = setupController({
options: {
hooks: {
afterSimulate: afterSimulateHook,
},
},
});

const { transactionMeta } = await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

shouldResimulateMock.mockReturnValue({
blockTime: 123,
resimulate: true,
});

await controller.updateEditableParams(transactionMeta.id, {});

expect(getBalanceChangesMock).toHaveBeenCalledTimes(2);
});

it('will not re-simulate balance changes if hook returns skipSimulation as true', async () => {
const afterSimulateHook = jest
.fn()
.mockResolvedValue({ skipSimulation: true });

const { controller } = setupController({
options: {
hooks: {
afterSimulate: afterSimulateHook,
},
},
});

const { transactionMeta } = await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

shouldResimulateMock.mockReturnValue({
blockTime: 123,
resimulate: true,
});

await controller.updateEditableParams(transactionMeta.id, {});

await flushPromises();

expect(getBalanceChangesMock).toHaveBeenCalledTimes(1);
});
});

describe('with beforeSign hook', () => {
it('calls beforeSign hook', async () => {
const beforeSignHook = jest.fn().mockResolvedValueOnce({});

const { controller } = setupController({
messengerOptions: {
addTransactionApprovalRequest: {
state: 'approved',
},
},
options: {
hooks: {
beforeSign: beforeSignHook,
},
},
});

await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

expect(beforeSignHook).toHaveBeenCalledTimes(1);
});

it('updates transaction if update callback returned', async () => {
const updateTransactionMock = jest.fn();

const beforeSignHook = jest
.fn()
.mockResolvedValueOnce({ updateTransaction: updateTransactionMock });

const { controller } = setupController({
messengerOptions: {
addTransactionApprovalRequest: {
state: 'approved',
},
},
options: {
hooks: {
beforeSign: beforeSignHook,
},
},
});

await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
networkClientId: NETWORK_CLIENT_ID_MOCK,
},
);

await flushPromises();

expect(updateTransactionMock).toHaveBeenCalledTimes(1);
});
});

describe('updates simulation data', () => {
it('by default', async () => {
getBalanceChangesMock.mockResolvedValueOnce(
Expand Down
Loading
Loading