Skip to content

Commit 2ff7fce

Browse files
committed
fix: remove deepClone
1 parent 129e011 commit 2ff7fce

File tree

2 files changed

+16
-51
lines changed

2 files changed

+16
-51
lines changed

packages/accounts-controller/src/AccountsController.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
import type { Draft } from 'immer';
3636

3737
import {
38-
deepCloneDraft,
3938
getUUIDFromAddressOfNormalAccount,
4039
isNormalKeyringType,
4140
keyringTypeToName,
@@ -412,12 +411,8 @@ export class AccountsController extends BaseController<
412411
...account,
413412
metadata: { ...account.metadata, name: accountName },
414413
};
415-
// FIXME: deep clone of old state to get around Type instantiation is excessively deep and possibly infinite.
416-
const newState = deepCloneDraft(currentState);
417-
418-
newState.internalAccounts.accounts[accountId] = internalAccount;
419-
420-
return newState;
414+
// @ts-expect-error Type instantiation is excessively deep and possibly infinite. See: https://github.com/MetaMask/utils/issues/168
415+
currentState.internalAccounts.accounts[accountId] = internalAccount;
421416
});
422417
}
423418

@@ -474,12 +469,7 @@ export class AccountsController extends BaseController<
474469
}, {} as Record<string, InternalAccount>);
475470

476471
this.update((currentState: Draft<AccountsControllerState>) => {
477-
// FIXME: deep clone of old state to get around Type instantiation is excessively deep and possibly infinite.
478-
const newState = deepCloneDraft(currentState);
479-
480-
newState.internalAccounts.accounts = accounts;
481-
482-
return newState;
472+
currentState.internalAccounts.accounts = accounts;
483473
});
484474
}
485475

@@ -491,12 +481,7 @@ export class AccountsController extends BaseController<
491481
loadBackup(backup: AccountsControllerState): void {
492482
if (backup.internalAccounts) {
493483
this.update((currentState: Draft<AccountsControllerState>) => {
494-
// FIXME: deep clone of old state to get around Type instantiation is excessively deep and possibly infinite.
495-
const newState = deepCloneDraft(currentState);
496-
497-
newState.internalAccounts = backup.internalAccounts;
498-
499-
return newState;
484+
currentState.internalAccounts = backup.internalAccounts;
500485
});
501486
}
502487
}
@@ -718,42 +703,41 @@ export class AccountsController extends BaseController<
718703
}
719704

720705
this.update((currentState: Draft<AccountsControllerState>) => {
721-
const newState = deepCloneDraft(currentState);
722-
723706
if (deletedAccounts.length > 0) {
724707
for (const account of deletedAccounts) {
725-
newState.internalAccounts.accounts = this.#handleAccountRemoved(
726-
newState.internalAccounts.accounts,
708+
currentState.internalAccounts.accounts = this.#handleAccountRemoved(
709+
currentState.internalAccounts.accounts,
727710
account.id,
728711
);
729712
}
730713
}
731714

732715
if (addedAccounts.length > 0) {
733716
for (const account of addedAccounts) {
734-
newState.internalAccounts.accounts = this.#handleNewAccountAdded(
735-
newState.internalAccounts.accounts,
736-
account,
737-
);
717+
currentState.internalAccounts.accounts =
718+
this.#handleNewAccountAdded(
719+
currentState.internalAccounts.accounts,
720+
account,
721+
);
738722
}
739723
}
740724

741725
// We don't use list accounts because it is not the updated state yet.
742726
const existingAccounts = Object.values(
743-
newState.internalAccounts.accounts,
727+
currentState.internalAccounts.accounts,
744728
);
745729

746730
// handle if the selected account was deleted
747731
if (
748-
!newState.internalAccounts.accounts[
732+
!currentState.internalAccounts.accounts[
749733
this.state.internalAccounts.selectedAccount
750734
]
751735
) {
752736
// if the accountToSelect is undefined, then there are no accounts
753737
// it mean the keyring was reinitialized.
754738
if (existingAccounts.length === 0) {
755-
newState.internalAccounts.selectedAccount = '';
756-
return newState;
739+
currentState.internalAccounts.selectedAccount = '';
740+
return;
757741
}
758742

759743
const [accountToSelect] = existingAccounts.sort(
@@ -766,10 +750,8 @@ export class AccountsController extends BaseController<
766750
},
767751
);
768752

769-
newState.internalAccounts.selectedAccount = accountToSelect.id;
753+
currentState.internalAccounts.selectedAccount = accountToSelect.id;
770754
}
771-
772-
return newState;
773755
});
774756
}
775757
}

packages/accounts-controller/src/utils.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { toBuffer } from '@ethereumjs/util';
22
import { isCustodyKeyring, KeyringTypes } from '@metamask/keyring-controller';
3-
import { deepClone } from '@metamask/snaps-utils';
43
import { sha256 } from 'ethereum-cryptography/sha256';
54
import type { Draft } from 'immer';
65
import type { V4Options } from 'uuid';
@@ -83,19 +82,3 @@ export function isNormalKeyringType(keyringType: KeyringTypes): boolean {
8382
// adapted later on if we have new kind of keyrings!
8483
return keyringType !== KeyringTypes.snap;
8584
}
86-
87-
/**
88-
* WARNING: To be removed once type issue is fixed. https://github.com/MetaMask/utils/issues/168
89-
*
90-
* Creates a deep clone of the given object.
91-
* This is to get around error `Type instantiation is excessively deep and possibly infinite.`
92-
*
93-
* @param obj - The object to be cloned.
94-
* @returns The deep clone of the object.
95-
*/
96-
export function deepCloneDraft(
97-
obj: Draft<AccountsControllerState>,
98-
): AccountsControllerState {
99-
// We use unknown here because the type inference when using structured clone leads to the same type error.
100-
return deepClone(obj) as unknown as AccountsControllerState;
101-
}

0 commit comments

Comments
 (0)