Skip to content

Commit

Permalink
Replace initialIdentities with getIdentities
Browse files Browse the repository at this point in the history
The `initialIdentities` option for the `AccountTrackerController` has
been replaced with a `getIdentities` option that returns the identities
on-demand. This bypasses the need to manage a copy of the identity
state in the `AccountTrackerController`, and is a bit more similar to
how this would be done with the new base controller API.
  • Loading branch information
Gudahtt committed Apr 13, 2021
1 parent e0fcf41 commit 146e352
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
33 changes: 26 additions & 7 deletions src/assets/AccountTrackerController.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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: {
Expand All @@ -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();
Expand All @@ -63,7 +82,7 @@ describe('AccountTrackerController', () => {
const controller = new AccountTrackerController(
{
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
initialIdentities: {},
getIdentities: () => ({}),
},
{ provider, interval: 100 },
);
Expand Down
15 changes: 7 additions & 8 deletions src/assets/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class AccountTrackerController extends BaseController<AccountTrackerConfi

private syncAccounts() {
const { accounts } = this.state;
const addresses = Object.keys(this.identities);
const addresses = Object.keys(this.getIdentities());
const existing = Object.keys(accounts);
const newAddresses = addresses.filter((address) => existing.indexOf(address) === -1);
const oldAddresses = existing.filter((address) => addresses.indexOf(address) === -1);
Expand All @@ -68,24 +68,24 @@ export class AccountTrackerController extends BaseController<AccountTrackerConfi
*/
name = 'AccountTrackerController';

private identities: PreferencesState['identities'];
private getIdentities: () => 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<AccountTrackerConfig>,
state?: Partial<AccountTrackerState>,
Expand All @@ -96,9 +96,8 @@ export class AccountTrackerController extends BaseController<AccountTrackerConfi
};
this.defaultState = { accounts: {} };
this.initialize();
this.identities = initialIdentities;
onPreferencesStateChange(({ identities }) => {
this.identities = identities;
this.getIdentities = getIdentities;
onPreferencesStateChange(() => {
this.refresh();
});
this.poll();
Expand Down
1 change: 0 additions & 1 deletion src/user/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export interface PreferencesState extends BaseState {
lostIdentities: { [address: string]: ContactEntry };
selectedAddress: string;
}

/**
* Controller that stores shared settings and exposes convenience methods
*/
Expand Down

0 comments on commit 146e352

Please sign in to comment.