Skip to content

Commit

Permalink
refactor: remove keyringMemStore from DetectTokensController
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Sep 5, 2023
1 parent 6ee90ac commit 1af59a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
29 changes: 7 additions & 22 deletions app/scripts/controllers/detect-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default class DetectTokensController {
interval = DEFAULT_INTERVAL,
preferences,
network,
keyringMemStore,
tokenList,
tokensController,
assetsContractController = null,
Expand All @@ -52,7 +51,6 @@ export default class DetectTokensController {
this.preferences = preferences;
this.interval = interval;
this.network = network;
this.keyringMemStore = keyringMemStore;
this.tokenList = tokenList;
this.useTokenDetection =
this.preferences?.store.getState().useTokenDetection;
Expand Down Expand Up @@ -91,6 +89,13 @@ export default class DetectTokensController {
this.restartTokenDetection({ chainId: this.chainId });
}
});
messenger.subscribe('KeyringController:unlock', () => {
this.isUnlocked = true;
this.restartTokenDetection();
});
messenger.subscribe('KeyringController:lock', () => {
this.isUnlocked = false;
});
}

/**
Expand Down Expand Up @@ -236,26 +241,6 @@ export default class DetectTokensController {
}, interval);
}

/**
* In setter when isUnlocked is updated to true, detectNewTokens and restart polling
*
* @type {object}
*/
set keyringMemStore(keyringMemStore) {
if (!keyringMemStore) {
return;
}
this._keyringMemStore = keyringMemStore;
this._keyringMemStore.subscribe(({ isUnlocked }) => {
if (this.isUnlocked !== isUnlocked) {
this.isUnlocked = isUnlocked;
if (isUnlocked) {
this.restartTokenDetection();
}
}
});
}

/**
* @type {object}
*/
Expand Down
22 changes: 9 additions & 13 deletions app/scripts/controllers/detect-tokens.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { strict as assert } from 'assert';
import sinon from 'sinon';
import nock from 'nock';
import { ObservableStore } from '@metamask/obs-store';
import BigNumber from 'bignumber.js';
import { ControllerMessenger } from '@metamask/base-controller';
import {
Expand All @@ -19,14 +18,17 @@ import PreferencesController from './preferences';
function buildMessenger() {
return new ControllerMessenger().getRestricted({
name: 'DetectTokensController',
allowedEvents: ['NetworkController:stateChange'],
allowedEvents: [
'NetworkController:stateChange',
'KeyringController:lock',
'KeyringController:unlock',
],
});
}

describe('DetectTokensController', function () {
let sandbox,
assetsContractController,
keyringMemStore,
network,
preferences,
provider,
Expand Down Expand Up @@ -198,7 +200,6 @@ describe('DetectTokensController', function () {
.reply(200, { error: 'ChainId 3 is not supported' })
.persist();

keyringMemStore = new ObservableStore({ isUnlocked: false });
const networkControllerMessenger = new ControllerMessenger();
network = new NetworkController({
messenger: networkControllerMessenger,
Expand Down Expand Up @@ -275,7 +276,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
Expand Down Expand Up @@ -312,7 +312,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
Expand All @@ -336,7 +335,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
Expand Down Expand Up @@ -388,7 +386,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
Expand Down Expand Up @@ -447,7 +444,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
Expand All @@ -463,19 +459,21 @@ describe('DetectTokensController', function () {

it('should trigger detect new tokens when submit password', async function () {
sandbox.useFakeTimers();
const messenger = buildMessenger();
const controller = new DetectTokensController({
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
});
controller.isOpen = true;
controller.selectedAddress = '0x0';
const stub = sandbox.stub(controller, 'detectNewTokens');
await controller._keyringMemStore.updateState({ isUnlocked: true });

messenger.publish('KeyringController:unlock');

sandbox.assert.called(stub);
});

Expand All @@ -486,7 +484,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokenList: tokenListController,
tokensController,
assetsContractController,
Expand All @@ -508,7 +505,6 @@ describe('DetectTokensController', function () {
messenger: buildMessenger(),
preferences,
network,
keyringMemStore,
tokensController,
assetsContractController,
});
Expand Down
7 changes: 5 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,15 +1116,18 @@ export default class MetamaskController extends EventEmitter {
const detectTokensControllerMessenger =
this.controllerMessenger.getRestricted({
name: 'DetectTokensController',
allowedEvents: ['NetworkController:stateChange'],
allowedEvents: [
'NetworkController:stateChange',
'KeyringController:lock',
'KeyringController:unlock',
],
});
this.detectTokensController = new DetectTokensController({
messenger: detectTokensControllerMessenger,
preferences: this.preferencesController,
tokensController: this.tokensController,
assetsContractController: this.assetsContractController,
network: this.networkController,
keyringMemStore: this.keyringController.memStore,
tokenList: this.tokenListController,
trackMetaMetricsEvent: this.metaMetricsController.trackEvent.bind(
this.metaMetricsController,
Expand Down

0 comments on commit 1af59a7

Please sign in to comment.