From b7e418208168d6a8cbae4346d971a916c89e1e2a Mon Sep 17 00:00:00 2001 From: huhuanming Date: Tue, 3 Dec 2024 09:55:38 +0800 Subject: [PATCH] refactor: replace the staking homepage and currency API. (#6289) --- .../v4ToV5Migration/V4MigrationForSettings.ts | 9 +-- .../src/services/ServiceAccountProfile.ts | 4 +- .../kit-bg/src/services/ServiceSetting.ts | 16 ++-- .../kit-bg/src/services/ServiceStaking.ts | 77 ++++++++++++++++--- .../kit-bg/src/states/jotai/atoms/currency.ts | 4 +- .../kit/src/components/Currency/index.tsx | 14 ++-- packages/kit/src/views/Earn/EarnHome.tsx | 27 ++++++- .../views/Setting/pages/Currency/index.tsx | 5 +- packages/shared/types/staking.ts | 4 +- 9 files changed, 121 insertions(+), 39 deletions(-) diff --git a/packages/kit-bg/src/migrations/v4ToV5Migration/V4MigrationForSettings.ts b/packages/kit-bg/src/migrations/v4ToV5Migration/V4MigrationForSettings.ts index 4e13cc87b51..9f1e90019db 100644 --- a/packages/kit-bg/src/migrations/v4ToV5Migration/V4MigrationForSettings.ts +++ b/packages/kit-bg/src/migrations/v4ToV5Migration/V4MigrationForSettings.ts @@ -88,11 +88,10 @@ export class V4MigrationForSettings extends V4MigrationManagerBase { if (v4Settings?.selectedFiatMoneySymbol) { await this.v4dbHubs.logger.runAsyncWithCatch( async () => { - const currencyList = - await this.backgroundApi.serviceSetting.getCurrencyList(); - const existingCurrencyItem = currencyList.find( - (i) => i.id === v4Settings.selectedFiatMoneySymbol, - ); + const currencyMap = + await this.backgroundApi.serviceSetting.getCurrencyMap(); + const existingCurrencyItem = + currencyMap[v4Settings.selectedFiatMoneySymbol]; if (existingCurrencyItem) { await this.backgroundApi.serviceSetting.setCurrency({ id: existingCurrencyItem.id, diff --git a/packages/kit-bg/src/services/ServiceAccountProfile.ts b/packages/kit-bg/src/services/ServiceAccountProfile.ts index b92b4d254ab..b5c54c5327f 100644 --- a/packages/kit-bg/src/services/ServiceAccountProfile.ts +++ b/packages/kit-bg/src/services/ServiceAccountProfile.ts @@ -441,12 +441,12 @@ class ServiceAccountProfile extends ServiceBase { }) { const { currency, value, updateAll } = params; - const currencyItems = (await currencyPersistAtom.get()).currencyItems; + const currencyMap = (await currencyPersistAtom.get()).currencyMap; let usdValue: Record = value; if (currency !== 'usd') { - const currencyInfo = currencyItems.find((item) => item.id === currency); + const currencyInfo = currencyMap[currency]; if (!currencyInfo) { throw new Error('Currency not found'); diff --git a/packages/kit-bg/src/services/ServiceSetting.ts b/packages/kit-bg/src/services/ServiceSetting.ts index a235441801c..f392f48f986 100644 --- a/packages/kit-bg/src/services/ServiceSetting.ts +++ b/packages/kit-bg/src/services/ServiceSetting.ts @@ -158,17 +158,17 @@ class ServiceSetting extends ServiceBase { })); } - _getCurrencyList = memoizee( + _getCurrencyMap = memoizee( async () => { const client = await this.getClient(EServiceEndpointEnum.Utility); - const res = await client.get<{ data: ICurrencyItem[] }>( - '/utility/v1/currency/exchange-rates', + const res = await client.get<{ data: Record }>( + '/utility/v1/currency/exchange-rates/map', ); return res.data.data; }, { promise: true, - maxAge: timerUtils.getTimeDurationMs({ minute: 5 }), + maxAge: timerUtils.getTimeDurationMs({ minute: 10 }), }, ); @@ -180,15 +180,15 @@ class ServiceSetting extends ServiceBase { } @backgroundMethod() - public async getCurrencyList(): Promise { - return this._getCurrencyList(); + public async getCurrencyMap() { + return this._getCurrencyMap(); } @backgroundMethod() public async fetchCurrencyList() { - const currencyItems = await this._getCurrencyList(); + const currencyMap = await this._getCurrencyMap(); await currencyPersistAtom.set({ - currencyItems, + currencyMap, }); } diff --git a/packages/kit-bg/src/services/ServiceStaking.ts b/packages/kit-bg/src/services/ServiceStaking.ts index 1320f83a6fb..8dff6c86b1e 100644 --- a/packages/kit-bg/src/services/ServiceStaking.ts +++ b/packages/kit-bg/src/services/ServiceStaking.ts @@ -1,3 +1,5 @@ +import BigNumber from 'bignumber.js'; + import { isTaprootAddress } from '@onekeyhq/core/src/chains/btc/sdkBtc'; import { backgroundClass, @@ -27,6 +29,7 @@ import type { IClaimRecordParams, IClaimableListResponse, IEarnAccountResponse, + IEarnAccountToken, IEarnAccountTokenResponse, IEarnBabylonTrackingItem, IEarnEstimateAction, @@ -491,28 +494,27 @@ class ServiceStaking extends ServiceBase { }[], ) { const client = await this.getClient(EServiceEndpointEnum.Earn); - const response = await client.post<{ - data: IEarnAccountResponse; - }>(`/earn/v1/account/list`, { accounts: params }); - const resp = response.data.data; const result: IEarnAccountTokenResponse = { - totalFiatValue: resp.totalFiatValue, - earnings24h: resp.earnings24h, accounts: [], }; + const tokensResponse = await client.post<{ + data: { tokens: IEarnAccountToken[] }; + }>(`/earn/v1/recommend`, { accounts: params }); for (const account of params) { result.accounts.push({ ...account, tokens: - resp.tokens?.filter((i) => i.networkId === account.networkId) || [], + tokensResponse.data.data.tokens?.filter( + (i) => i.networkId === account.networkId, + ) || [], }); } return result; } @backgroundMethod() - async fetchAllNetworkAssets({ + async getEarnAvailableAccountsParams({ accountId, networkId, assets, @@ -550,7 +552,64 @@ class ServiceStaking extends ServiceBase { ]), ).values(), ); - return this.getAccountAsset(uniqueAccountParams); + return uniqueAccountParams; + } + + @backgroundMethod() + async fetchAccountOverview(params: { + accountId: string; + networkId: string; + assets: IAvailableAsset[]; + }) { + const accounts = await this.getEarnAvailableAccountsParams(params); + const client = await this.getClient(EServiceEndpointEnum.Earn); + const overviewData = await Promise.all( + accounts.map((account) => + client.get<{ + data: IEarnAccountResponse; + }>(`/earn/v1/overview`, { params: account }), + ), + ); + + const { totalFiatValue, earnings24h } = overviewData.reduce( + (prev, item) => { + prev.totalFiatValue = prev.totalFiatValue.plus( + BigNumber(item.data.data.totalFiatValue || 0), + ); + prev.earnings24h = prev.earnings24h.plus( + BigNumber(item.data.data.earnings24h || 0), + ); + return prev; + }, + { + totalFiatValue: BigNumber(0), + earnings24h: BigNumber(0), + }, + ); + // const resp = response.data.data; + + return { + totalFiatValue: totalFiatValue.toFixed(), + earnings24h: earnings24h.toFixed(), + }; + } + + @backgroundMethod() + async fetchAllNetworkAssets({ + accountId, + networkId, + assets, + }: { + accountId: string; + networkId: string; + assets: IAvailableAsset[]; + }) { + const accounts = await this.getEarnAvailableAccountsParams({ + accountId, + networkId, + assets, + }); + return this.getAccountAsset(accounts); } @backgroundMethod() diff --git a/packages/kit-bg/src/states/jotai/atoms/currency.ts b/packages/kit-bg/src/states/jotai/atoms/currency.ts index f5bd79e0346..6eecb6977e9 100644 --- a/packages/kit-bg/src/states/jotai/atoms/currency.ts +++ b/packages/kit-bg/src/states/jotai/atoms/currency.ts @@ -4,13 +4,13 @@ import { EAtomNames } from '../atomNames'; import { globalAtom } from '../utils'; export type ICurrencyPersistAtom = { - currencyItems: ICurrencyItem[]; + currencyMap: Record; }; export const { target: currencyPersistAtom, use: useCurrencyPersistAtom } = globalAtom({ persist: true, name: EAtomNames.currencyPersistAtom, initialValue: { - currencyItems: [], + currencyMap: {}, }, }); diff --git a/packages/kit/src/components/Currency/index.tsx b/packages/kit/src/components/Currency/index.tsx index d2a278aa654..08e618cd3dd 100644 --- a/packages/kit/src/components/Currency/index.tsx +++ b/packages/kit/src/components/Currency/index.tsx @@ -21,16 +21,16 @@ function BasicCurrency({ children, ...props }: ICurrencyProps) { - const [{ currencyItems }] = useCurrencyPersistAtom(); + const [{ currencyMap }] = useCurrencyPersistAtom(); const [{ currencyInfo }] = useSettingsPersistAtom(); const sourceCurrencyInfo = useMemo( - () => currencyItems.find((i) => i.id === sourceCurrency), - [currencyItems, sourceCurrency], + () => currencyMap[sourceCurrency], + [currencyMap, sourceCurrency], + ); + const targetCurrencyInfo = useMemo( + () => currencyMap[targetCurrency ?? currencyInfo.id], + [currencyInfo.id, currencyMap, targetCurrency], ); - const targetCurrencyInfo = useMemo(() => { - const currencyId = targetCurrency ?? currencyInfo.id; - return currencyItems.find((i) => i.id === currencyId); - }, [currencyInfo.id, currencyItems, targetCurrency]); const value = useMemo( () => diff --git a/packages/kit/src/views/Earn/EarnHome.tsx b/packages/kit/src/views/Earn/EarnHome.tsx index 337b95e5308..58b18197630 100644 --- a/packages/kit/src/views/Earn/EarnHome.tsx +++ b/packages/kit/src/views/Earn/EarnHome.tsx @@ -634,7 +634,7 @@ function BasicEarnHome() { .then(actions.current.updateAvailableAssets); }); } - const earnAccountData = actions.current.getEarnAccount(totalFiatMapKey); + const fetchAndUpdateAction = async () => { const earnAccount = await backgroundApiProxy.serviceStaking.fetchAllNetworkAssets({ @@ -642,17 +642,40 @@ function BasicEarnHome() { accountId: account?.id ?? '', networkId: network?.id ?? '', }); + const earnAccountData = actions.current.getEarnAccount(totalFiatMapKey); actions.current.updateEarnAccounts({ key: totalFiatMapKey, - earnAccount, + earnAccount: { + ...earnAccountData, + ...earnAccount, + }, }); }; + const fetchAndUpdateOverview = async () => { + const overviewData = + await backgroundApiProxy.serviceStaking.fetchAccountOverview({ + assets, + accountId: account?.id ?? '', + networkId: network?.id ?? '', + }); + const earnAccountData = actions.current.getEarnAccount(totalFiatMapKey); + actions.current.updateEarnAccounts({ + key: totalFiatMapKey, + earnAccount: { + accounts: earnAccountData?.accounts || [], + ...overviewData, + }, + }); + }; + const earnAccountData = actions.current.getEarnAccount(totalFiatMapKey); if (earnAccountData) { setTimeout(() => { + void fetchAndUpdateOverview(); void fetchAndUpdateAction(); }); } else { await fetchAndUpdateAction(); + void fetchAndUpdateOverview(); } return { loaded: true }; }, diff --git a/packages/kit/src/views/Setting/pages/Currency/index.tsx b/packages/kit/src/views/Setting/pages/Currency/index.tsx index b792f244787..45ef2921221 100644 --- a/packages/kit/src/views/Setting/pages/Currency/index.tsx +++ b/packages/kit/src/views/Setting/pages/Currency/index.tsx @@ -69,8 +69,9 @@ export default function SettingCurrencyModal() { currencyRef.current as ICurrencyItem, ); const intl = useIntl(); - const [{ currencyItems }] = useCurrencyPersistAtom(); + const [{ currencyMap }] = useCurrencyPersistAtom(); const sections = useMemo(() => { + const currencyItems = Object.values(currencyMap); if (currencyItems.length === 0) { return []; } @@ -102,7 +103,7 @@ export default function SettingCurrencyModal() { data: section.fiat, }, ].filter((item) => item.data.length > 0); - }, [currencyItems, text, intl]); + }, [currencyMap, intl, text]); const handlePress = useCallback((item: ICurrencyItem) => { setCurrency(item); diff --git a/packages/shared/types/staking.ts b/packages/shared/types/staking.ts index bc52893d2b3..7ad3b31ab1c 100644 --- a/packages/shared/types/staking.ts +++ b/packages/shared/types/staking.ts @@ -307,8 +307,8 @@ export type IEarnAccount = { }; export type IEarnAccountTokenResponse = { - totalFiatValue: string; - earnings24h: string; + totalFiatValue?: string; + earnings24h?: string; accounts: IEarnAccount[]; };