You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
4.8 KiB
166 lines
4.8 KiB
4 years ago
|
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 { getTokenFiatAmount } from '../helpers/utils/token-util';
|
||
4 years ago
|
import {
|
||
|
getTokenExchangeRates,
|
||
|
getConversionRate,
|
||
|
getCurrentCurrency,
|
||
4 years ago
|
getSwapsDefaultToken,
|
||
|
getCurrentChainId,
|
||
4 years ago
|
} from '../selectors';
|
||
|
import { getSwapsTokens } from '../ducks/swaps/swaps';
|
||
4 years ago
|
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
|
||
|
import { isSwapsDefaultTokenSymbol } from '../../shared/modules/swaps.utils';
|
||
4 years ago
|
import { useEqualityCheck } from './useEqualityCheck';
|
||
4 years ago
|
|
||
4 years ago
|
const tokenList = shuffle(
|
||
|
Object.entries(contractMap)
|
||
|
.map(([address, tokenData]) => ({
|
||
|
...tokenData,
|
||
|
address: address.toLowerCase(),
|
||
|
}))
|
||
|
.filter((tokenData) => Boolean(tokenData.erc20)),
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
4 years ago
|
export function getRenderableTokenData(
|
||
|
token,
|
||
|
contractExchangeRates,
|
||
|
conversionRate,
|
||
|
currentCurrency,
|
||
4 years ago
|
chainId,
|
||
4 years ago
|
) {
|
||
4 years ago
|
const { symbol, name, address, iconUrl, string, balance, decimals } = token;
|
||
4 years ago
|
|
||
4 years ago
|
const formattedFiat =
|
||
|
getTokenFiatAmount(
|
||
4 years ago
|
isSwapsDefaultTokenSymbol(symbol, chainId)
|
||
|
? 1
|
||
|
: contractExchangeRates[address],
|
||
4 years ago
|
conversionRate,
|
||
|
currentCurrency,
|
||
|
string,
|
||
|
symbol,
|
||
|
true,
|
||
4 years ago
|
) || '';
|
||
4 years ago
|
const rawFiat =
|
||
|
getTokenFiatAmount(
|
||
4 years ago
|
isSwapsDefaultTokenSymbol(symbol, chainId)
|
||
|
? 1
|
||
|
: contractExchangeRates[address],
|
||
4 years ago
|
conversionRate,
|
||
|
currentCurrency,
|
||
|
string,
|
||
|
symbol,
|
||
|
false,
|
||
4 years ago
|
) || '';
|
||
4 years ago
|
const usedIconUrl =
|
||
|
iconUrl ||
|
||
4 years ago
|
(contractMap[toChecksumHexAddress(address)] &&
|
||
|
`images/contract/${contractMap[toChecksumHexAddress(address)].logo}`);
|
||
4 years ago
|
return {
|
||
|
...token,
|
||
|
primaryLabel: symbol,
|
||
4 years ago
|
secondaryLabel: name || contractMap[toChecksumHexAddress(address)]?.name,
|
||
4 years ago
|
rightPrimaryLabel:
|
||
|
string && `${new BigNumber(string).round(6).toString()} ${symbol}`,
|
||
4 years ago
|
rightSecondaryLabel: formattedFiat,
|
||
|
iconUrl: usedIconUrl,
|
||
|
identiconAddress: usedIconUrl ? null : address,
|
||
|
balance,
|
||
|
decimals,
|
||
4 years ago
|
name: name || contractMap[toChecksumHexAddress(address)]?.name,
|
||
4 years ago
|
rawFiat,
|
||
4 years ago
|
};
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export function useTokensToSearch({ usersTokens = [], topTokens = {} }) {
|
||
|
const chainId = useSelector(getCurrentChainId);
|
||
4 years ago
|
const tokenConversionRates = useSelector(getTokenExchangeRates, isEqual);
|
||
|
const conversionRate = useSelector(getConversionRate);
|
||
|
const currentCurrency = useSelector(getCurrentCurrency);
|
||
4 years ago
|
const defaultSwapsToken = useSelector(getSwapsDefaultToken);
|
||
4 years ago
|
|
||
4 years ago
|
const memoizedTopTokens = useEqualityCheck(topTokens);
|
||
|
const memoizedUsersToken = useEqualityCheck(usersTokens);
|
||
4 years ago
|
|
||
4 years ago
|
const defaultToken = getRenderableTokenData(
|
||
|
defaultSwapsToken,
|
||
4 years ago
|
tokenConversionRates,
|
||
|
conversionRate,
|
||
|
currentCurrency,
|
||
4 years ago
|
chainId,
|
||
4 years ago
|
);
|
||
4 years ago
|
const memoizedDefaultToken = useEqualityCheck(defaultToken);
|
||
4 years ago
|
|
||
4 years ago
|
const swapsTokens = useSelector(getSwapsTokens) || [];
|
||
4 years ago
|
|
||
|
const tokensToSearch = swapsTokens.length
|
||
|
? swapsTokens
|
||
|
: [
|
||
|
memoizedDefaultToken,
|
||
|
...tokenList.filter(
|
||
|
(token) => token.symbol !== memoizedDefaultToken.symbol,
|
||
|
),
|
||
|
];
|
||
|
|
||
4 years ago
|
const memoizedTokensToSearch = useEqualityCheck(tokensToSearch);
|
||
4 years ago
|
return useMemo(() => {
|
||
4 years ago
|
const usersTokensAddressMap = memoizedUsersToken.reduce(
|
||
|
(acc, token) => ({ ...acc, [token.address]: token }),
|
||
|
{},
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
|
const tokensToSearchBuckets = {
|
||
4 years ago
|
owned: [],
|
||
4 years ago
|
top: [],
|
||
|
others: [],
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
|
memoizedTokensToSearch.forEach((token) => {
|
||
4 years ago
|
const renderableDataToken = getRenderableTokenData(
|
||
|
{ ...usersTokensAddressMap[token.address], ...token },
|
||
|
tokenConversionRates,
|
||
|
conversionRate,
|
||
|
currentCurrency,
|
||
4 years ago
|
chainId,
|
||
4 years ago
|
);
|
||
4 years ago
|
if (
|
||
4 years ago
|
isSwapsDefaultTokenSymbol(renderableDataToken.symbol, chainId) ||
|
||
|
(usersTokensAddressMap[token.address] &&
|
||
4 years ago
|
Number(renderableDataToken.balance ?? 0) !== 0)
|
||
|
) {
|
||
4 years ago
|
tokensToSearchBuckets.owned.push(renderableDataToken);
|
||
4 years ago
|
} else if (memoizedTopTokens[token.address]) {
|
||
4 years ago
|
tokensToSearchBuckets.top[
|
||
|
memoizedTopTokens[token.address].index
|
||
4 years ago
|
] = renderableDataToken;
|
||
4 years ago
|
} else {
|
||
4 years ago
|
tokensToSearchBuckets.others.push(renderableDataToken);
|
||
4 years ago
|
}
|
||
4 years ago
|
});
|
||
4 years ago
|
|
||
4 years ago
|
tokensToSearchBuckets.owned = tokensToSearchBuckets.owned.sort(
|
||
|
({ rawFiat }, { rawFiat: secondRawFiat }) => {
|
||
4 years ago
|
return new BigNumber(rawFiat || 0).gt(secondRawFiat || 0) ? -1 : 1;
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
|
tokensToSearchBuckets.top = tokensToSearchBuckets.top.filter(Boolean);
|
||
4 years ago
|
return [
|
||
|
...tokensToSearchBuckets.owned,
|
||
|
...tokensToSearchBuckets.top,
|
||
|
...tokensToSearchBuckets.others,
|
||
4 years ago
|
];
|
||
4 years ago
|
}, [
|
||
|
memoizedTokensToSearch,
|
||
|
memoizedUsersToken,
|
||
|
tokenConversionRates,
|
||
|
conversionRate,
|
||
|
currentCurrency,
|
||
|
memoizedTopTokens,
|
||
4 years ago
|
chainId,
|
||
4 years ago
|
]);
|
||
4 years ago
|
}
|