Skip to content

Commit

Permalink
deepClone state before modifying
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi committed Dec 7, 2023
1 parent 1e63820 commit 083cde2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/assets-controllers/src/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import { assert } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { cloneDeep } from 'lodash';

/**
* @type AccountInformation
Expand Down Expand Up @@ -73,7 +74,8 @@ export class AccountTrackerController extends PollingControllerV1<
private handle?: ReturnType<typeof setTimeout>;

private syncAccounts(newChainId: string) {
const { accounts, accountsByChainId } = this.state;
const accounts = cloneDeep(this.state.accounts)

Check failure on line 77 in packages/assets-controllers/src/AccountTrackerController.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Insert `;`
const accountsByChainId = cloneDeep(this.state.accountsByChainId)

Check failure on line 78 in packages/assets-controllers/src/AccountTrackerController.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Insert `;`

const existing = Object.keys(accounts);
if (!accountsByChainId[newChainId]) {
Expand Down Expand Up @@ -272,22 +274,22 @@ export class AccountTrackerController extends PollingControllerV1<
? Object.keys(accounts)
: [this.getSelectedAddress()];

const accountsForChain = accountsByChainId[chainId];
const accountsForChain = cloneDeep(accountsByChainId[chainId]);
for (const address of accountsToUpdate) {
accountsForChain[address] = {
balance: BNToHex(await this.getBalanceFromChain(address, ethQuery)),
};
}

this.update({
...(chainId === this.getCurrentChainId() && {
accounts: accountsForChain,
}),
accountsByChainId: {
...this.state.accountsByChainId,
[chainId]: accountsForChain,
},
});
if (chainId === this.getCurrentChainId()) {
this.update({ accounts: accountsForChain });
}
} catch (err) {
releaseLock();
throw err;
Expand Down

0 comments on commit 083cde2

Please sign in to comment.