Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Feb 15, 2024
1 parent 78cabd0 commit bbbea84
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { AccountsControllerSelectedAccountChangeEvent } from '@metamask/accounts-controller';
import type {
AccountsControllerGetSelectedAccountAction,
AccountsControllerSelectedAccountChangeEvent,
} from '@metamask/accounts-controller';
import type {
RestrictedControllerMessenger,
ControllerGetStateAction,
Expand All @@ -19,6 +22,7 @@ import type {
NetworkClientId,
NetworkControllerNetworkDidChangeEvent,
NetworkControllerGetNetworkConfigurationByNetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import type {
Expand Down Expand Up @@ -95,7 +99,9 @@ export type TokenDetectionControllerActions =
TokenDetectionControllerGetStateAction;

export type AllowedActions =
| AccountsControllerGetSelectedAccountAction
| NetworkControllerGetNetworkConfigurationByNetworkClientId
| NetworkControllerGetNetworkClientByIdAction
| GetTokenListState
| KeyringControllerGetStateAction
| PreferencesControllerGetStateAction
Expand Down Expand Up @@ -130,7 +136,7 @@ export type TokenDetectionControllerMessenger = RestrictedControllerMessenger<
* @property chainId - The chain ID of the current network
* @property selectedAddress - Vault selected address
* @property networkClientId - The network client ID of the current selected network
* @property disabled - Boolean to track if network requests are blocked
* @property disableLegacyInterval - Boolean to track if network requests are blocked
* @property isUnlocked - Boolean to track if the keyring state is unlocked
* @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
* @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
Expand All @@ -148,7 +154,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<

#networkClientId: NetworkClientId;

#disabled: boolean;
#disableLegacyInterval: boolean;

#isUnlocked: boolean;

Expand All @@ -173,7 +179,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
*
* @param options - The controller options.
* @param options.messenger - The controller messaging system.
* @param options.disabled - If set to true, all network requests are blocked.
* @param options.disableLegacyInterval - If set to true, all network requests are blocked.
* @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
Expand All @@ -184,15 +190,15 @@ export class TokenDetectionController extends StaticIntervalPollingController<
networkClientId,
selectedAddress = '',
interval = DEFAULT_INTERVAL,
disabled = true,
disableLegacyInterval = true,
getBalancesInSingleCall,
trackMetaMetricsEvent,
messenger,
}: {
networkClientId: NetworkClientId;
selectedAddress?: string;
interval?: number;
disabled?: boolean;
disableLegacyInterval?: boolean;
getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
trackMetaMetricsEvent: (options: {
event: string;
Expand All @@ -212,11 +218,16 @@ export class TokenDetectionController extends StaticIntervalPollingController<
metadata: {},
});

this.#disabled = disabled;
this.setIntervalLength(interval);
this.#disableLegacyInterval = disableLegacyInterval;
if (!this.#disableLegacyInterval) {
this.setIntervalLength(interval);
}

this.#networkClientId = networkClientId;
this.#selectedAddress = selectedAddress;
this.#selectedAddress =
selectedAddress ??
this.messagingSystem.call('AccountsController:getSelectedAccount')
.address;
this.#chainId = this.#getCorrectChainId(networkClientId);

const { useTokenDetection: defaultUseTokenDetection } =
Expand Down Expand Up @@ -326,14 +337,14 @@ export class TokenDetectionController extends StaticIntervalPollingController<
* Allows controller to make active and passive polling requests
*/
enable() {
this.#disabled = false;
this.#disableLegacyInterval = false;
}

/**
* Blocks controller from making network calls
*/
disable() {
this.#disabled = true;
this.#disableLegacyInterval = true;
}

/**
Expand All @@ -342,7 +353,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
* @type {object}
*/
get isActive() {
return !this.#disabled && this.#isUnlocked;
return !this.#disableLegacyInterval && this.#isUnlocked;
}

/**
Expand Down Expand Up @@ -398,8 +409,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<
return;
}
await this.detectTokens({
...options,
networkClientId,
accountAddress: options.address,
});
}

Expand Down

0 comments on commit bbbea84

Please sign in to comment.