1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/ui/hooks/useTokensToSearch.js

214 lines
6.9 KiB
JavaScript
Raw Normal View History

import { useMemo } from 'react';
import { shallowEqual, useSelector } from 'react-redux';
import BigNumber from 'bignumber.js';
import { isEqual, uniqBy } from 'lodash';
import { formatIconUrlWithProxy } from '@metamask/controllers';
import { getTokenFiatAmount } from '../helpers/utils/token-util';
2020-11-03 00:41:28 +01:00
import {
getTokenExchangeRates,
getCurrentCurrency,
getSwapsDefaultToken,
getCurrentChainId,
getTokenList,
} from '../selectors';
import { getConversionRate } from '../ducks/metamask/metamask';
import { getSwapsTokens } from '../ducks/swaps/swaps';
import { isSwapsDefaultTokenSymbol } from '../../shared/modules/swaps.utils';
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
import { TOKEN_BUCKET_PRIORITY } from '../../shared/constants/swaps';
import { CHAIN_IDS, CURRENCY_SYMBOLS } from '../../shared/constants/network';
import { useEqualityCheck } from './useEqualityCheck';
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
export function getRenderableTokenData(
token,
contractExchangeRates,
conversionRate,
currentCurrency,
chainId,
tokenList,
2020-11-03 00:41:28 +01:00
) {
const { symbol, name, address, iconUrl, string, balance, decimals } = token;
let contractExchangeRate;
if (isSwapsDefaultTokenSymbol(symbol, chainId)) {
contractExchangeRate = 1;
} else if (string && conversionRate > 0) {
// This condition improves performance significantly, because it only gets a contract exchange rate
// if a token amount is truthy and conversion rate is higher than 0.
contractExchangeRate = contractExchangeRates[toChecksumHexAddress(address)];
}
2020-11-03 00:41:28 +01:00
const formattedFiat =
getTokenFiatAmount(
contractExchangeRate,
2020-11-03 00:41:28 +01:00
conversionRate,
currentCurrency,
string,
symbol,
true,
) || '';
const rawFiat = formattedFiat
? getTokenFiatAmount(
contractExchangeRate,
conversionRate,
currentCurrency,
string,
symbol,
false,
)
: '';
2022-04-13 14:18:35 +02:00
const chainIdForTokenIcons =
chainId === CHAIN_IDS.GOERLI ? CHAIN_IDS.MAINNET : chainId;
const tokenIconUrl =
(symbol === CURRENCY_SYMBOLS.ETH && chainId === CHAIN_IDS.MAINNET) ||
(symbol === CURRENCY_SYMBOLS.ETH && chainId === CHAIN_IDS.GOERLI) ||
(symbol === CURRENCY_SYMBOLS.BNB && chainId === CHAIN_IDS.BSC) ||
(symbol === CURRENCY_SYMBOLS.MATIC && chainId === CHAIN_IDS.POLYGON) ||
(symbol === CURRENCY_SYMBOLS.AVALANCHE && chainId === CHAIN_IDS.AVALANCHE)
? iconUrl
: formatIconUrlWithProxy({
chainId: chainIdForTokenIcons,
tokenAddress: address || '',
});
const usedIconUrl = tokenIconUrl || token?.image;
2020-10-06 20:28:38 +02:00
return {
...token,
primaryLabel: symbol,
secondaryLabel: name || tokenList[address?.toLowerCase()]?.name,
2020-11-03 00:41:28 +01:00
rightPrimaryLabel:
string && `${new BigNumber(string).round(6).toString()} ${symbol}`,
2020-10-06 20:28:38 +02:00
rightSecondaryLabel: formattedFiat,
iconUrl: usedIconUrl,
identiconAddress: usedIconUrl ? null : address,
balance,
decimals,
name: name || tokenList[address?.toLowerCase()]?.name,
2020-10-06 20:28:38 +02:00
rawFiat,
};
2020-10-06 20:28:38 +02:00
}
export function useTokensToSearch({
usersTokens = [],
topTokens = {},
shuffledTokensList,
tokenBucketPriority = TOKEN_BUCKET_PRIORITY.OWNED,
}) {
const chainId = useSelector(getCurrentChainId);
const tokenConversionRates = useSelector(getTokenExchangeRates, isEqual);
const conversionRate = useSelector(getConversionRate);
const currentCurrency = useSelector(getCurrentCurrency);
const defaultSwapsToken = useSelector(getSwapsDefaultToken, shallowEqual);
const tokenList = useSelector(getTokenList, isEqual);
const memoizedTopTokens = useEqualityCheck(topTokens);
const memoizedUsersToken = useEqualityCheck(usersTokens);
2020-10-06 20:28:38 +02:00
const defaultToken = getRenderableTokenData(
defaultSwapsToken,
2020-10-06 20:28:38 +02:00
tokenConversionRates,
conversionRate,
currentCurrency,
chainId,
tokenList,
);
const memoizedDefaultToken = useEqualityCheck(defaultToken);
2020-10-06 20:28:38 +02:00
const swapsTokens = useSelector(getSwapsTokens, isEqual) || [];
const tokensToSearch = swapsTokens.length
? swapsTokens
: [
memoizedDefaultToken,
...shuffledTokensList.filter(
(token) => token.symbol !== memoizedDefaultToken.symbol,
),
];
const memoizedTokensToSearch = useEqualityCheck(tokensToSearch);
2020-10-06 20:28:38 +02:00
return useMemo(() => {
2020-11-03 00:41:28 +01:00
const usersTokensAddressMap = memoizedUsersToken.reduce(
(acc, token) => ({ ...acc, [token.address.toLowerCase()]: token }),
2020-11-03 00:41:28 +01:00
{},
);
2020-10-06 20:28:38 +02:00
const tokensToSearchBuckets = {
owned: [],
2020-10-06 20:28:38 +02:00
top: [],
others: [],
};
2020-10-06 20:28:38 +02:00
const memoizedSwapsAndUserTokensWithoutDuplicities = uniqBy(
[memoizedDefaultToken, ...memoizedTokensToSearch, ...memoizedUsersToken],
(token) => token.address.toLowerCase(),
);
memoizedSwapsAndUserTokensWithoutDuplicities.forEach((token) => {
2020-11-03 00:41:28 +01:00
const renderableDataToken = getRenderableTokenData(
{ ...usersTokensAddressMap[token.address.toLowerCase()], ...token },
2020-11-03 00:41:28 +01:00
tokenConversionRates,
conversionRate,
currentCurrency,
chainId,
tokenList,
);
if (tokenBucketPriority === TOKEN_BUCKET_PRIORITY.OWNED) {
if (
isSwapsDefaultTokenSymbol(renderableDataToken.symbol, chainId) ||
usersTokensAddressMap[token.address.toLowerCase()]
) {
tokensToSearchBuckets.owned.push(renderableDataToken);
} else if (memoizedTopTokens[token.address.toLowerCase()]) {
tokensToSearchBuckets.top[
memoizedTopTokens[token.address.toLowerCase()].index
] = renderableDataToken;
} else {
tokensToSearchBuckets.others.push(renderableDataToken);
}
} else if (memoizedTopTokens[token.address.toLowerCase()]) {
tokensToSearchBuckets.top[
memoizedTopTokens[token.address.toLowerCase()].index
] = renderableDataToken;
} else if (
isSwapsDefaultTokenSymbol(renderableDataToken.symbol, chainId) ||
usersTokensAddressMap[token.address.toLowerCase()]
2020-11-03 00:41:28 +01:00
) {
tokensToSearchBuckets.owned.push(renderableDataToken);
2020-10-06 20:28:38 +02:00
} else {
tokensToSearchBuckets.others.push(renderableDataToken);
2020-10-06 20:28:38 +02:00
}
});
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
tokensToSearchBuckets.owned = tokensToSearchBuckets.owned.sort(
({ rawFiat }, { rawFiat: secondRawFiat }) => {
return new BigNumber(rawFiat || 0).gt(secondRawFiat || 0) ? -1 : 1;
2020-11-03 00:41:28 +01:00
},
);
tokensToSearchBuckets.top = tokensToSearchBuckets.top.filter(Boolean);
if (tokenBucketPriority === TOKEN_BUCKET_PRIORITY.OWNED) {
return [
...tokensToSearchBuckets.owned,
...tokensToSearchBuckets.top,
...tokensToSearchBuckets.others,
];
}
2020-10-06 20:28:38 +02:00
return [
...tokensToSearchBuckets.top,
...tokensToSearchBuckets.owned,
2020-10-06 20:28:38 +02:00
...tokensToSearchBuckets.others,
];
2020-11-03 00:41:28 +01:00
}, [
memoizedTokensToSearch,
memoizedUsersToken,
memoizedTopTokens,
2020-11-03 00:41:28 +01:00
tokenConversionRates,
conversionRate,
currentCurrency,
memoizedDefaultToken,
chainId,
tokenList,
tokenBucketPriority,
]);
2020-10-06 20:28:38 +02:00
}