Skip to content
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

fix: remove scoped state #5310

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
8 changes: 3 additions & 5 deletions eslint-warning-thresholds.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"import-x/namespace": 2
},
"packages/assets-controllers/src/AccountTrackerController.ts": {
"jsdoc/check-tag-names": 5,
"jsdoc/tag-lines": 1
"jsdoc/check-tag-names": 5
},
"packages/assets-controllers/src/AssetsContractController.test.ts": {
"import-x/order": 3
Expand Down Expand Up @@ -98,7 +97,6 @@
},
"packages/assets-controllers/src/TokenDetectionController.test.ts": {
"import-x/namespace": 11,
"import-x/order": 3,
"jsdoc/tag-lines": 1
},
"packages/assets-controllers/src/TokenDetectionController.ts": {
Expand Down Expand Up @@ -132,8 +130,8 @@
"packages/assets-controllers/src/TokensController.ts": {
"@typescript-eslint/no-unused-vars": 1,
"@typescript-eslint/prefer-readonly": 1,
"jsdoc/check-tag-names": 13,
"jsdoc/tag-lines": 3
"jsdoc/check-tag-names": 10,
"jsdoc/tag-lines": 2
},
"packages/assets-controllers/src/assetsUtil.test.ts": {
"jest/no-conditional-in-test": 2
Expand Down
17 changes: 17 additions & 0 deletions packages/assets-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Updated `TokensController`, `TokenListController`, and `AccountTrackerController` to use per-chain state variants. ([#5310](https://github.com/MetaMask/core/pull/5310))

### Removed

- **BREAKING:** Remove deprecated state fields scoped to the current chain ([#5310](https://github.com/MetaMask/core/pull/5310))
- This change removes the following state fields:
- `TokensController:state`
- `detectedTokens` (replaced by `detectedTokensByChainId`)
- `ignoredTokens` (replaced by `ignoredTokensByChainId`)
- `tokens` (replaced by `tokensByChainId`)
- `TokenListController:state`
- `tokenList` (replaced by `tokensChainsCache`)
- `AccountTrackerController:state`
- `accounts` (replaced by `accountsByChainId`)

## [50.0.0]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ describe('AccountTrackerController', () => {
},
({ controller }) => {
expect(controller.state).toStrictEqual({
accounts: {},
accountsByChainId: {
[initialChainId]: {},
},
Expand Down Expand Up @@ -111,10 +110,6 @@ describe('AccountTrackerController', () => {
{
options: {
state: {
accounts: {
[checksumAddress1]: { balance: '0x1' },
foo: { balance: '0x2' },
},
accountsByChainId: {
'0x1': {
[checksumAddress1]: { balance: '0x1' },
Expand All @@ -134,10 +129,6 @@ describe('AccountTrackerController', () => {
async ({ controller }) => {
await controller.refresh();
expect(controller.state).toStrictEqual({
accounts: {
[checksumAddress1]: { balance: '0x0' },
[checksumAddress2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[checksumAddress1]: { balance: '0x0' },
Expand Down Expand Up @@ -166,11 +157,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: {
balance: '0x10',
},
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -198,10 +184,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x10' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: { balance: '0x10' },
Expand All @@ -228,10 +210,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x11' },
[CHECKSUM_ADDRESS_2]: { balance: '0x12' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: { balance: '0x11' },
Expand Down Expand Up @@ -262,10 +240,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x10', stakedBalance: '0x1' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -301,10 +275,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x13' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -339,10 +309,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x11', stakedBalance: '0x1' },
[CHECKSUM_ADDRESS_2]: { balance: '0x12', stakedBalance: '0x1' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -378,10 +344,6 @@ describe('AccountTrackerController', () => {
{
options: {
state: {
accounts: {
[checksumAddress1]: { balance: '0x1' },
foo: { balance: '0x2' },
},
accountsByChainId: {
'0x1': {
[checksumAddress1]: { balance: '0x1' },
Expand All @@ -406,10 +368,6 @@ describe('AccountTrackerController', () => {
async ({ controller }) => {
await controller.refresh(networkClientId);
expect(controller.state).toStrictEqual({
accounts: {
[checksumAddress1]: { balance: '0x1' },
[checksumAddress2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[checksumAddress1]: { balance: '0x1' },
Expand Down Expand Up @@ -448,11 +406,6 @@ describe('AccountTrackerController', () => {
await controller.refresh(networkClientId);

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: {
balance: '0x0',
},
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -491,10 +444,6 @@ describe('AccountTrackerController', () => {
await controller.refresh(networkClientId);

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x0' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: { balance: '0x0' },
Expand Down Expand Up @@ -531,10 +480,6 @@ describe('AccountTrackerController', () => {
await controller.refresh(networkClientId);

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x0' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: { balance: '0x0' },
Expand Down Expand Up @@ -575,10 +520,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x10', stakedBalance: '0x1' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -620,10 +561,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x13' },
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -664,10 +601,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x11', stakedBalance: '0x1' },
[CHECKSUM_ADDRESS_2]: { balance: '0x12', stakedBalance: '0x1' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down Expand Up @@ -710,10 +643,6 @@ describe('AccountTrackerController', () => {
await controller.refresh();

expect(controller.state).toStrictEqual({
accounts: {
[CHECKSUM_ADDRESS_1]: { balance: '0x11' },
[CHECKSUM_ADDRESS_2]: { balance: '0x12' },
},
accountsByChainId: {
'0x1': {
[CHECKSUM_ADDRESS_1]: {
Expand Down
40 changes: 8 additions & 32 deletions packages/assets-controllers/src/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from '@metamask/network-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';
import { type Hex, assert } from '@metamask/utils';
import { assert } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { cloneDeep } from 'lodash';

Expand Down Expand Up @@ -52,18 +52,13 @@ export type AccountInformation = {
* @type AccountTrackerControllerState
*
* Account tracker controller state
* @property accounts - Map of addresses to account information
* @property accountsByChainId - Map of addresses to account information by chain
*/
export type AccountTrackerControllerState = {
accounts: { [address: string]: AccountInformation };
accountsByChainId: Record<string, { [address: string]: AccountInformation }>;
};

const accountTrackerMetadata = {
accounts: {
persist: true,
anonymous: false,
},
accountsByChainId: {
persist: true,
anonymous: false,
Expand Down Expand Up @@ -182,7 +177,6 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac
name: controllerName,
messenger,
state: {
accounts: {},
accountsByChainId: {
[chainId]: {},
},
Expand All @@ -204,28 +198,20 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac
);
}

/**
* Gets the current chain ID.
* @returns The current chain ID.
*/
#getCurrentChainId(): Hex {
private syncAccounts(newChainId: string) {
const accountsByChainId = cloneDeep(this.state.accountsByChainId);
const { selectedNetworkClientId } = this.messagingSystem.call(
'NetworkController:getState',
);
const {
configuration: { chainId },
configuration: { chainId: currentChainId },
} = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
selectedNetworkClientId,
);
return chainId;
}

private syncAccounts(newChainId: string) {
const accounts = { ...this.state.accounts };
const accountsByChainId = cloneDeep(this.state.accountsByChainId);
const existing = Object.keys(accountsByChainId?.[currentChainId] ?? {});

const existing = Object.keys(accounts);
if (!accountsByChainId[newChainId]) {
accountsByChainId[newChainId] = {};
existing.forEach((address) => {
Expand All @@ -248,9 +234,6 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac
const oldAddresses = existing.filter(
(address) => !addresses.includes(address),
);
newAddresses.forEach((address) => {
accounts[address] = { balance: '0x0' };
});
Object.keys(accountsByChainId).forEach((chainId) => {
newAddresses.forEach((address) => {
accountsByChainId[chainId][address] = {
Expand All @@ -259,17 +242,13 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac
});
});

oldAddresses.forEach((address) => {
delete accounts[address];
});
Object.keys(accountsByChainId).forEach((chainId) => {
oldAddresses.forEach((address) => {
delete accountsByChainId[chainId][address];
});
});

this.update((state) => {
state.accounts = accounts;
state.accountsByChainId = accountsByChainId;
});
}
Expand Down Expand Up @@ -333,13 +312,13 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac
const { chainId, ethQuery } =
this.#getCorrectNetworkClient(networkClientId);
this.syncAccounts(chainId);
const { accounts, accountsByChainId } = this.state;
const { accountsByChainId } = this.state;
const { isMultiAccountBalancesEnabled } = this.messagingSystem.call(
'PreferencesController:getState',
);

const accountsToUpdate = isMultiAccountBalancesEnabled
? Object.keys(accounts)
? Object.keys(accountsByChainId[chainId])
: [toChecksumHexAddress(selectedAccount.address)];

const accountsForChain = { ...accountsByChainId[chainId] };
Expand All @@ -365,9 +344,6 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac
}

this.update((state) => {
if (chainId === this.#getCurrentChainId()) {
state.accounts = accountsForChain;
}
state.accountsByChainId[chainId] = accountsForChain;
});
} finally {
Expand Down
Loading
Loading