2021-02-04 19:15:23 +01:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import contractMap from '@metamask/contract-metadata';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { isEqual, shuffle } from 'lodash';
|
|
|
|
import { checksumAddress } from '../helpers/utils/util';
|
|
|
|
import { getTokenFiatAmount } from '../helpers/utils/token-util';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getTokenExchangeRates,
|
|
|
|
getConversionRate,
|
|
|
|
getCurrentCurrency,
|
2021-03-04 18:16:01 +01:00
|
|
|
getSwapsEthToken,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../selectors';
|
|
|
|
import { getSwapsTokens } from '../ducks/swaps/swaps';
|
|
|
|
import { useEqualityCheck } from './useEqualityCheck';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenList = shuffle(
|
|
|
|
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-02-04 19:15:23 +01:00
|
|
|
const { symbol, name, address, iconUrl, string, balance, decimals } = token;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const formattedFiat =
|
|
|
|
getTokenFiatAmount(
|
|
|
|
symbol === 'ETH' ? 1 : contractExchangeRates[address],
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
string,
|
|
|
|
symbol,
|
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
) || '';
|
2020-11-03 00:41:28 +01:00
|
|
|
const rawFiat =
|
|
|
|
getTokenFiatAmount(
|
|
|
|
symbol === 'ETH' ? 1 : contractExchangeRates[address],
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
string,
|
|
|
|
symbol,
|
|
|
|
false,
|
2021-02-04 19:15:23 +01:00
|
|
|
) || '';
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedIconUrl =
|
|
|
|
iconUrl ||
|
|
|
|
(contractMap[checksumAddress(address)] &&
|
2021-02-04 19:15:23 +01:00
|
|
|
`images/contract/${contractMap[checksumAddress(address)].logo}`);
|
2020-10-06 20:28:38 +02:00
|
|
|
return {
|
|
|
|
...token,
|
|
|
|
primaryLabel: symbol,
|
|
|
|
secondaryLabel: name || contractMap[checksumAddress(address)]?.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 || contractMap[checksumAddress(address)]?.name,
|
|
|
|
rawFiat,
|
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 useTokensToSearch({
|
2020-10-09 21:17:56 +02:00
|
|
|
providedTokens,
|
|
|
|
usersTokens = [],
|
|
|
|
topTokens = {},
|
|
|
|
onlyEth,
|
|
|
|
singleToken,
|
|
|
|
}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokenConversionRates = useSelector(getTokenExchangeRates, isEqual);
|
|
|
|
const conversionRate = useSelector(getConversionRate);
|
|
|
|
const currentCurrency = useSelector(getCurrentCurrency);
|
2021-03-04 18:16:01 +01:00
|
|
|
const swapsEthToken = useSelector(getSwapsEthToken);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const memoizedTopTokens = useEqualityCheck(topTokens);
|
|
|
|
const memoizedUsersToken = useEqualityCheck(usersTokens);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-10-12 18:51:01 +02:00
|
|
|
const ethToken = getRenderableTokenData(
|
2020-10-09 21:17:56 +02:00
|
|
|
swapsEthToken,
|
2020-10-06 20:28:38 +02:00
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const memoizedEthToken = useEqualityCheck(ethToken);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const swapsTokens = useSelector(getSwapsTokens) || [];
|
|
|
|
let tokensToSearch;
|
2020-10-06 20:28:38 +02:00
|
|
|
if (onlyEth) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearch = [memoizedEthToken];
|
2020-10-06 20:28:38 +02:00
|
|
|
} else if (singleToken) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearch = providedTokens;
|
2020-10-06 20:28:38 +02:00
|
|
|
} else if (providedTokens) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearch = [memoizedEthToken, ...providedTokens];
|
2020-10-06 20:28:38 +02:00
|
|
|
} else if (swapsTokens.length) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearch = [memoizedEthToken, ...swapsTokens];
|
2020-10-06 20:28:38 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearch = [memoizedEthToken, ...tokenList];
|
2020-10-06 20:28:38 +02: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(
|
|
|
|
(acc, token) => ({ ...acc, [token.address]: token }),
|
|
|
|
{},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const tokensToSearchBuckets = {
|
2020-10-12 18:51:01 +02:00
|
|
|
owned: singleToken ? [] : [memoizedEthToken],
|
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
|
|
|
|
|
|
|
memoizedTokensToSearch.forEach((token) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const renderableDataToken = getRenderableTokenData(
|
|
|
|
{ ...usersTokensAddressMap[token.address], ...token },
|
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
usersTokensAddressMap[token.address] &&
|
|
|
|
(renderableDataToken.symbol === 'ETH' ||
|
|
|
|
Number(renderableDataToken.balance ?? 0) !== 0)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToSearchBuckets.owned.push(renderableDataToken);
|
2020-10-06 20:28:38 +02:00
|
|
|
} else if (memoizedTopTokens[token.address]) {
|
2020-11-03 00:41:28 +01:00
|
|
|
tokensToSearchBuckets.top[
|
|
|
|
memoizedTopTokens[token.address].index
|
2021-02-04 19:15:23 +01:00
|
|
|
] = 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.owned,
|
|
|
|
...tokensToSearchBuckets.top,
|
|
|
|
...tokensToSearchBuckets.others,
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
memoizedTokensToSearch,
|
|
|
|
memoizedUsersToken,
|
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
memoizedTopTokens,
|
|
|
|
memoizedEthToken,
|
|
|
|
singleToken,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|