Skip to content

Commit

Permalink
Fix/ token issues, OK-24190, OK-24180 (#3752)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
qwang1113 authored Nov 2, 2023
1 parent edb9253 commit febad45
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/kit/src/hooks/useTokens.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function TxDetailAddTokenBox(props: Props) {
const [accountTokens, setAccountTokens] = useState<Token[]>([]);
const [isAddingTokens, setIsAddingTokens] = useState(false);

const { accountId, networkId } = useActiveWalletAccount();
const { accountId, networkId, network } = useActiveWalletAccount();

const handleAddToken = useCallback(async () => {
if (isAddingTokens) return;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit febad45

Please sign in to comment.