Skip to content

Commit cb4c666

Browse files
authored
fix: preserve transaction type of batched transactions (#6056)
1 parent 1691d07 commit cb4c666

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

packages/transaction-controller/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
### Fixed
2121

22+
- Preserve provided `type` in `transactions` when calling `addTransactionBatch` ([#6056](https://github.com/MetaMask/core/pull/6056))
2223
- Normalize transaction `data` to ensure case-insensitive detection ([#6102](https://github.com/MetaMask/core/pull/6102))
2324

2425
## [58.1.1]

packages/transaction-controller/src/TransactionController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ import type {
120120
AfterSimulateHook,
121121
BeforeSignHook,
122122
TransactionContainerType,
123+
NestedTransactionMetadata,
123124
} from './types';
124125
import {
125126
GasFeeEstimateLevel,
@@ -1132,7 +1133,7 @@ export class TransactionController extends BaseController<
11321133
deviceConfirmedOn?: WalletDevice;
11331134
disableGasBuffer?: boolean;
11341135
method?: string;
1135-
nestedTransactions?: BatchTransactionParams[];
1136+
nestedTransactions?: NestedTransactionMetadata[];
11361137
networkClientId: NetworkClientId;
11371138
origin?: string;
11381139
publishHook?: PublishHook;

packages/transaction-controller/src/utils/batch.test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,101 @@ describe('Batch Utils', () => {
370370
expect(result.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
371371
});
372372

373+
it('preserves nested transaction types when disable7702 is true', async () => {
374+
const publishBatchHook: jest.MockedFn<PublishBatchHook> = jest.fn();
375+
mockRequestApproval(MESSENGER_MOCK, {
376+
state: 'approved',
377+
});
378+
379+
addTransactionMock
380+
.mockResolvedValueOnce({
381+
transactionMeta: {
382+
...TRANSACTION_META_MOCK,
383+
id: TRANSACTION_ID_MOCK,
384+
},
385+
result: Promise.resolve(''),
386+
})
387+
.mockResolvedValueOnce({
388+
transactionMeta: {
389+
...TRANSACTION_META_MOCK,
390+
id: TRANSACTION_ID_2_MOCK,
391+
},
392+
result: Promise.resolve(''),
393+
});
394+
addTransactionBatch({
395+
...request,
396+
publishBatchHook,
397+
request: {
398+
...request.request,
399+
transactions: [
400+
{
401+
...request.request.transactions[0],
402+
type: TransactionType.swap,
403+
},
404+
{
405+
...request.request.transactions[1],
406+
type: TransactionType.bridge,
407+
},
408+
],
409+
disable7702: true,
410+
},
411+
}).catch(() => {
412+
// Intentionally empty
413+
});
414+
415+
await flushPromises();
416+
417+
expect(addTransactionMock).toHaveBeenCalledTimes(2);
418+
expect(addTransactionMock.mock.calls[0][1].type).toBe('swap');
419+
expect(addTransactionMock.mock.calls[1][1].type).toBe('bridge');
420+
});
421+
422+
it('preserves nested transaction types when disable7702 is false', async () => {
423+
const publishBatchHook: jest.MockedFn<PublishBatchHook> = jest.fn();
424+
425+
isAccountUpgradedToEIP7702Mock.mockResolvedValueOnce({
426+
delegationAddress: undefined,
427+
isSupported: true,
428+
});
429+
430+
addTransactionMock.mockResolvedValueOnce({
431+
transactionMeta: TRANSACTION_META_MOCK,
432+
result: Promise.resolve(''),
433+
});
434+
435+
const result = await addTransactionBatch({
436+
...request,
437+
publishBatchHook,
438+
request: {
439+
...request.request,
440+
transactions: [
441+
{
442+
...request.request.transactions[0],
443+
type: TransactionType.swapApproval,
444+
},
445+
{
446+
...request.request.transactions[1],
447+
type: TransactionType.bridgeApproval,
448+
},
449+
],
450+
disable7702: false,
451+
},
452+
});
453+
454+
expect(result.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
455+
expect(addTransactionMock).toHaveBeenCalledTimes(1);
456+
expect(addTransactionMock.mock.calls[0][1].type).toStrictEqual(
457+
TransactionType.batch,
458+
);
459+
460+
expect(
461+
addTransactionMock.mock.calls[0][1].nestedTransactions?.[0].type,
462+
).toBe(TransactionType.swapApproval);
463+
expect(
464+
addTransactionMock.mock.calls[0][1].nestedTransactions?.[1].type,
465+
).toBe(TransactionType.bridgeApproval);
466+
});
467+
373468
it('returns provided batch ID', async () => {
374469
isAccountUpgradedToEIP7702Mock.mockResolvedValueOnce({
375470
delegationAddress: undefined,

packages/transaction-controller/src/utils/batch.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,14 @@ async function getNestedTransactionMeta(
246246
ethQuery: EthQuery,
247247
): Promise<NestedTransactionMetadata> {
248248
const { from } = request;
249-
const { params } = singleRequest;
249+
const { params, type: requestedType } = singleRequest;
250250

251-
const { type } = await determineTransactionType(
251+
const { type: determinedType } = await determineTransactionType(
252252
{ from, ...params },
253253
ethQuery,
254254
);
255255

256+
const type = requestedType ?? determinedType;
256257
return {
257258
...params,
258259
type,
@@ -550,7 +551,7 @@ async function processTransactionWithHook(
550551
request: AddTransactionBatchRequest,
551552
txBatchMeta?: TransactionBatchMeta,
552553
) {
553-
const { existingTransaction, params } = nestedTransaction;
554+
const { existingTransaction, params, type } = nestedTransaction;
554555

555556
const {
556557
addTransaction,
@@ -606,6 +607,7 @@ async function processTransactionWithHook(
606607
networkClientId,
607608
publishHook,
608609
requireApproval: false,
610+
type,
609611
},
610612
);
611613

@@ -626,11 +628,16 @@ async function processTransactionWithHook(
626628
value,
627629
};
628630

629-
log('Processed new transaction with hook', { id, params: newParams });
631+
log('Processed new transaction with hook', {
632+
id,
633+
params: newParams,
634+
type,
635+
});
630636

631637
return {
632638
id,
633639
params: newParams,
640+
type,
634641
};
635642
}
636643

0 commit comments

Comments
 (0)