Skip to content

fix: preserve transaction type of batched transactions #6056

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add fallback to the sequential hook when `publishBatchHook` returns empty ([#6063](https://github.com/MetaMask/core/pull/6063))

### Fixed

- Preserve provided `type` in `transactions` when calling `addTransactionBatch` ([#6056](https://github.com/MetaMask/core/pull/6056))

## [58.1.1]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import type {
AfterSimulateHook,
BeforeSignHook,
TransactionContainerType,
NestedTransactionMetadata,
} from './types';
import {
GasFeeEstimateLevel,
Expand Down Expand Up @@ -1132,7 +1133,7 @@ export class TransactionController extends BaseController<
deviceConfirmedOn?: WalletDevice;
disableGasBuffer?: boolean;
method?: string;
nestedTransactions?: BatchTransactionParams[];
nestedTransactions?: NestedTransactionMetadata[];
networkClientId: NetworkClientId;
origin?: string;
publishHook?: PublishHook;
Expand Down
3 changes: 3 additions & 0 deletions packages/transaction-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,9 @@ export type PublishBatchHookTransaction = {

/** Signed transaction data to publish. */
signedTx: Hex;

/** Type of the nested transaction. */
type?: TransactionType;
Copy link
Member

@matthewwalsh0 matthewwalsh0 Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the type here?

The intent of providing the id is so the hook can require any specific transaction metadata it needs via the transaction controller state and that reference.

};

/**
Expand Down
129 changes: 129 additions & 0 deletions packages/transaction-controller/src/utils/batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,52 @@ describe('Batch Utils', () => {
expect(result.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
});

it('preserves nested transaction types when disable7702 is false', async () => {
const publishBatchHook: jest.MockedFn<PublishBatchHook> = jest.fn();

isAccountUpgradedToEIP7702Mock.mockResolvedValueOnce({
delegationAddress: undefined,
isSupported: true,
});

addTransactionMock.mockResolvedValueOnce({
transactionMeta: TRANSACTION_META_MOCK,
result: Promise.resolve(''),
});

const result = await addTransactionBatch({
...request,
publishBatchHook,
request: {
...request.request,
transactions: [
{
...request.request.transactions[0],
type: TransactionType.swapApproval,
},
{
...request.request.transactions[1],
type: TransactionType.bridgeApproval,
},
],
disable7702: false,
},
});

expect(result.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
expect(addTransactionMock).toHaveBeenCalledTimes(1);
expect(addTransactionMock.mock.calls[0][1].type).toStrictEqual(
TransactionType.batch,
);

expect(
addTransactionMock.mock.calls[0][1].nestedTransactions?.[0].type,
).toBe(TransactionType.swapApproval);
expect(
addTransactionMock.mock.calls[0][1].nestedTransactions?.[1].type,
).toBe(TransactionType.bridgeApproval);
});

it('returns provided batch ID', async () => {
isAccountUpgradedToEIP7702Mock.mockResolvedValueOnce({
delegationAddress: undefined,
Expand Down Expand Up @@ -838,6 +884,89 @@ describe('Batch Utils', () => {
);
});

it('calls publish batch hook with requested transaction type', async () => {
const publishBatchHook: jest.MockedFn<PublishBatchHook> = jest.fn();

addTransactionMock
.mockResolvedValueOnce({
transactionMeta: {
...TRANSACTION_META_MOCK,
id: TRANSACTION_ID_MOCK,
},
result: Promise.resolve(''),
})
.mockResolvedValueOnce({
transactionMeta: {
...TRANSACTION_META_MOCK,
id: TRANSACTION_ID_2_MOCK,
},
result: Promise.resolve(''),
});

publishBatchHook.mockResolvedValue({
results: [
{
transactionHash: TRANSACTION_HASH_MOCK,
},
{
transactionHash: TRANSACTION_HASH_2_MOCK,
},
],
});

addTransactionBatch({
...request,
publishBatchHook,
request: {
...request.request,
transactions: [
{
...request.request.transactions[0],
type: TransactionType.swap,
},
{
...request.request.transactions[1],
type: TransactionType.bridge,
},
],
disable7702: true,
},
}).catch(() => {
// Intentionally empty
});

expect(request.request.transactions).toHaveLength(2);
await flushPromises();

const publishHooks = addTransactionMock.mock.calls.map(
([, options]) => options.publishHook,
);

publishHooks[0]?.(
TRANSACTION_META_MOCK,
TRANSACTION_SIGNATURE_MOCK,
).catch(() => {
// Intentionally empty
});

publishHooks[1]?.(
TRANSACTION_META_MOCK,
TRANSACTION_SIGNATURE_2_MOCK,
).catch(() => {
// Intentionally empty
});

await flushPromises();

expect(publishBatchHook).toHaveBeenCalledTimes(1);
expect(publishBatchHook.mock.calls[0][0].transactions[0].type).toBe(
TransactionType.swap,
);
expect(publishBatchHook.mock.calls[0][0].transactions[1].type).toBe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than asserting the hook is called with the type, ideally the hook wouldn't know, and instead we could verify that addTransaction is called multiple times with the correct types?

TransactionType.bridge,
);
});

it('resolves individual publish hooks with transaction hashes from publish batch hook', async () => {
const publishBatchHook: jest.MockedFn<PublishBatchHook> = jest.fn();

Expand Down
15 changes: 11 additions & 4 deletions packages/transaction-controller/src/utils/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ async function getNestedTransactionMeta(
ethQuery: EthQuery,
): Promise<NestedTransactionMetadata> {
const { from } = request;
const { params } = singleRequest;
const { params, type: requestedType } = singleRequest;

const { type } = await determineTransactionType(
const { type: determinedType } = await determineTransactionType(
{ from, ...params },
ethQuery,
);

const type = requestedType ?? determinedType;
return {
...params,
type,
Expand Down Expand Up @@ -550,7 +551,7 @@ async function processTransactionWithHook(
request: AddTransactionBatchRequest,
txBatchMeta?: TransactionBatchMeta,
) {
const { existingTransaction, params } = nestedTransaction;
const { existingTransaction, params, type } = nestedTransaction;

const {
addTransaction,
Expand Down Expand Up @@ -606,6 +607,7 @@ async function processTransactionWithHook(
networkClientId,
publishHook,
requireApproval: false,
type,
},
);

Expand All @@ -626,11 +628,16 @@ async function processTransactionWithHook(
value,
};

log('Processed new transaction with hook', { id, params: newParams });
log('Processed new transaction with hook', {
id,
params: newParams,
type,
});

return {
id,
params: newParams,
type,
};
}

Expand Down
Loading