diff --git a/packages/kit-bg/src/services/ServiceBootstrap.ts b/packages/kit-bg/src/services/ServiceBootstrap.ts index 5f1ebcbacdb..928d739f582 100644 --- a/packages/kit-bg/src/services/ServiceBootstrap.ts +++ b/packages/kit-bg/src/services/ServiceBootstrap.ts @@ -101,6 +101,7 @@ export default class ServiceBootstrap extends ServiceBase { serviceSwap, serviceOnboarding, serviceCloudBackup, + servicePrice, serviceAllNetwork, } = this.backgroundApi; @@ -112,6 +113,7 @@ export default class ServiceBootstrap extends ServiceBase { serviceSwap.registerEvents(); serviceAccount.registerEvents(); serviceAllNetwork.registerEvents(); + servicePrice.registerEvents(); this.initFetchFiatMoneyRateSchedule(); serviceOnboarding.checkOnboardingStatus(); diff --git a/packages/kit-bg/src/services/ServicePrice.ts b/packages/kit-bg/src/services/ServicePrice.ts index 542a5981e4a..e77c99c8d97 100644 --- a/packages/kit-bg/src/services/ServicePrice.ts +++ b/packages/kit-bg/src/services/ServicePrice.ts @@ -36,6 +36,30 @@ type PriceQueryParams = { @backgroundClass() export default class ServicePrice extends ServiceBase { + @bindThis() + registerEvents() { + appEventBus.on(AppEventBusNames.CurrencyChanged, () => { + const { appSelector } = this.backgroundApi; + const { activeAccountId: accountId, activeNetworkId: networkId } = + appSelector((s) => s.general); + if (!networkId || !accountId) { + return; + } + const accountTokens = appSelector( + (s) => s.tokens.accountTokens?.[networkId]?.[accountId], + ); + const currentVsCurrency = appSelector( + (s) => s.settings.selectedFiatMoneySymbol, + ); + this.fetchSimpleTokenPrice({ + networkId, + accountId, + tokenIds: accountTokens.map((t) => t.tokenIdOnNetwork), + vsCurrency: currentVsCurrency, + }); + }); + } + @backgroundMethod() async fetchSimpleTokenPrice({ networkId, diff --git a/packages/kit-bg/src/services/ServiceToken.ts b/packages/kit-bg/src/services/ServiceToken.ts index 4a65430227d..371a714fcd2 100644 --- a/packages/kit-bg/src/services/ServiceToken.ts +++ b/packages/kit-bg/src/services/ServiceToken.ts @@ -73,12 +73,10 @@ export default class ServiceToken extends ServiceBase { @bindThis() registerEvents() { - // eslint-disable-next-line @typescript-eslint/unbound-method appEventBus.on(AppEventBusNames.NetworkChanged, () => { this.refreshAccountTokens({ includeTop50TokensQuery: true }); }); - // eslint-disable-next-line @typescript-eslint/unbound-method - appEventBus.on(AppEventBusNames.CurrencyChanged, () => { + appEventBus.on(AppEventBusNames.AccountChanged, () => { this.refreshAccountTokens({ includeTop50TokensQuery: true }); }); diff --git a/packages/kit/src/views/Swap/Main/Observers/swap.tsx b/packages/kit/src/views/Swap/Main/Observers/swap.tsx index 406bbe051b3..0e894209af7 100644 --- a/packages/kit/src/views/Swap/Main/Observers/swap.tsx +++ b/packages/kit/src/views/Swap/Main/Observers/swap.tsx @@ -80,6 +80,7 @@ const NetworkStatusObserver = () => { const PriceObserver = () => { const inputToken = useAppSelector((s) => s.swap.inputToken); const outputToken = useAppSelector((s) => s.swap.outputToken); + const vsCurrency = useAppSelector((s) => s.settings.selectedFiatMoneySymbol); useEffect(() => { if (inputToken) { @@ -87,9 +88,10 @@ const PriceObserver = () => { networkId: inputToken.networkId, accountId: '', tokenIds: [inputToken.tokenIdOnNetwork], + vsCurrency, }); } - }, [inputToken]); + }, [inputToken, vsCurrency]); useEffect(() => { if (outputToken) { @@ -97,9 +99,10 @@ const PriceObserver = () => { networkId: outputToken.networkId, accountId: '', tokenIds: [outputToken.tokenIdOnNetwork], + vsCurrency, }); } - }, [outputToken]); + }, [outputToken, vsCurrency]); return null; }; diff --git a/packages/kit/src/views/Swap/hooks/useTokenSelecter.ts b/packages/kit/src/views/Swap/hooks/useTokenSelecter.ts index 20984bb0def..af544d54eb2 100644 --- a/packages/kit/src/views/Swap/hooks/useTokenSelecter.ts +++ b/packages/kit/src/views/Swap/hooks/useTokenSelecter.ts @@ -130,15 +130,19 @@ export function createTokenSelectorUtils( function Observer() { const { networkId, accountId } = useContext(context); const tokens = useAccountTokens(networkId, accountId); + const vsCurrency = useAppSelector( + (s) => s.settings.selectedFiatMoneySymbol, + ); useEffect(() => { if (networkId && accountId) { backgroundApiProxy.servicePrice.fetchSimpleTokenPrice({ accountId, networkId, tokenIds: tokens.map((item) => item.tokenIdOnNetwork), + vsCurrency, }); } - }, [tokens, networkId, accountId]); + }, [tokens, networkId, accountId, vsCurrency]); return null; }