Skip to content

Commit 6aac6e7

Browse files
committed
fix: applied suggestions
1 parent 559651c commit 6aac6e7

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
@@ -1402,7 +1402,7 @@ describe('Batch Utils', () => {
14021402
expect(result?.batchId).toMatch(/^0x[0-9a-f]{32}$/u);
14031403
});
14041404

1405-
it('saves a transaction batch and then cleans the state', async () => {
1405+
it('saves a transaction batch and then cleans the specific batch by ID', async () => {
14061406
const { approve } = mockRequestApproval(MESSENGER_MOCK, {
14071407
state: 'approved',
14081408
});
@@ -1441,13 +1441,18 @@ describe('Batch Utils', () => {
14411441
expect(updateMock).toHaveBeenCalledTimes(2);
14421442
expect(updateMock).toHaveBeenCalledWith(expect.any(Function));
14431443

1444-
// Simulate the state update
1444+
// Simulate the state update for adding the batch
14451445
const state = {
1446-
transactionBatches: [],
1446+
transactionBatches: [
1447+
{ id: 'batch1', chainId: '0x1', transactions: [] },
1448+
],
14471449
} as unknown as TransactionControllerState;
1450+
1451+
// Simulate adding the batch
14481452
updateMock.mock.calls[0][0](state);
14491453

14501454
expect(state.transactionBatches).toStrictEqual([
1455+
{ id: 'batch1', chainId: '0x1', transactions: [] },
14511456
expect.objectContaining({
14521457
id: expect.any(String),
14531458
chainId: CHAIN_ID_MOCK,
@@ -1474,9 +1479,12 @@ describe('Batch Utils', () => {
14741479

14751480
await resultPromise;
14761481

1482+
// Simulate cleaning the specific batch by ID
14771483
updateMock.mock.calls[1][0](state);
14781484

1479-
expect(state.transactionBatches).toStrictEqual([]);
1485+
expect(state.transactionBatches).toStrictEqual([
1486+
{ id: 'batch1', chainId: '0x1', transactions: [] },
1487+
]);
14801488
});
14811489
});
14821490
});

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

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

57-
type UpdateBatchMetadata = (
57+
type UpdateStateCallback = (
5858
callback: (
5959
state: WritableDraft<TransactionControllerState>,
6060
) => void | TransactionControllerState,
@@ -81,7 +81,7 @@ type AddTransactionBatchRequest = {
8181
getPendingTransactionTracker: (
8282
networkClientId: string,
8383
) => PendingTransactionTracker;
84-
update: UpdateBatchMetadata;
84+
update: UpdateStateCallback;
8585
};
8686

8787
type IsAtomicBatchSupportedRequestInternal = {
@@ -480,8 +480,8 @@ async function addTransactionBatchWithHook(
480480

481481
throw error;
482482
} finally {
483-
log('Cleaning up publish batch hook');
484-
wipeTransactionBatches(update);
483+
log('Cleaning up publish batch hook', batchId);
484+
wipeTransactionBatchById(update, batchId);
485485
}
486486
}
487487

@@ -639,9 +639,9 @@ function newBatchMetadata({
639639
*/
640640
function addBatchMetadata(
641641
transactionBatchMeta: TransactionBatchMeta,
642-
update: UpdateBatchMetadata,
642+
update: UpdateStateCallback,
643643
) {
644-
update((state: WritableDraft<TransactionControllerState>) => {
644+
update((state) => {
645645
state.transactionBatches = [
646646
...state.transactionBatches,
647647
transactionBatchMeta,
@@ -650,12 +650,18 @@ function addBatchMetadata(
650650
}
651651

652652
/**
653-
* Wipes all transaction batches from the transaction controller state.
653+
* Wipes a specific transaction batch from the transaction controller state by its ID.
654654
*
655655
* @param update - The update function to modify the transaction controller state.
656+
* @param id - The ID of the transaction batch to be wiped.
656657
*/
657-
function wipeTransactionBatches(update: UpdateBatchMetadata): void {
658-
update((state: WritableDraft<TransactionControllerState>) => {
659-
state.transactionBatches = [];
658+
function wipeTransactionBatchById(
659+
update: UpdateStateCallback,
660+
id: string,
661+
): void {
662+
update((state) => {
663+
state.transactionBatches = state.transactionBatches.filter(
664+
(batch) => batch.id !== id,
665+
);
660666
});
661667
}

0 commit comments

Comments
 (0)