Skip to content
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

feat: support publish batch hook #5401

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion packages/transaction-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = merge(baseConfig, {
coverageThreshold: {
global: {
branches: 91.76,
functions: 93.52,
functions: 93.44,
lines: 96.83,
statements: 96.82,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { MethodDataHelper } from './helpers/MethodDataHelper';
import { MultichainTrackingHelper } from './helpers/MultichainTrackingHelper';
import { PendingTransactionTracker } from './helpers/PendingTransactionTracker';
import { shouldResimulate } from './helpers/ResimulateHelper';
import { ExtraTransactionsPublishHook } from './hooks/ExtraTransactionsPublishHook';
import type {
AllowedActions,
AllowedEvents,
Expand All @@ -66,6 +67,7 @@ import type {
GasFeeFlowResponse,
SubmitHistoryEntry,
InternalAccount,
PublishHook,
} from './types';
import {
GasFeeEstimateType,
Expand Down Expand Up @@ -103,6 +105,8 @@ type UnrestrictedMessenger = Messenger<

const MOCK_V1_UUID = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d';
const TRANSACTION_HASH_MOCK = '0x123456';
const DATA_MOCK = '0x12345678';
const VALUE_MOCK = '0xabcd';

jest.mock('@metamask/eth-query');
jest.mock('./api/accounts-api');
Expand All @@ -114,6 +118,7 @@ jest.mock('./helpers/IncomingTransactionHelper');
jest.mock('./helpers/MethodDataHelper');
jest.mock('./helpers/MultichainTrackingHelper');
jest.mock('./helpers/PendingTransactionTracker');
jest.mock('./hooks/ExtraTransactionsPublishHook');
jest.mock('./utils/batch');
jest.mock('./utils/gas');
jest.mock('./utils/gas-fees');
Expand Down Expand Up @@ -1578,6 +1583,7 @@ describe('TransactionController', () => {

const expectedInitialSnapshot = {
actionId: undefined,
batchId: undefined,
chainId: expect.any(String),
dappSuggestedGasFees: undefined,
deviceConfirmedOn: undefined,
Expand Down Expand Up @@ -2169,6 +2175,63 @@ describe('TransactionController', () => {
]);
});

it('uses extra transactions publish hook if batch transactions in metadata', async () => {
const { controller } = setupController({
messengerOptions: {
addTransactionApprovalRequest: {
state: 'approved',
},
},
});

const publishHook: jest.MockedFn<PublishHook> = jest.fn();

publishHook.mockResolvedValueOnce({
transactionHash: TRANSACTION_HASH_MOCK,
});

const extraTransactionsPublishHook = jest.mocked(
ExtraTransactionsPublishHook,
);

extraTransactionsPublishHook.mockReturnValue({
getHook: () => publishHook,
} as unknown as ExtraTransactionsPublishHook);

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

controller.updateBatchTransactions({
transactionId: transactionMeta.id,
batchTransactions: [
{ data: DATA_MOCK, to: ACCOUNT_2_MOCK, value: VALUE_MOCK },
],
});

result.catch(() => {
// Intentionally empty
});

await flushPromises();

expect(ExtraTransactionsPublishHook).toHaveBeenCalledTimes(1);
expect(ExtraTransactionsPublishHook).toHaveBeenCalledWith({
addTransactionBatch: expect.any(Function),
transactions: [
{ data: DATA_MOCK, to: ACCOUNT_2_MOCK, value: VALUE_MOCK },
],
});

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

describe('fails', () => {
/**
* Test template to assert adding and submitting a transaction fails.
Expand Down Expand Up @@ -5088,6 +5151,42 @@ describe('TransactionController', () => {
expect.any(String),
);
});

it('supports publish hook override per call', async () => {
const publishHookController = jest.fn();

const publishHookCall = jest.fn().mockResolvedValueOnce({
transactionHash: TRANSACTION_HASH_MOCK,
});

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

jest.spyOn(mockEthQuery, 'sendRawTransaction');

const { result } = await controller.addTransaction(paramsMock, {
networkClientId: NETWORK_CLIENT_ID_MOCK,
publishHook: publishHookCall,
});

await result;

expect(controller.state.transactions[0].hash).toBe(TRANSACTION_HASH_MOCK);

expect(publishHookCall).toHaveBeenCalledTimes(1);
expect(publishHookController).not.toHaveBeenCalled();
expect(mockEthQuery.sendRawTransaction).not.toHaveBeenCalled();
});
});

describe('updateSecurityAlertResponse', () => {
Expand Down
Loading
Loading