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

Fix/fetch price when vsCurreny changed, OK-23681, OK-23564 #3635

Merged
merged 2 commits into from
Oct 12, 2023
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
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