Skip to content

Commit

Permalink
refactor: rename controller
Browse files Browse the repository at this point in the history
  • Loading branch information
montelaidev committed Jun 20, 2024
1 parent a7e9c4c commit 0ccb56c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/scripts/lib/setupSentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export const SENTRY_BACKGROUND_STATE = {
PushPlatformNotificationsController: {
fcmToken: false,
},
RatesController: {
MultichainRatesController: {
fiatCurrency: true,
rates: true,
cryptocurrencies: true,
Expand Down
21 changes: 11 additions & 10 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,12 +925,13 @@ export default class MetamaskController extends EventEmitter {
state: initState.AccountOrderController,
});

const ratesControllerMessenger = this.controllerMessenger.getRestricted({
name: 'RatesController',
});
this.ratesController = new RatesController({
state: initState.RatesController,
messenger: ratesControllerMessenger,
const multichainRatesControllerMessenger =
this.controllerMessenger.getRestricted({
name: 'RatesController',
});
this.multichainRatesController = new RatesController({
state: initState.MultichainRatesController,
messenger: multichainRatesControllerMessenger,
includeUsdRate: true,
fetchMultiExchangeRate,
});
Expand All @@ -939,10 +940,10 @@ export default class MetamaskController extends EventEmitter {
'AccountsController:selectedAccountChange',
(selectedAccount) => {
if (isEvmAccountType(selectedAccount.type)) {
this.ratesController.stop();
this.multichainRatesController.stop();
return;
}
this.ratesController.start();
this.multichainRatesController.start();
},
);

Expand Down Expand Up @@ -2219,7 +2220,7 @@ export default class MetamaskController extends EventEmitter {
PhishingController: this.phishingController,
SelectedNetworkController: this.selectedNetworkController,
LoggingController: this.loggingController,
RatesController: this.ratesController,
MultichainRatesController: this.multichainRatesController,
///: BEGIN:ONLY_INCLUDE_IF(snaps)
SnapController: this.snapController,
CronjobController: this.cronjobController,
Expand Down Expand Up @@ -2275,7 +2276,7 @@ export default class MetamaskController extends EventEmitter {
SelectedNetworkController: this.selectedNetworkController,
LoggingController: this.loggingController,
TxController: this.txController,
RatesController: this.ratesController,
MultichainRatesController: this.multichainRatesController,
///: BEGIN:ONLY_INCLUDE_IF(snaps)
SnapController: this.snapController,
CronjobController: this.cronjobController,
Expand Down
52 changes: 30 additions & 22 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ describe('MetaMaskController', () => {
});
});

describe('start/stop rates controller', () => {
describe('MultichainRatesController start/stop', () => {
const mockEVMAccount = createMockInternalAccount();
const mockNonEvmAccount = {
...mockEVMAccount,
Expand All @@ -2041,60 +2041,68 @@ describe('MetaMaskController', () => {
};

beforeEach(() => {
jest.spyOn(metamaskController.ratesController, 'start');
jest.spyOn(metamaskController.ratesController, 'stop');
jest.spyOn(metamaskController.multichainRatesController, 'start');
jest.spyOn(metamaskController.multichainRatesController, 'stop');
});

afterEach(() => {
jest.clearAllMocks();
});

it('starts rates controller if selected account is non-EVM', async () => {
expect(metamaskController.ratesController.start).not.toHaveBeenCalled();
it('starts MultichainRatesController if selected account is non-EVM', async () => {
expect(
metamaskController.multichainRatesController.start,
).not.toHaveBeenCalled();

metamaskController.controllerMessenger.publish(
'AccountsController:selectedAccountChange',
mockNonEvmAccount,
);

expect(metamaskController.ratesController.start).toHaveBeenCalledTimes(
1,
);
expect(
metamaskController.multichainRatesController.start,
).toHaveBeenCalledTimes(1);
});

it('stops rates controller if selected account is EVM', async () => {
expect(metamaskController.ratesController.start).not.toHaveBeenCalled();
it('stops MultichainRatesController if selected account is EVM', async () => {
expect(
metamaskController.multichainRatesController.start,
).not.toHaveBeenCalled();

metamaskController.controllerMessenger.publish(
'AccountsController:selectedAccountChange',
mockNonEvmAccount,
);

expect(metamaskController.ratesController.start).toHaveBeenCalledTimes(
1,
);
expect(
metamaskController.multichainRatesController.start,
).toHaveBeenCalledTimes(1);

metamaskController.controllerMessenger.publish(
'AccountsController:selectedAccountChange',
mockEVMAccount,
);
expect(metamaskController.ratesController.start).toHaveBeenCalledTimes(
1,
);
expect(metamaskController.ratesController.stop).toHaveBeenCalledTimes(
1,
);
expect(
metamaskController.multichainRatesController.start,
).toHaveBeenCalledTimes(1);
expect(
metamaskController.multichainRatesController.stop,
).toHaveBeenCalledTimes(1);
});

it('does not start rates controller if selected account is EVM', async () => {
expect(metamaskController.ratesController.start).not.toHaveBeenCalled();
it('does not start MultichainRatesController if selected account is EVM', async () => {
expect(
metamaskController.multichainRatesController.start,
).not.toHaveBeenCalled();

metamaskController.controllerMessenger.publish(
'AccountsController:selectedAccountChange',
mockEVMAccount,
);

expect(metamaskController.ratesController.start).not.toHaveBeenCalled();
expect(
metamaskController.multichainRatesController.start,
).not.toHaveBeenCalled();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
"isUpdatingMetamaskNotificationsAccount": "object",
"isCheckingAccountsPresence": "boolean"
},
"MultichainRatesController": {
"fiatCurrency": "usd",
"rates": { "btc": { "conversionDate": 0, "conversionRate": "0" } },
"cryptocurrencies": ["btc"]
},
"NameController": { "names": "object", "nameSources": "object" },
"NetworkController": {
"selectedNetworkClientId": "string",
Expand Down Expand Up @@ -209,11 +214,6 @@
"selectedAddress": "string"
},
"PushPlatformNotificationsController": { "fcmToken": "string" },
"RatesController": {
"fiatCurrency": "usd",
"rates": { "btc": { "conversionDate": 0, "conversionRate": "0" } },
"cryptocurrencies": ["btc"]
},
"SelectedNetworkController": { "domains": "object" },
"SignatureController": {
"unapprovedMsgs": "object",
Expand Down

0 comments on commit 0ccb56c

Please sign in to comment.