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 BigNumber from 'bignumber.js';
|
2022-08-10 03:26:25 +02:00
|
|
|
import { isEqual, uniqBy } from 'lodash';
|
2022-12-01 21:01:34 +01:00
|
|
|
import { formatIconUrlWithProxy } from '@metamask/assets-controllers';
|
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,
|
2022-08-17 18:38:06 +02:00
|
|
|
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';
|
2022-04-15 14:57:58 +02:00
|
|
|
import { TOKEN_BUCKET_PRIORITY } from '../../shared/constants/swaps';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { CHAIN_IDS, CURRENCY_SYMBOLS } from '../../shared/constants/network';
|
2021-02-04 19:15:23 +01:00
|
|
|
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,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2022-08-17 18:38:06 +02:00
|
|
|
tokenList,
|
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;
|
2022-07-28 15:56:15 +02:00
|
|
|
let contractExchangeRate;
|
|
|
|
if (isSwapsDefaultTokenSymbol(symbol, chainId)) {
|
|
|
|
contractExchangeRate = 1;
|
|
|
|
} else if (string && conversionRate > 0) {
|
2022-08-11 19:33:33 +02:00
|
|
|
// 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.
|
2022-07-28 15:56:15 +02:00
|
|
|
contractExchangeRate = contractExchangeRates[toChecksumHexAddress(address)];
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
const formattedFiat =
|
|
|
|
getTokenFiatAmount(
|
2022-07-28 15:56:15 +02:00
|
|
|
contractExchangeRate,
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
string,
|
|
|
|
symbol,
|
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
) || '';
|
2022-07-28 15:56:15 +02:00
|
|
|
const rawFiat = formattedFiat
|
|
|
|
? getTokenFiatAmount(
|
|
|
|
contractExchangeRate,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
string,
|
|
|
|
symbol,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
: '';
|
2022-04-13 14:18:35 +02:00
|
|
|
|
2022-09-12 13:11:29 +02:00
|
|
|
const chainIdForTokenIcons =
|
2022-09-14 16:55:31 +02:00
|
|
|
chainId === CHAIN_IDS.GOERLI ? CHAIN_IDS.MAINNET : chainId;
|
2022-09-12 13:11:29 +02:00
|
|
|
|
2022-08-22 21:16:57 +02:00
|
|
|
const tokenIconUrl =
|
2022-09-14 16:55:31 +02:00
|
|
|
(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) ||
|
2022-11-08 19:14:17 +01:00
|
|
|
(symbol === CURRENCY_SYMBOLS.AVALANCHE &&
|
|
|
|
chainId === CHAIN_IDS.AVALANCHE) ||
|
|
|
|
(symbol === CURRENCY_SYMBOLS.ETH && chainId === CHAIN_IDS.OPTIMISM) ||
|
|
|
|
(symbol === CURRENCY_SYMBOLS.ETH && chainId === CHAIN_IDS.ARBITRUM)
|
2022-08-22 21:16:57 +02:00
|
|
|
? iconUrl
|
2022-09-12 13:11:29 +02:00
|
|
|
: formatIconUrlWithProxy({
|
|
|
|
chainId: chainIdForTokenIcons,
|
|
|
|
tokenAddress: address || '',
|
|
|
|
});
|
2022-08-22 21:16:57 +02:00
|
|
|
const usedIconUrl = tokenIconUrl || token?.image;
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
return {
|
|
|
|
...token,
|
|
|
|
primaryLabel: symbol,
|
2022-08-17 18:38:06 +02:00
|
|
|
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,
|
2022-08-17 18:38:06 +02:00
|
|
|
name: name || tokenList[address?.toLowerCase()]?.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,
|
2022-04-15 14:57:58 +02:00
|
|
|
tokenBucketPriority = TOKEN_BUCKET_PRIORITY.OWNED,
|
2021-09-09 22:56:27 +02:00
|
|
|
}) {
|
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);
|
2022-08-17 18:38:06 +02:00
|
|
|
const tokenList = useSelector(getTokenList, isEqual);
|
2022-08-10 03:26:25 +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
|
|
|
|
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,
|
2022-08-17 18:38:06 +02:00
|
|
|
tokenList,
|
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,
|
2022-08-10 03:26:25 +02:00
|
|
|
...shuffledTokensList.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,
|
2022-08-17 18:38:06 +02:00
|
|
|
tokenList,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-04-15 14:57:58 +02:00
|
|
|
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()]) {
|
2021-11-30 19:56:28 +01:00
|
|
|
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);
|
2022-04-15 14:57:58 +02:00
|
|
|
if (tokenBucketPriority === TOKEN_BUCKET_PRIORITY.OWNED) {
|
|
|
|
return [
|
|
|
|
...tokensToSearchBuckets.owned,
|
|
|
|
...tokensToSearchBuckets.top,
|
|
|
|
...tokensToSearchBuckets.others,
|
|
|
|
];
|
|
|
|
}
|
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,
|
2022-07-28 15:56:15 +02:00
|
|
|
memoizedTopTokens,
|
2020-11-03 00:41:28 +01:00
|
|
|
tokenConversionRates,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
2021-10-25 17:03:21 +02:00
|
|
|
memoizedDefaultToken,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2022-08-17 18:38:06 +02:00
|
|
|
tokenList,
|
2022-04-15 14:57:58 +02:00
|
|
|
tokenBucketPriority,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|