Skip to content

Commit

Permalink
Migrate CurrencyRateController to BaseControllerV2
Browse files Browse the repository at this point in the history
The CurrencyRateController has been migrated to the new base
controller. The configuration can now only be set during construction,
as it was never changed at runtime in practice with the old controller.
Similarly, the `disable` function was removed as it wasn't relied upon
in practice.

The TokenRatesController has been detached temporarily from the
CurrencyRateController, just to get this to compile and to get tests
running. This can't be merged as-is, because now the
TokenRatesController doesn't get currency rate updates anymore.
  • Loading branch information
Gudahtt committed Mar 3, 2021
1 parent 531a1c0 commit fa0d1b9
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 167 deletions.
21 changes: 0 additions & 21 deletions src/ComposableController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TokenRatesController from './assets/TokenRatesController';
import { AssetsController } from './assets/AssetsController';
import { NetworkController, NetworksChainId } from './network/NetworkController';
import { AssetsContractController } from './assets/AssetsContractController';
import CurrencyRateController from './assets/CurrencyRateController';

describe('ComposableController', () => {
it('should compose controller state', () => {
Expand All @@ -16,7 +15,6 @@ describe('ComposableController', () => {
new AssetsController(),
new AssetsContractController(),
new EnsController(),
new CurrencyRateController(),
new NetworkController(),
new PreferencesController(),
new TokenRatesController(),
Expand All @@ -35,13 +33,6 @@ describe('ComposableController', () => {
suggestedAssets: [],
tokens: [],
},
CurrencyRateController: {
conversionDate: 0,
conversionRate: 0,
currentCurrency: 'usd',
nativeCurrency: 'ETH',
usdConversionRate: 0,
},
EnsController: {
ensEntries: {},
},
Expand All @@ -67,7 +58,6 @@ describe('ComposableController', () => {
new AssetsController(),
new AssetsContractController(),
new EnsController(),
new CurrencyRateController(),
new NetworkController(),
new PreferencesController(),
new TokenRatesController(),
Expand All @@ -80,9 +70,6 @@ describe('ComposableController', () => {
collectibleContracts: [],
collectibles: [],
contractExchangeRates: {},
conversionDate: 0,
conversionRate: 0,
currentCurrency: 'usd',
ensEntries: {},
featureFlags: {},
frequentRpcList: [],
Expand All @@ -91,13 +78,11 @@ describe('ComposableController', () => {
ignoredTokens: [],
ipfsGateway: 'https://ipfs.io/ipfs/',
lostIdentities: {},
nativeCurrency: 'ETH',
network: 'loading',
provider: { type: 'mainnet', chainId: NetworksChainId.mainnet },
selectedAddress: '',
suggestedAssets: [],
tokens: [],
usdConversionRate: 0,
});
});

Expand All @@ -106,7 +91,6 @@ describe('ComposableController', () => {
new AddressBookController(),
new AssetsController(),
new AssetsContractController(),
new CurrencyRateController(),
new EnsController(),
new NetworkController(),
new PreferencesController(),
Expand Down Expand Up @@ -134,9 +118,6 @@ describe('ComposableController', () => {
collectibleContracts: [],
collectibles: [],
contractExchangeRates: {},
conversionDate: 0,
conversionRate: 0,
currentCurrency: 'usd',
ensEntries: {},
featureFlags: {},
frequentRpcList: [],
Expand All @@ -145,13 +126,11 @@ describe('ComposableController', () => {
ignoredTokens: [],
ipfsGateway: 'https://ipfs.io/ipfs/',
lostIdentities: {},
nativeCurrency: 'ETH',
network: 'loading',
provider: { type: 'mainnet', chainId: NetworksChainId.mainnet },
selectedAddress: '',
suggestedAssets: [],
tokens: [],
usdConversionRate: 0,
});
});

Expand Down
91 changes: 47 additions & 44 deletions src/assets/CurrencyRateController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,108 @@ import CurrencyRateController from './CurrencyRateController';
describe('CurrencyRateController', () => {
it('should set default state', () => {
const fetchExchangeRateStub = stub();
const controller = new CurrencyRateController({}, {}, fetchExchangeRateStub);
const controller = new CurrencyRateController({}, undefined, fetchExchangeRateStub);
expect(controller.state).toEqual({
conversionDate: 0,
conversionRate: 0,
currentCurrency: 'usd',
nativeCurrency: 'ETH',
usdConversionRate: 0,
pendingCurrentCurrency: null,
pendingNativeCurrency: null,
usdConversionRate: null,
});

controller.disabled = true;
controller.destroy();
});

it('should initialize with the default config', () => {
const fetchExchangeRateStub = stub();
const controller = new CurrencyRateController({}, {}, fetchExchangeRateStub);
expect(controller.config).toEqual({
currentCurrency: 'usd',
disabled: false,
interval: 180000,
nativeCurrency: 'ETH',
includeUSDRate: false,
});

controller.disabled = true;
});

it('should initialize with the currency in state', () => {
it('should initialize with initial state', () => {
const fetchExchangeRateStub = stub();
const existingState = { currentCurrency: 'rep' };
const controller = new CurrencyRateController({}, existingState, fetchExchangeRateStub);
expect(controller.config).toEqual({
const controller = new CurrencyRateController(existingState, undefined, fetchExchangeRateStub);
expect(controller.state).toEqual({
conversionDate: 0,
conversionRate: 0,
currentCurrency: 'rep',
disabled: false,
interval: 180000,
nativeCurrency: 'ETH',
includeUSDRate: false,
pendingCurrentCurrency: null,
pendingNativeCurrency: null,
usdConversionRate: null,
});

controller.disabled = true;
controller.destroy();
});

it('should poll and update rate in the right interval', async () => {
const fetchExchangeRateStub = stub();
const controller = new CurrencyRateController({ interval: 100 }, {}, fetchExchangeRateStub);
const controller = new CurrencyRateController({}, { interval: 100 }, fetchExchangeRateStub);

await new Promise<void>((resolve) => setTimeout(() => resolve(), 1));
expect(fetchExchangeRateStub.called).toBe(true);
expect(fetchExchangeRateStub.calledTwice).toBe(false);
await new Promise<void>((resolve) => setTimeout(() => resolve(), 150));
expect(fetchExchangeRateStub.calledTwice).toBe(true);

controller.disabled = true;
});

it('should not update rates if disabled', async () => {
const fetchExchangeRateStub = stub().resolves({});
const controller = new CurrencyRateController({ interval: 10 }, {}, fetchExchangeRateStub);
controller.disabled = true;

await controller.updateExchangeRate();
expect(fetchExchangeRateStub.called).toBe(false);
controller.destroy();
});

it('should clear previous interval', () => {
const fetchExchangeRateStub = stub();
const mock = stub(global, 'clearTimeout');
const controller = new CurrencyRateController({ interval: 1337 }, {}, fetchExchangeRateStub);
const controller = new CurrencyRateController({}, { interval: 1337 }, fetchExchangeRateStub);
return new Promise<void>((resolve) => {
setTimeout(() => {
controller.poll(1338);
controller.poll();
expect(mock.called).toBe(true);
mock.restore();

controller.disabled = true;
controller.destroy();
resolve();
}, 100);
});
});

it('should update currency', async () => {
it('should update exchange rate', async () => {
const fetchExchangeRateStub = stub().resolves({ conversionRate: 10 });
const controller = new CurrencyRateController({ interval: 10 }, {}, fetchExchangeRateStub);
const controller = new CurrencyRateController({}, { interval: 10 }, fetchExchangeRateStub);
expect(controller.state.conversionRate).toEqual(0);
await controller.updateExchangeRate();
expect(controller.state.conversionRate).toEqual(10);

controller.disabled = true;
controller.destroy();
});

it('should update current currency', async () => {
const fetchExchangeRateStub = stub().resolves({ conversionRate: 10 });
const controller = new CurrencyRateController({}, { interval: 10 }, fetchExchangeRateStub);
expect(controller.state.conversionRate).toEqual(0);
await controller.setCurrentCurrency('CAD');
expect(controller.state.conversionRate).toEqual(10);

controller.destroy();
});

it('should add usd rate to state when includeUSDRate is configured true', async () => {
it('should update native currency', async () => {
const fetchExchangeRateStub = stub().resolves({ conversionRate: 10 });
const controller = new CurrencyRateController({}, { interval: 10 }, fetchExchangeRateStub);
expect(controller.state.conversionRate).toEqual(0);
await controller.setNativeCurrency('xDAI');
expect(controller.state.conversionRate).toEqual(10);

controller.destroy();
});

it('should add usd rate to state when includeUsdRate is configured true', async () => {
const fetchExchangeRateStub = stub().resolves({});
const controller = new CurrencyRateController(
{ includeUSDRate: true, currentCurrency: 'xyz' },
{},
{ currentCurrency: 'xyz' },
{ includeUsdRate: true },
fetchExchangeRateStub,
);

await controller.updateExchangeRate();

expect(fetchExchangeRateStub.alwaysCalledWithExactly('xyz', 'ETH', true)).toBe(true);

controller.destroy();
});
});
Loading

0 comments on commit fa0d1b9

Please sign in to comment.