Skip to content

Commit 312e450

Browse files
committed
In networkDidChange event listener, detect tokens if networkClientId is changed instead of chainId, and avoid resetting polling interval
1 parent 04df278 commit 312e450

4 files changed

Lines changed: 13 additions & 12 deletions

File tree

packages/assets-controllers/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Changed
1616

1717
- **BREAKING:** `TokenDetectionController` is merged with `DetectTokensController` from the `metamask-extension` repo. ([#3775](https://github.com/MetaMask/core/pull/3775/))
18-
- **BREAKING:** `TokenDetectionController` now resets its polling interval to the default value of 3 minutes when token detection is triggered by external controller events.
18+
- **BREAKING:** `TokenDetectionController` now resets its polling interval to the default value of 3 minutes when token detection is triggered by external controller events `KeyringController:unlock`, `TokenListController:stateChange`, `PreferencesController:stateChange`, `AccountsController:selectedAccountChange`.
19+
- **BREAKING:** `TokenDetectionController` now responds to `NetworkController:networkDidChange` event only if the `networkClientId` is changed, and does not reset the polling interval in this case.
1920
- **BREAKING:** `TokenDetectionController` cannot initiate polling or token detection if `KeyringController` state is locked. It also subscribes to the `KeyringController:lock` and `KeyringController:unlock` events.
2021
- **BREAKING:** The `detectTokens` method now excludes tokens that are already included in the `TokensController`'s `detectedTokens` list from the batch of incoming tokens it sends to the `TokensController` `addDetectedTokens` method.
2122
- **BREAKING:** The constructor for `TokenDetectionController` expects a new required proprerty `trackMetaMetricsEvent`, which defines the callback that is called in the `detectTokens` method.

packages/assets-controllers/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = merge(baseConfig, {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 88.5,
20+
branches: 88.3,
2121
functions: 95.32,
2222
lines: 96.76,
2323
statements: 96.76,

packages/assets-controllers/src/TokenDetectionController.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ describe('TokenDetectionController', () => {
760760
});
761761

762762
describe('when "disabled" is "false"', () => {
763-
it('should detect new tokens after switching chains', async () => {
763+
it('should detect new tokens after switching network client id', async () => {
764764
const mockGetBalancesInSingleCall = jest.fn().mockResolvedValue({
765765
[sampleTokenA.address]: new BN(1),
766766
});
@@ -859,7 +859,7 @@ describe('TokenDetectionController', () => {
859859
);
860860
});
861861

862-
it('should not detect new tokens if the chain has not changed', async () => {
862+
it('should not detect new tokens if the network client id has not changed', async () => {
863863
const mockGetBalancesInSingleCall = jest.fn().mockResolvedValue({
864864
[sampleTokenA.address]: new BN(1),
865865
});
@@ -898,7 +898,7 @@ describe('TokenDetectionController', () => {
898898

899899
messenger.publish('NetworkController:networkDidChange', {
900900
...defaultNetworkState,
901-
selectedNetworkClientId: 'mainnnet',
901+
selectedNetworkClientId: 'mainnet',
902902
});
903903
await advanceTime({ clock, duration: 1 });
904904

@@ -909,7 +909,7 @@ describe('TokenDetectionController', () => {
909909
});
910910

911911
describe('when "disabled" is "true"', () => {
912-
it('should not detect new tokens after switching chains', async () => {
912+
it('should not detect new tokens after switching network client id', async () => {
913913
const mockGetBalancesInSingleCall = jest.fn().mockResolvedValue({
914914
[sampleTokenA.address]: new BN(1),
915915
});

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ export class TokenDetectionController extends StaticIntervalPollingController<
306306
this.messagingSystem.subscribe(
307307
'NetworkController:networkDidChange',
308308
async ({ selectedNetworkClientId }) => {
309-
this.#networkClientId = selectedNetworkClientId;
310-
const newChainId = this.#getCorrectChainId(selectedNetworkClientId);
311-
const isChainIdChanged = this.#chainId !== newChainId;
309+
const isNetworkClientIdChanged =
310+
this.#networkClientId !== selectedNetworkClientId;
312311

312+
const newChainId = this.#getCorrectChainId(selectedNetworkClientId);
313313
this.#isDetectionEnabledForNetwork =
314314
isTokenDetectionSupportedForNetwork(newChainId);
315315

316-
if (isChainIdChanged && this.#isDetectionEnabledForNetwork) {
317-
this.#chainId = newChainId;
318-
await this.#restartTokenDetection({
316+
if (isNetworkClientIdChanged && this.#isDetectionEnabledForNetwork) {
317+
this.#networkClientId = selectedNetworkClientId;
318+
await this.detectTokens({
319319
networkClientId: this.#networkClientId,
320320
});
321321
}

0 commit comments

Comments
 (0)