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 12 commits into
base: main
Choose a base branch
from
Open
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
139 changes: 139 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,101 @@ describe('Batch Utils', () => {
expect(result.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
});

it('preserves nested transaction types when disable7702 is true', async () => {
const publishBatchHook: jest.MockedFn<PublishBatchHook> = jest.fn();
mockRequestApproval(MESSENGER_MOCK, {
state: 'approved',
});

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(''),
});
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
});

await flushPromises();

expect(addTransactionMock).toHaveBeenCalledTimes(2);
expect(addTransactionMock.mock.calls[0][1].type).toBe('swap');
expect(addTransactionMock.mock.calls[1][1].type).toBe('bridge');
});

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 @@ -837,6 +932,50 @@ describe('Batch Utils', () => {
PUBLISH_BATCH_HOOK_PARAMS,
);
});
// 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(''),
// });
// 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
// });

// await flushPromises();

// expect(addTransactionMock).toHaveBeenCalledTimes(2);
// expect(addTransactionMock.mock.calls[0][1].type).toBe('swap');
// expect(addTransactionMock.mock.calls[1][1].type).toBe('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