Skip to content

Commit

Permalink
Fix/fetch price when vsCurreny changed, OK-23681, OK-23564 (#3635)
Browse files Browse the repository at this point in the history
* fix: fetch price when vsCurreny changed, OK-23681, OK-23564

* fix: clean code
  • Loading branch information
qwang1113 authored Oct 12, 2023
1 parent d041248 commit 13da8b5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/kit-bg/src/services/ServiceBootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default class ServiceBootstrap extends ServiceBase {
serviceSwap,
serviceOnboarding,
serviceCloudBackup,
servicePrice,
serviceAllNetwork,
} = this.backgroundApi;

Expand All @@ -112,6 +113,7 @@ export default class ServiceBootstrap extends ServiceBase {
serviceSwap.registerEvents();
serviceAccount.registerEvents();
serviceAllNetwork.registerEvents();
servicePrice.registerEvents();

this.initFetchFiatMoneyRateSchedule();
serviceOnboarding.checkOnboardingStatus();
Expand Down
24 changes: 24 additions & 0 deletions packages/kit-bg/src/services/ServicePrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions packages/kit-bg/src/services/ServiceToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/views/Swap/Main/Observers/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,29 @@ 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) {
backgroundApiProxy.servicePrice.fetchSimpleTokenPrice({
networkId: inputToken.networkId,
accountId: '',
tokenIds: [inputToken.tokenIdOnNetwork],
vsCurrency,
});
}
}, [inputToken]);
}, [inputToken, vsCurrency]);

useEffect(() => {
if (outputToken) {
backgroundApiProxy.servicePrice.fetchSimpleTokenPrice({
networkId: outputToken.networkId,
accountId: '',
tokenIds: [outputToken.tokenIdOnNetwork],
vsCurrency,
});
}
}, [outputToken]);
}, [outputToken, vsCurrency]);

return null;
};
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/views/Swap/hooks/useTokenSelecter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 13da8b5

Please sign in to comment.