Skip to content

Commit

Permalink
sync selected fiat currency
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Jan 22, 2025
1 parent 2add199 commit a638f62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/core/Engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ import { removeAccountsFromPermissions } from '../Permissions';
import { keyringSnapPermissionsBuilder } from '../SnapKeyring/keyringSnapsPermissions';
import { createMultichainBalancesController } from './controllers/MultichainBalancesController/utils';
import { createMultichainRatesController } from './controllers/RatesController/utils';
import { setupCurrencyRateSync } from './controllers/RatesController/subscriptions';
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
import { HandleSnapRequestArgs } from '../Snaps/types';
Expand Down Expand Up @@ -413,6 +414,9 @@ export class Engine {
messenger: multichainRatesControllerMessenger,
initialState: initialState.RatesController,
});

// Set up currency rate sync
setupCurrencyRateSync(this.controllerMessenger, multichainRatesController);
///: END:ONLY_INCLUDE_IF

const nftController = new NftController({
Expand Down
37 changes: 37 additions & 0 deletions app/core/Engine/controllers/RatesController/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
RatesController,
CurrencyRateState,
CurrencyRateController,
} from '@metamask/assets-controllers';
import Logger from '../../../../util/Logger';

/**
* Sets up subscription to sync CurrencyRateController changes with RatesController
* @param controllerMessenger - The main controller messenger
* @param ratesController - The RatesController instance to sync with
*/
export const setupCurrencyRateSync = (
controllerMessenger: {
subscribe: (
eventName: string,
handler: (state: CurrencyRateState) => void,
) => void;
},
ratesController: RatesController,
): void => {
controllerMessenger.subscribe(
`${CurrencyRateController.name}:stateChange`,
(state: CurrencyRateState) => {
if (state.currentCurrency) {
ratesController
.setFiatCurrency(state.currentCurrency)
.catch((error) => {
Logger.error(
error as Error,
'RatesController: Failed to sync fiat currency with CurrencyRateController',
);
});
}
},
);
};
2 changes: 2 additions & 0 deletions app/core/Engine/controllers/RatesController/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
RatesController,
RatesControllerState,
RatesControllerMessenger,
CurrencyRateState,
} from '@metamask/assets-controllers';
import Logger from '../../../../util/Logger';

Expand All @@ -25,6 +26,7 @@ export const createMultichainRatesController = ({
state: initialState ?? {},
includeUsdRate: true,
});

return multichainRatesController;
} catch (error) {
Logger.error(error as Error, 'Failed to initialize RatesController');
Expand Down

0 comments on commit a638f62

Please sign in to comment.