Skip to content

Commit

Permalink
[token-detection-controller] Replace tokens-controller callbacks with…
Browse files Browse the repository at this point in the history
… `getState`, `addDetectedTokens` messenger actions
  • Loading branch information
MajorLift committed Jan 24, 2024
1 parent bf87104 commit 83e87ad
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import type {
TokenListToken,
} from './TokenListController';
import type { Token } from './TokenRatesController';
import type { TokensController, TokensState } from './TokensController';
import type {
TokensControllerAddDetectedTokensAction,
TokensControllerGetStateAction,
} from './TokensController';

const DEFAULT_INTERVAL = 180000;

Expand Down Expand Up @@ -80,7 +83,9 @@ export type AllowedActions =
| NetworkControllerGetNetworkConfigurationByNetworkClientId
| GetTokenListState
| KeyringControllerGetStateAction
| PreferencesControllerGetStateAction;
| PreferencesControllerGetStateAction
| TokensControllerGetStateAction
| TokensControllerAddDetectedTokensAction;

export type TokenDetectionControllerStateChangeEvent =
ControllerStateChangeEvent<typeof controllerName, TokenDetectionState>;
Expand Down Expand Up @@ -136,12 +141,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<

#isDetectionEnabledForNetwork: boolean;

readonly #addDetectedTokens: TokensController['addDetectedTokens'];

readonly #getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];

readonly #getTokensState: () => TokensState;

readonly #trackMetaMetricsEvent: (options: {
event: string;
category: string;
Expand All @@ -161,9 +162,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
* @param options.interval - Polling interval used to fetch new token rates
* @param options.networkClientId - The selected network client ID of the current network
* @param options.selectedAddress - Vault selected address
* @param options.addDetectedTokens - Add a list of detected tokens.
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
* @param options.getTokensState - Gets the current state of the Tokens controller.
* @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
*/
constructor({
Expand All @@ -172,18 +171,14 @@ export class TokenDetectionController extends StaticIntervalPollingController<
interval = DEFAULT_INTERVAL,
disabled = true,
getBalancesInSingleCall,
addDetectedTokens,
getTokensState,
trackMetaMetricsEvent,
messenger,
}: {
networkClientId: NetworkClientId;
selectedAddress?: string;
interval?: number;
disabled?: boolean;
addDetectedTokens: TokensController['addDetectedTokens'];
getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
getTokensState: () => TokensState;
trackMetaMetricsEvent: (options: {
event: string;
category: string;
Expand Down Expand Up @@ -216,9 +211,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
this.#chainId,
);

this.#addDetectedTokens = addDetectedTokens;
this.#getBalancesInSingleCall = getBalancesInSingleCall;
this.#getTokensState = getTokensState;

this.#trackMetaMetricsEvent = trackMetaMetricsEvent;

Expand Down Expand Up @@ -449,7 +442,9 @@ export class TokenDetectionController extends StaticIntervalPollingController<
? STATIC_MAINNET_TOKEN_LIST
: tokenList;

const { tokens, detectedTokens } = this.#getTokensState();
const { tokens, detectedTokens } = this.messagingSystem.call(
'TokensController:getState',
);
const tokensToDetect: string[] = [];

const findCaseInsensitiveMatch = (source: string[], target: string) =>
Expand Down Expand Up @@ -496,7 +491,9 @@ export class TokenDetectionController extends StaticIntervalPollingController<
for (const tokenAddress of Object.keys(balances)) {
let ignored;
/* istanbul ignore else */
const { ignoredTokens } = this.#getTokensState();
const { ignoredTokens } = this.messagingSystem.call(
'TokensController:getState',
);
if (ignoredTokens.length) {
ignored = ignoredTokens.find(
(ignoredTokenAddress) =>
Expand Down Expand Up @@ -535,10 +532,14 @@ export class TokenDetectionController extends StaticIntervalPollingController<
asset_type: 'TOKEN',
},
});
await this.#addDetectedTokens(tokensToAdd, {
selectedAddress,
chainId,
});
await this.messagingSystem.call(
'TokensController:addDetectedTokens',
tokensToAdd,
{
selectedAddress,
chainId,
},
);
}
});
}
Expand Down

0 comments on commit 83e87ad

Please sign in to comment.