Skip to content

Commit

Permalink
multichain currency rate polling
Browse files Browse the repository at this point in the history
  • Loading branch information
bergeron committed Nov 12, 2024
1 parent de05544 commit 1970754
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 14 deletions.
9 changes: 6 additions & 3 deletions app/components/Views/Root/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useAppTheme, ThemeContext } from '../../../util/theme';
import { ToastContextWrapper } from '../../../component-library/components/Toast';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { isTest } from '../../../util/test/utils';
import { AssetPollingProvider } from '../../hooks/AssetPolling/AssetPollingProvider';

/**
* Top level of the component hierarchy
Expand Down Expand Up @@ -85,9 +86,11 @@ const ConnectedRoot = () => {
<SafeAreaProvider>
<ThemeContext.Provider value={theme}>
<ToastContextWrapper>
<ErrorBoundary view="Root">
<App />
</ErrorBoundary>
<AssetPollingProvider>
<ErrorBoundary view="Root">
<App />
</ErrorBoundary>
</AssetPollingProvider>
</ToastContextWrapper>
</ThemeContext.Provider>
</SafeAreaProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ export class NetworkSettings extends PureComponent {
networkConfigurations.defaultRpcEndpointIndex
] ?? {};

CurrencyRateController.updateExchangeRate(NetworksTicker.mainnet);
CurrencyRateController.updateExchangeRate([NetworksTicker.mainnet]);
NetworkController.setActiveNetwork(networkClientId);

setTimeout(async () => {
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/Settings/NetworksSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class NetworksSettings extends PureComponent {
switchToMainnet = () => {
const { NetworkController, CurrencyRateController } = Engine.context;

CurrencyRateController.updateExchangeRate(NetworksTicker.mainnet);
CurrencyRateController.updateExchangeRate([NetworksTicker.mainnet]);
NetworkController.setProviderType(MAINNET);

setTimeout(async () => {
Expand Down
11 changes: 11 additions & 0 deletions app/components/hooks/AssetPolling/AssetPollingProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { ReactNode } from 'react';
import useCurrencyRatePolling from './useCurrencyRatePolling';

// This provider is a step towards making controller polling fully UI based.
// Eventually, individual UI components will call the use*Polling hooks to
// poll and return particular data. This polls globally in the meantime.
export const AssetPollingProvider = ({ children }: { children: ReactNode }) => {
useCurrencyRatePolling();

return <>{children}</>;
};
39 changes: 39 additions & 0 deletions app/components/hooks/AssetPolling/useCurrencyRatePolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useSelector } from 'react-redux';
import usePolling from '../usePolling';
import { selectNetworkConfigurations } from '../../../selectors/networkController';
import Engine from '../../../core/Engine';
import { selectConversionRate, selectCurrencyRates } from '../../../selectors/currencyRateController';

// Polls native currency prices across networks.
const useCurrencyRatePolling = () => {

// Selectors to determine polling input
const networkConfigurations = useSelector(selectNetworkConfigurations);

// Selectors returning state updated by the polling
const conversionRate = useSelector(selectConversionRate);
const currencyRates = useSelector(selectCurrencyRates);

const nativeCurrencies = [
...new Set(
Object.values(networkConfigurations).map((n) => n.nativeCurrency),
),
];

const { CurrencyRateController } = Engine.context;

usePolling({
startPolling:
CurrencyRateController.startPolling.bind(CurrencyRateController),
stopPollingByPollingToken:
CurrencyRateController.stopPollingByPollingToken.bind(CurrencyRateController),
input: [{nativeCurrencies}],
});

return {
conversionRate,
currencyRates,
};
};

export default useCurrencyRatePolling;
10 changes: 1 addition & 9 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,15 +706,7 @@ export class Engine {
}),
state: initialState.CurrencyRateController,
});
const currentNetworkConfig =
networkController.getNetworkConfigurationByNetworkClientId(
networkController?.state.selectedNetworkClientId,
);
currencyRateController.startPolling({
nativeCurrencies: currentNetworkConfig?.nativeCurrency
? [currentNetworkConfig?.nativeCurrency]
: [],
});

const gasFeeController = new GasFeeController({
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: this.controllerMessenger.getRestricted({
Expand Down
7 changes: 7 additions & 0 deletions app/selectors/currencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ export const selectCurrentCurrency = createSelector(
(currencyRateControllerState: CurrencyRateState) =>
currencyRateControllerState?.currentCurrency,
);

export const selectCurrencyRates = createSelector(
selectCurrencyRateControllerState,
(
currencyRateControllerState: CurrencyRateState,
) => currencyRateControllerState?.currencyRates,
);

0 comments on commit 1970754

Please sign in to comment.