2021-02-04 19:15:23 +01:00
|
|
|
import { useMemo } from 'react';
|
2021-12-01 17:25:09 +01:00
|
|
|
import { shallowEqual, useSelector } from 'react-redux';
|
2021-02-04 19:15:23 +01:00
|
|
|
import contractMap from '@metamask/contract-metadata';
|
|
|
|
import BigNumber from 'bignumber.js';
|
2021-06-03 18:08:37 +02:00
|
|
|
import { isEqual, shuffle, uniqBy } from 'lodash';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { getTokenFiatAmount } from '../helpers/utils/token-util';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getTokenExchangeRates,
|
|
|
|
getCurrentCurrency,
|
2021-03-18 11:20:06 +01:00
|
|
|
getSwapsDefaultToken,
|
|
|
|
getCurrentChainId,
|
2021-09-09 22:56:27 +02:00
|
|
|
getUseTokenDetection,
|
|
|
|
getTokenList,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../selectors';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getConversionRate } from '../ducks/metamask/metamask';
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { getSwapsTokens } from '../ducks/swaps/swaps';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { isSwapsDefaultTokenSymbol } from '../../shared/modules/swaps.utils';
|
2021-10-25 17:03:21 +02:00
|
|
|
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useEqualityCheck } from './useEqualityCheck';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
const shuffledContractMap = shuffle(
|
2020-11-03 00:41:28 +01:00
|
|
|
Object.entries(contractMap)
|
|
|
|
.map(([address, tokenData]) => ({
|
|
|
|
...tokenData,
|
|
|
|
address: address.toLowerCase(),
|
|
|
|
}))
|
|
|
|
.filter((tokenData) => Boolean(tokenData.erc20)),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getRenderableTokenData(
|
|
|
|
token,
|
|
|
|
contractExchangeRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList,
|
|
|
|
useTokenDetection,
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { symbol, name, address, iconUrl, string, balance, decimals } = token;
|
2021-09-09 22:56:27 +02:00
|
|
|
// token from dynamic api list is fetched when useTokenDetection is true
|
2021-09-11 05:55:23 +02:00
|
|
|
// And since the token.address from allTokens is checksumaddress
|
|
|
|
// token Address have to be changed to lowercase when we are using dynamic list
|
|
|
|
const tokenAddress = useTokenDetection ? address?.toLowerCase() : address;
|
2020-11-03 00:41:28 +01:00
|
|
|
const formattedFiat =
|
|
|
|
getTokenFiatAmount(
|
2021-03-18 11:20:06 +01:00
|
|
|
isSwapsDefaultTokenSymbol(symbol, chainId)
|
|
|
|
? 1
|
2021-10-25 17:03:21 +02:00
|
|
|
: contractExchangeRates[toChecksumHexAddress(address)],
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
string,
|
|
|
|
symbol,
|
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
) || '';
|
2020-11-03 00:41:28 +01:00
|
|
|
const rawFiat =
|
|
|
|
getTokenFiatAmount(
|
2021-03-18 11:20:06 +01:00
|
|
|
isSwapsDefaultTokenSymbol(symbol, chainId)
|
|
|
|
? 1
|
2021-10-25 17:03:21 +02:00
|
|
|
: contractExchangeRates[toChecksumHexAddress(address)],
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
string,
|
|
|
|
symbol,
|
|
|
|
false,
|
2021-02-04 19:15:23 +01:00
|
|
|
) || '';
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedIconUrl =
|
|
|
|
iconUrl ||
|
2021-09-09 22:56:27 +02:00
|
|
|
(tokenList[tokenAddress] &&
|
2021-09-22 17:31:36 +02:00
|
|
|
`images/contract/${tokenList[tokenAddress].iconUrl}`) ||
|
|
|
|
token?.image;
|
2020-10-06 20:28:38 +02:00
|
|
|
return {
|
|
|
|
...token,
|
|
|
|
primaryLabel: symbol,
|
2021-09-09 22:56:27 +02:00
|
|
|
secondaryLabel: name || tokenList[tokenAddress]?.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,
|
2021-09-09 22:56:27 +02:00
|
|
|
name: name || tokenList[tokenAddress]?.name,
|
2020-10-06 20:28:38 +02:00
|
|
|
rawFiat,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
export function useTokensToSearch({
|
|
|
|
usersTokens = [],
|
|
|
|
topTokens = {},
|
|
|
|
shuffledTokensList,
|
|
|
|
}) {
|
2021-03-18 11:20:06 +01:00
|
|
|
const chainId = useSelector(getCurrentChainId);
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokenConversionRates = useSelector(getTokenExchangeRates, isEqual);
|
|
|
|
const conversionRate = useSelector(getConversionRate);
|
|
|
|
const currentCurrency = useSelector(getCurrentCurrency);
|
2021-12-01 17:25:09 +01:00
|
|
|
const defaultSwapsToken = useSelector(getSwapsDefaultToken, shallowEqual);
|
|
|
|
const tokenList = useSelector(getTokenList, isEqual);
|
2021-09-09 22:56:27 +02:00
|
|
|
const useTokenDetection = useSelector(getUseTokenDetection);
|
|
|
|
// token from dynamic api list is fetched when useTokenDetection is true
|
|
|
|
const shuffledTokenList = useTokenDetection
|
|
|
|
? shuffledTokensList
|
|
|
|
: shuffledContractMap;
|
2021-02-04 19:15:23 +01:00
|
|
|
const memoizedTopTokens = useEqualityCheck(topTokens);
|
|
|
|
const memoizedUsersToken = useEqualityCheck(usersTokens);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-03-18 11:20:06 +01:00
|
|
|
const defaultToken = getRenderableTokenData(
|
|
|
|
defaultSwapsToken,
|
2020-10-06 20:28:38 +02:00
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList,
|
|
|
|
useTokenDetection,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-03-18 11:20:06 +01:00
|
|
|
const memoizedDefaultToken = useEqualityCheck(defaultToken);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-12-01 17:25:09 +01:00
|
|
|
const swapsTokens = useSelector(getSwapsTokens, isEqual) || [];
|
2021-03-15 17:51:43 +01:00
|
|
|
|
|
|
|
const tokensToSearch = swapsTokens.length
|
|
|
|
? swapsTokens
|
|
|
|
: [
|
2021-03-18 11:20:06 +01:00
|
|
|
memoizedDefaultToken,
|
2021-09-09 22:56:27 +02:00
|
|
|
...shuffledTokenList.filter(
|
2021-03-18 11:20:06 +01:00
|
|
|
(token) => token.symbol !== memoizedDefaultToken.symbol,
|
2021-03-15 17:51:43 +01:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
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(
|
2021-09-22 17:31:36 +02:00
|
|
|
(acc, token) => ({ ...acc, [token.address.toLowerCase()]: token }),
|
2020-11-03 00:41:28 +01:00
|
|
|
{},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const tokensToSearchBuckets = {
|
2021-03-15 17:51:43 +01:00
|
|
|
owned: [],
|
2020-10-06 20:28:38 +02:00
|
|
|
top: [],
|
|
|
|
others: [],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-06-03 18:08:37 +02:00
|
|
|
const memoizedSwapsAndUserTokensWithoutDuplicities = uniqBy(
|
2021-10-25 17:03:21 +02:00
|
|
|
[memoizedDefaultToken, ...memoizedTokensToSearch, ...memoizedUsersToken],
|
2021-09-22 17:31:36 +02:00
|
|
|
(token) => token.address.toLowerCase(),
|
2021-06-03 18:08:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
memoizedSwapsAndUserTokensWithoutDuplicities.forEach((token) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const renderableDataToken = getRenderableTokenData(
|
2021-09-22 17:31:36 +02:00
|
|
|
{ ...usersTokensAddressMap[token.address.toLowerCase()], ...token },
|
2020-11-03 00:41:28 +01:00
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList,
|
|
|
|
useTokenDetection,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-11-30 19:56:28 +01:00
|
|
|
if (memoizedTopTokens[token.address.toLowerCase()]) {
|
|
|
|
tokensToSearchBuckets.top[
|
|
|
|
memoizedTopTokens[token.address.toLowerCase()].index
|
|
|
|
] = renderableDataToken;
|
|
|
|
} else if (
|
2021-03-18 11:20:06 +01:00
|
|
|
isSwapsDefaultTokenSymbol(renderableDataToken.symbol, chainId) ||
|
2021-09-22 17:31:36 +02:00
|
|
|
usersTokensAddressMap[token.address.toLowerCase()]
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearchBuckets.owned.push(renderableDataToken);
|
2020-10-06 20:28:38 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearchBuckets.others.push(renderableDataToken);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01: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 }) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return new BigNumber(rawFiat || 0).gt(secondRawFiat || 0) ? -1 : 1;
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
tokensToSearchBuckets.top = tokensToSearchBuckets.top.filter(Boolean);
|
2020-10-06 20:28:38 +02:00
|
|
|
return [
|
|
|
|
...tokensToSearchBuckets.top,
|
2022-01-17 22:55:23 +01:00
|
|
|
...tokensToSearchBuckets.owned,
|
2020-10-06 20:28:38 +02:00
|
|
|
...tokensToSearchBuckets.others,
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
memoizedTokensToSearch,
|
|
|
|
memoizedUsersToken,
|
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
memoizedTopTokens,
|
2021-10-25 17:03:21 +02:00
|
|
|
memoizedDefaultToken,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList,
|
|
|
|
useTokenDetection,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|