diff --git a/src/assets/AccountTrackerController.test.ts b/src/assets/AccountTrackerController.test.ts index f82204053ef..8500d5b566c 100644 --- a/src/assets/AccountTrackerController.test.ts +++ b/src/assets/AccountTrackerController.test.ts @@ -1,28 +1,39 @@ import { stub, spy } from 'sinon'; import HttpProvider from 'ethjs-provider-http'; import type { ContactEntry } from '../user/AddressBookController'; -import PreferencesController from '../user/PreferencesController'; +import { PreferencesController } from '../user/PreferencesController'; import AccountTrackerController from './AccountTrackerController'; const provider = new HttpProvider('https://ropsten.infura.io/v3/341eacb578dd44a1a049cbc5f6fd4035'); describe('AccountTrackerController', () => { it('should set default state', () => { - const controller = new AccountTrackerController({ onPreferencesStateChange: stub(), initialIdentities: {} }); + const controller = new AccountTrackerController({ + onPreferencesStateChange: stub(), + getIdentities: () => ({}), + }); expect(controller.state).toEqual({ accounts: {}, }); }); it('should throw when provider property is accessed', () => { - const controller = new AccountTrackerController({ onPreferencesStateChange: stub(), initialIdentities: {} }); + const controller = new AccountTrackerController({ + onPreferencesStateChange: stub(), + getIdentities: () => ({}), + }); expect(() => console.log(controller.provider)).toThrow('Property only used for setting'); }); it('should get real balance', async () => { const address = '0xc38bf1ad06ef69f0c04e29dbeb4152b4175f0a8d'; const controller = new AccountTrackerController( - { onPreferencesStateChange: stub(), initialIdentities: { [address]: {} as ContactEntry } }, + { + onPreferencesStateChange: stub(), + getIdentities: () => { + return { [address]: {} as ContactEntry }; + }, + }, { provider }, ); await controller.refresh(); @@ -31,7 +42,12 @@ describe('AccountTrackerController', () => { it('should sync addresses', () => { const controller = new AccountTrackerController( - { onPreferencesStateChange: stub(), initialIdentities: { baz: {} as ContactEntry } }, + { + onPreferencesStateChange: stub(), + getIdentities: () => { + return { baz: {} as ContactEntry }; + }, + }, { provider }, { accounts: { @@ -47,7 +63,10 @@ describe('AccountTrackerController', () => { it('should subscribe to new sibling preference controllers', async () => { const preferences = new PreferencesController(); const controller = new AccountTrackerController( - { onPreferencesStateChange: (listener) => preferences.subscribe(listener), initialIdentities: {} }, + { + onPreferencesStateChange: (listener) => preferences.subscribe(listener), + getIdentities: () => ({}), + }, { provider }, ); controller.refresh = stub(); @@ -63,7 +82,7 @@ describe('AccountTrackerController', () => { const controller = new AccountTrackerController( { onPreferencesStateChange: (listener) => preferences.subscribe(listener), - initialIdentities: {}, + getIdentities: () => ({}), }, { provider, interval: 100 }, ); diff --git a/src/assets/AccountTrackerController.ts b/src/assets/AccountTrackerController.ts index 707b3cf2184..e3420351442 100644 --- a/src/assets/AccountTrackerController.ts +++ b/src/assets/AccountTrackerController.ts @@ -50,7 +50,7 @@ export class AccountTrackerController extends BaseController existing.indexOf(address) === -1); const oldAddresses = existing.filter((address) => addresses.indexOf(address) === -1); @@ -68,24 +68,24 @@ export class AccountTrackerController extends BaseController PreferencesState['identities']; /** * Creates an AccountTracker instance * * @param options * @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes - * @param options.initialIdentities - The initial `identities` state from the Preferences controller + * @param options.getIdentities - Gets the identities from the Preferences store * @param config - Initial options used to configure this controller * @param state - Initial state to set on this controller */ constructor( { onPreferencesStateChange, - initialIdentities, + getIdentities, }: { onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void; - initialIdentities: PreferencesState['identities']; + getIdentities: () => PreferencesState['identities']; }, config?: Partial, state?: Partial, @@ -96,9 +96,8 @@ export class AccountTrackerController extends BaseController { - this.identities = identities; + this.getIdentities = getIdentities; + onPreferencesStateChange(() => { this.refresh(); }); this.poll(); diff --git a/src/user/PreferencesController.ts b/src/user/PreferencesController.ts index 4d952770cf4..076156c7b2f 100644 --- a/src/user/PreferencesController.ts +++ b/src/user/PreferencesController.ts @@ -47,7 +47,6 @@ export interface PreferencesState extends BaseState { lostIdentities: { [address: string]: ContactEntry }; selectedAddress: string; } - /** * Controller that stores shared settings and exposes convenience methods */