Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update currency rate controller polling input #4839

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('CurrencyRateController', () => {
messenger,
});

controller.startPolling({ networkClientId: 'mainnet' });
controller.startPolling({ nativeCurrency: 'ETH' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why we are using the symbol here rather than an address of some sort?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the currency rate controller handles prices for the native gas token of each chain. They're identified by symbol.

await advanceTime({ clock, duration: 0 });
expect(fetchExchangeRateStub).toHaveBeenCalledTimes(1);
expect(controller.state.currencyRates).toStrictEqual({
Expand Down Expand Up @@ -192,7 +192,7 @@ describe('CurrencyRateController', () => {
messenger,
});

controller.startPolling({ networkClientId: 'sepolia' });
controller.startPolling({ nativeCurrency: 'ETH' });

await advanceTime({ clock, duration: 0 });

Expand All @@ -217,15 +217,15 @@ describe('CurrencyRateController', () => {
fetchExchangeRate: fetchExchangeRateStub,
messenger,
});
controller.startPolling({ networkClientId: 'sepolia' });
controller.startPolling({ nativeCurrency: 'ETH' });
await advanceTime({ clock, duration: 0 });

controller.stopAllPolling();

// called once upon initial start
expect(fetchExchangeRateStub).toHaveBeenCalledTimes(1);

controller.startPolling({ networkClientId: 'sepolia' });
controller.startPolling({ nativeCurrency: 'ETH' });
await advanceTime({ clock, duration: 0 });

expect(fetchExchangeRateStub).toHaveBeenCalledTimes(2);
Expand Down
17 changes: 5 additions & 12 deletions packages/assets-controllers/src/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
TESTNET_TICKER_SYMBOLS,
FALL_BACK_VS_CURRENCY,
} from '@metamask/controller-utils';
import type {
NetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import type { NetworkControllerGetNetworkClientByIdAction } from '@metamask/network-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import { Mutex } from 'async-mutex';

Expand Down Expand Up @@ -80,7 +77,7 @@ const defaultState = {

/** The input to start polling for the {@link CurrencyRateController} */
type CurrencyRatePollingInput = {
networkClientId: NetworkClientId;
nativeCurrency: string;
};

/**
Expand Down Expand Up @@ -243,16 +240,12 @@ export class CurrencyRateController extends StaticIntervalPollingController<Curr
* Updates exchange rate for the current currency.
*
* @param input - The input for the poll.
* @param input.networkClientId - The network client ID used to get a ticker value.
* @param input.nativeCurrency - The native currency symbol to poll prices for.
*/
async _executePoll({
networkClientId,
nativeCurrency,
}: CurrencyRatePollingInput): Promise<void> {
const networkClient = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
networkClientId,
);
await this.updateExchangeRate(networkClient.configuration.ticker);
await this.updateExchangeRate(nativeCurrency);
}
}

Expand Down
Loading