Skip to content

feat: migrate TokensController to BaseControllerV2 #4304

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/assets-controllers/src/TokenBalancesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import type {
} from './TokenBalancesController';
import { TokenBalancesController } from './TokenBalancesController';
import type { Token } from './TokenRatesController';
import { getDefaultTokensState, type TokensState } from './TokensController';
import {
getDefaultTokensState,
type TokensControllerState,
} from './TokensController';

const controllerName = 'TokenBalancesController';

Expand Down Expand Up @@ -192,7 +195,7 @@ describe('TokenBalancesController', () => {
getERC20BalanceOf: jest.fn().mockReturnValue(new BN(1)),
messenger,
});
const triggerTokensStateChange = async (state: TokensState) => {
const triggerTokensStateChange = async (state: TokensControllerState) => {
controllerMessenger.publish('TokensController:stateChange', state, []);
};

Expand Down Expand Up @@ -230,7 +233,7 @@ describe('TokenBalancesController', () => {
getERC20BalanceOf: jest.fn().mockReturnValue(new BN(1)),
messenger,
});
const triggerTokensStateChange = async (state: TokensState) => {
const triggerTokensStateChange = async (state: TokensControllerState) => {
controllerMessenger.publish('TokensController:stateChange', state, []);
};

Expand Down Expand Up @@ -303,7 +306,7 @@ describe('TokenBalancesController', () => {

await controller.updateBalances();

expect(tokens[0].balanceError).toBeNull();
expect(tokens[0].hasBalanceError).toBe(false);
expect(Object.keys(controller.state.contractBalances)).toContain(address);
expect(controller.state.contractBalances[address]).not.toBe(toHex(0));
});
Expand Down Expand Up @@ -338,15 +341,14 @@ describe('TokenBalancesController', () => {

await controller.updateBalances();

expect(tokens[0].balanceError).toBeInstanceOf(Error);
expect(tokens[0].balanceError).toHaveProperty('message', errorMsg);
expect(tokens[0].hasBalanceError).toBe(true);
expect(controller.state.contractBalances[address]).toBe(toHex(0));

getERC20BalanceOfStub.mockReturnValue(new BN(1));

await controller.updateBalances();

expect(tokens[0].balanceError).toBeNull();
expect(tokens[0].hasBalanceError).toBe(false);
expect(Object.keys(controller.state.contractBalances)).toContain(address);
expect(controller.state.contractBalances[address]).not.toBe(0);
});
Expand All @@ -361,7 +363,7 @@ describe('TokenBalancesController', () => {
interval: 1337,
messenger,
});
const triggerTokensStateChange = async (state: TokensState) => {
const triggerTokensStateChange = async (state: TokensControllerState) => {
controllerMessenger.publish('TokensController:stateChange', state, []);
};
const updateBalancesSpy = jest.spyOn(controller, 'updateBalances');
Expand Down Expand Up @@ -390,7 +392,7 @@ describe('TokenBalancesController', () => {
getERC20BalanceOf: jest.fn().mockReturnValue(new BN(1)),
messenger,
});
const triggerTokensStateChange = async (state: TokensState) => {
const triggerTokensStateChange = async (state: TokensControllerState) => {
controllerMessenger.publish('TokensController:stateChange', state, []);
};
expect(controller.state.contractBalances).toStrictEqual({});
Expand Down
16 changes: 8 additions & 8 deletions packages/assets-controllers/src/TokenBalancesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,20 @@ export class TokenBalancesController extends BaseController<
return;
}

const { selectedAddress } = this.messagingSystem.call(
'PreferencesController:getState',
);

const newContractBalances: ContractBalances = {};
for (const token of this.#tokens) {
const { address } = token;
const { selectedAddress } = this.messagingSystem.call(
'PreferencesController:getState',
);
try {
newContractBalances[address] = toHex(
await this.#getERC20BalanceOf(address, selectedAddress),
);
token.balanceError = null;
const balance = await this.#getERC20BalanceOf(address, selectedAddress);
newContractBalances[address] = toHex(balance);
token.hasBalanceError = false;
} catch (error) {
newContractBalances[address] = toHex(0);
token.balanceError = error;
token.hasBalanceError = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import {
type TokenListState,
type TokenListToken,
} from './TokenListController';
import type { TokensController, TokensState } from './TokensController';
import type {
TokensController,
TokensControllerState,
} from './TokensController';
import { getDefaultTokensState } from './TokensController';

const DEFAULT_INTERVAL = 180000;
Expand Down Expand Up @@ -1996,7 +1999,7 @@ type WithControllerCallback<ReturnValue> = ({
controller: TokenDetectionController;
mockGetSelectedAccount: (address: string) => void;
mockKeyringGetState: (state: KeyringControllerState) => void;
mockTokensGetState: (state: TokensState) => void;
mockTokensGetState: (state: TokensControllerState) => void;
mockTokenListGetState: (state: TokenListState) => void;
mockPreferencesGetState: (state: PreferencesState) => void;
mockGetNetworkClientById: (
Expand Down Expand Up @@ -2090,7 +2093,7 @@ async function withController<ReturnValue>(
'NetworkController:getState',
mockNetworkState.mockReturnValue({ ...defaultNetworkState }),
);
const mockTokensState = jest.fn<TokensState, []>();
const mockTokensState = jest.fn<TokensControllerState, []>();
controllerMessenger.registerActionHandler(
'TokensController:getState',
mockTokensState.mockReturnValue({ ...getDefaultTokensState() }),
Expand Down Expand Up @@ -2133,7 +2136,7 @@ async function withController<ReturnValue>(
mockKeyringGetState: (state: KeyringControllerState) => {
mockKeyringState.mockReturnValue(state);
},
mockTokensGetState: (state: TokensState) => {
mockTokensGetState: (state: TokensControllerState) => {
mockTokensState.mockReturnValue(state);
},
mockPreferencesGetState: (state: PreferencesState) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type {
Token,
TokenRatesState,
} from './TokenRatesController';
import type { TokensState } from './TokensController';
import type { TokensControllerState } from './TokensController';

const defaultSelectedAddress = '0x0000000000000000000000000000000000000001';
const mockTokenAddress = '0x0000000000000000000000000000000000000010';
Expand Down Expand Up @@ -2387,7 +2387,7 @@ describe('TokenRatesController', () => {
type ControllerEvents = {
networkStateChange: (state: NetworkState) => void;
preferencesStateChange: (state: PreferencesState) => void;
tokensStateChange: (state: TokensState) => void;
tokensStateChange: (state: TokensControllerState) => void;
};

/**
Expand Down
14 changes: 6 additions & 8 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from './assetsUtil';
import { fetchExchangeRate as fetchNativeCurrencyExchangeRate } from './crypto-compare-service';
import type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';
import { ZERO_ADDRESS } from './token-prices-service/codefi-v2';
import type { TokensState } from './TokensController';
import type { TokensControllerState } from './TokensController';

/**
* @type Token
Expand All @@ -30,19 +30,17 @@ import type { TokensState } from './TokensController';
* @property symbol - Symbol of the token
* @property image - Image of the token, url or bit32 image
*/
// This interface was created before this ESLint rule was added.
// Convert to a `type` in a future major version.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface Token {

export type Token = {
address: string;
decimals: number;
symbol: string;
aggregators?: string[];
image?: string;
balanceError?: unknown;
hasBalanceError?: boolean;
isERC721?: boolean;
name?: string;
}
};

/**
* @type TokenRatesConfig
Expand Down Expand Up @@ -218,7 +216,7 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
listener: (preferencesState: PreferencesState) => void,
) => void;
onTokensStateChange: (
listener: (tokensState: TokensState) => void,
listener: (tokensState: TokensControllerState) => void,
) => void;
onNetworkStateChange: (
listener: (networkState: NetworkState) => void,
Expand Down
Loading
Loading