From febad453932654f10e5d7160e54449c1cea6dc40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?qwang1113=20=28=E7=8E=8B=E5=BC=BA=29?= <15064266+qwang1113@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:10:04 +0800 Subject: [PATCH] Fix/ token issues, OK-24190, OK-24180 (#3752) * fix: auto convert usd price with exchange rates if current currency is not usd * fix: history detail page hide add token to account button when current network do not support custom tokens --- packages/kit/src/hooks/useTokens.ts | 12 ++++++++++++ .../TxDetail/components/TxDetailAddTokenBox.tsx | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/hooks/useTokens.ts b/packages/kit/src/hooks/useTokens.ts index d609a1cae11..a695590682c 100644 --- a/packages/kit/src/hooks/useTokens.ts +++ b/packages/kit/src/hooks/useTokens.ts @@ -1,5 +1,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react'; +import { BigNumber } from 'bignumber.js'; import { pick } from 'lodash'; import type { Token } from '@onekeyhq/engine/src/types/token'; @@ -192,7 +193,18 @@ export const useTokenPrice = ({ ? `${networkId}-${tokenIdOnNetwork ?? ''}` : networkId; const prices = useAppSelector((s) => s.tokens.tokenPriceMap); + const exchangeRateMap = useAppSelector((s) => s.fiatMoney.map); const price = prices?.[key]?.[vsCurrency]; + const usdPrice = prices?.[key]?.usd; + + if (typeof price === 'undefined' && usdPrice) { + const convertedValue = new BigNumber(usdPrice).multipliedBy( + exchangeRateMap?.[vsCurrency]?.value ?? 0, + ); + if (!convertedValue.isNaN()) { + return convertedValue.toNumber(); + } + } return price ?? fallback; }; diff --git a/packages/kit/src/views/TxDetail/components/TxDetailAddTokenBox.tsx b/packages/kit/src/views/TxDetail/components/TxDetailAddTokenBox.tsx index 67759fddc8e..ee5c842b25c 100644 --- a/packages/kit/src/views/TxDetail/components/TxDetailAddTokenBox.tsx +++ b/packages/kit/src/views/TxDetail/components/TxDetailAddTokenBox.tsx @@ -23,7 +23,7 @@ function TxDetailAddTokenBox(props: Props) { const [accountTokens, setAccountTokens] = useState([]); const [isAddingTokens, setIsAddingTokens] = useState(false); - const { accountId, networkId } = useActiveWalletAccount(); + const { accountId, networkId, network } = useActiveWalletAccount(); const handleAddToken = useCallback(async () => { if (isAddingTokens) return; @@ -97,7 +97,7 @@ function TxDetailAddTokenBox(props: Props) { if (!tokensNotInList || tokensNotInList.length === 0) return null; - if (isAllNetworks(networkId)) { + if (isAllNetworks(networkId) || !network?.settings?.tokenEnabled) { return null; }