Skip to content

Commit 1e6520d

Browse files
authored
Merge branch 'feat/add-batch-transaction-approval-type' into feat/add-estimate-gas-batch-transaction
2 parents 01941aa + 6aac6e7 commit 1e6520d

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

packages/transaction-controller/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add optional approval request when calling `addTransactionBatch` ([#5793](https://github.com/MetaMask/core/pull/5793))
1313
- Add `transactionBatches` array to state.
1414
- Add `TransactionBatchMeta` type.
15-
- Add `addBatchMetadata` to store batch metadata and `wipeTransactionBatches` to clean up state after batch hook completion.
1615

1716
## [56.2.0]
1817

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ describe('Batch Utils', () => {
14661466
expect(result?.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
14671467
});
14681468

1469-
it('saves a transaction batch and then cleans the state', async () => {
1469+
it('saves a transaction batch and then cleans the specific batch by ID', async () => {
14701470
const { approve } = mockRequestApproval(MESSENGER_MOCK, {
14711471
state: 'approved',
14721472
});
@@ -1505,13 +1505,18 @@ describe('Batch Utils', () => {
15051505
expect(updateMock).toHaveBeenCalledTimes(2);
15061506
expect(updateMock).toHaveBeenCalledWith(expect.any(Function));
15071507

1508-
// Simulate the state update
1508+
// Simulate the state update for adding the batch
15091509
const state = {
1510-
transactionBatches: [],
1510+
transactionBatches: [
1511+
{ id: 'batch1', chainId: '0x1', transactions: [] },
1512+
],
15111513
} as unknown as TransactionControllerState;
1514+
1515+
// Simulate adding the batch
15121516
updateMock.mock.calls[0][0](state);
15131517

15141518
expect(state.transactionBatches).toStrictEqual([
1519+
{ id: 'batch1', chainId: '0x1', transactions: [] },
15151520
expect.objectContaining({
15161521
id: expect.any(String),
15171522
chainId: CHAIN_ID_MOCK,
@@ -1535,9 +1540,12 @@ describe('Batch Utils', () => {
15351540

15361541
await resultPromise;
15371542

1543+
// Simulate cleaning the specific batch by ID
15381544
updateMock.mock.calls[1][0](state);
15391545

1540-
expect(state.transactionBatches).toStrictEqual([]);
1546+
expect(state.transactionBatches).toStrictEqual([
1547+
{ id: 'batch1', chainId: '0x1', transactions: [] },
1548+
]);
15411549
});
15421550
});
15431551
});

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
TransactionType,
5656
} from '../types';
5757

58-
type UpdateBatchMetadata = (
58+
type UpdateStateCallback = (
5959
callback: (
6060
state: WritableDraft<TransactionControllerState>,
6161
) => void | TransactionControllerState,
@@ -82,7 +82,7 @@ type AddTransactionBatchRequest = {
8282
getPendingTransactionTracker: (
8383
networkClientId: string,
8484
) => PendingTransactionTracker;
85-
update: UpdateBatchMetadata;
85+
update: UpdateStateCallback;
8686
};
8787

8888
type IsAtomicBatchSupportedRequestInternal = {
@@ -506,8 +506,8 @@ async function addTransactionBatchWithHook(
506506

507507
throw error;
508508
} finally {
509-
log('Cleaning up publish batch hook');
510-
wipeTransactionBatches(update);
509+
log('Cleaning up publish batch hook', batchId);
510+
wipeTransactionBatchById(update, batchId);
511511
}
512512
}
513513

@@ -658,9 +658,9 @@ function newBatchMetadata(
658658
*/
659659
function addBatchMetadata(
660660
transactionBatchMeta: TransactionBatchMeta,
661-
update: UpdateBatchMetadata,
661+
update: UpdateStateCallback,
662662
) {
663-
update((state: WritableDraft<TransactionControllerState>) => {
663+
update((state) => {
664664
state.transactionBatches = [
665665
...state.transactionBatches,
666666
transactionBatchMeta,
@@ -669,12 +669,18 @@ function addBatchMetadata(
669669
}
670670

671671
/**
672-
* Wipes all transaction batches from the transaction controller state.
672+
* Wipes a specific transaction batch from the transaction controller state by its ID.
673673
*
674674
* @param update - The update function to modify the transaction controller state.
675+
* @param id - The ID of the transaction batch to be wiped.
675676
*/
676-
function wipeTransactionBatches(update: UpdateBatchMetadata): void {
677-
update((state: WritableDraft<TransactionControllerState>) => {
678-
state.transactionBatches = [];
677+
function wipeTransactionBatchById(
678+
update: UpdateStateCallback,
679+
id: string,
680+
): void {
681+
update((state) => {
682+
state.transactionBatches = state.transactionBatches.filter(
683+
(batch) => batch.id !== id,
684+
);
679685
});
680686
}

0 commit comments

Comments
 (0)