1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Update token detection logic to only control auto-detection (#14251)

This commit is contained in:
Niranjana Binoy 2022-04-06 13:59:16 -04:00 committed by GitHub
parent a7a1646002
commit 07231e42b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 56 deletions

View File

@ -151,14 +151,22 @@ export default class Identicon extends PureComponent {
} }
if (address) { if (address) {
if (process.env.TOKEN_DETECTION_V2) {
if (tokenList[address.toLowerCase()]?.iconUrl) {
return this.renderJazzicon();
}
} else {
/** TODO: Remove during TOKEN_DETECTION_V2 feature flag clean up */
// token from dynamic api list is fetched when useTokenDetection is true // token from dynamic api list is fetched when useTokenDetection is true
// And since the token.address from allTokens is checksumaddress // And since the token.address from allTokens is checksumaddress
// tokenAddress have to be changed to lowercase when we are using dynamic list // tokenAddress have to be changed to lowercase when we are using dynamic list
const tokenAddress = useTokenDetection ? address.toLowerCase() : address; const tokenAddress = useTokenDetection
? address.toLowerCase()
: address;
if (tokenAddress && tokenList[tokenAddress]?.iconUrl) { if (tokenAddress && tokenList[tokenAddress]?.iconUrl) {
return this.renderJazzicon(); return this.renderJazzicon();
} }
}
return ( return (
<div <div
className={classnames({ 'identicon__address-wrapper': addBorder })} className={classnames({ 'identicon__address-wrapper': addBorder })}

View File

@ -1,4 +1,5 @@
import React, { useCallback, useContext } from 'react'; import React, { useCallback, useContext } from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { I18nContext } from '../../../contexts/i18n'; import { I18nContext } from '../../../contexts/i18n';
import Tooltip from '../tooltip'; import Tooltip from '../tooltip';
@ -8,6 +9,7 @@ import Identicon from '../identicon/identicon.component';
import { shortenAddress } from '../../../helpers/utils/util'; import { shortenAddress } from '../../../helpers/utils/util';
import CopyIcon from '../icon/copy-icon.component'; import CopyIcon from '../icon/copy-icon.component';
import { useCopyToClipboard } from '../../../hooks/useCopyToClipboard'; import { useCopyToClipboard } from '../../../hooks/useCopyToClipboard';
import { getUseTokenDetection, getTokenList } from '../../../selectors';
const NicknamePopover = ({ const NicknamePopover = ({
address, address,
@ -23,6 +25,8 @@ const NicknamePopover = ({
}, [onAdd]); }, [onAdd]);
const [copied, handleCopy] = useCopyToClipboard(); const [copied, handleCopy] = useCopyToClipboard();
const useTokenDetection = useSelector(getUseTokenDetection);
const tokenList = useSelector(getTokenList);
return ( return (
<div className="nickname-popover"> <div className="nickname-popover">
@ -31,6 +35,8 @@ const NicknamePopover = ({
address={address} address={address}
diameter={36} diameter={36}
className="nickname-popover__identicon" className="nickname-popover__identicon"
useTokenDetection={useTokenDetection}
tokenList={tokenList}
/> />
<div className="nickname-popover__address"> <div className="nickname-popover__address">
{nickname || shortenAddress(address)} {nickname || shortenAddress(address)}

View File

@ -1,4 +1,5 @@
import React, { useCallback, useContext, useState } from 'react'; import React, { useCallback, useContext, useState } from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Popover from '../popover'; import Popover from '../popover';
@ -8,6 +9,7 @@ import TextField from '../text-field';
import { I18nContext } from '../../../contexts/i18n'; import { I18nContext } from '../../../contexts/i18n';
import Identicon from '../identicon/identicon.component'; import Identicon from '../identicon/identicon.component';
import { getUseTokenDetection, getTokenList } from '../../../selectors';
export default function UpdateNicknamePopover({ export default function UpdateNicknamePopover({
nickname, nickname,
@ -42,6 +44,9 @@ export default function UpdateNicknamePopover({
onClose(); onClose();
}; };
const useTokenDetection = useSelector(getUseTokenDetection);
const tokenList = useSelector(getTokenList);
return ( return (
<Popover <Popover
title={nickname ? t('editAddressNickname') : t('addANickname')} title={nickname ? t('editAddressNickname') : t('addANickname')}
@ -72,6 +77,8 @@ export default function UpdateNicknamePopover({
className="update-nickname__content__indenticon" className="update-nickname__content__indenticon"
address={address} address={address}
diameter={36} diameter={36}
useTokenDetection={useTokenDetection}
tokenList={tokenList}
/> />
<label className="update-nickname__content__label--capitalized"> <label className="update-nickname__content__label--capitalized">
{t('address')} {t('address')}

View File

@ -23,6 +23,12 @@ IconFactory.prototype.iconForAddress = function (
useTokenDetection, useTokenDetection,
tokenList, tokenList,
) { ) {
if (process.env.TOKEN_DETECTION_V2) {
if (iconExistsFor(address.toLowerCase(), tokenList)) {
return imageElFor(address.toLowerCase(), useTokenDetection, tokenList);
}
} else {
/** TODO: Remove during TOKEN_DETECTION_V2 feature flag clean up */
// When useTokenDetection flag is true the tokenList contains tokens with non-checksum address from the dynamic token service api, // When useTokenDetection flag is true the tokenList contains tokens with non-checksum address from the dynamic token service api,
// When useTokenDetection flag is false the tokenList contains tokens with checksum addresses from contract-metadata. // When useTokenDetection flag is false the tokenList contains tokens with checksum addresses from contract-metadata.
// So the flag indicates whether the address of tokens currently on the tokenList is checksum or not. // So the flag indicates whether the address of tokens currently on the tokenList is checksum or not.
@ -34,6 +40,7 @@ IconFactory.prototype.iconForAddress = function (
if (iconExistsFor(addr, tokenList)) { if (iconExistsFor(addr, tokenList)) {
return imageElFor(addr, useTokenDetection, tokenList); return imageElFor(addr, useTokenDetection, tokenList);
} }
}
return this.generateIdenticonSvg(address, diameter); return this.generateIdenticonSvg(address, diameter);
}; };
@ -76,7 +83,7 @@ function imageElFor(address, useTokenDetection, tokenList) {
// so that it can be accessed using the filename in iconUrl. // so that it can be accessed using the filename in iconUrl.
const path = useTokenDetection ? fileName : `images/contract/${fileName}`; const path = useTokenDetection ? fileName : `images/contract/${fileName}`;
const img = document.createElement('img'); const img = document.createElement('img');
img.src = path; img.src = process.env.TOKEN_DETECTION_V2 ? fileName : path;
img.style.width = '100%'; img.style.width = '100%';
return img; return img;
} }

View File

@ -28,6 +28,11 @@ const useAddressDetails = (toAddress) => {
if (identities[toAddress]?.name) { if (identities[toAddress]?.name) {
return { toName: identities[toAddress].name, isTrusted: true }; return { toName: identities[toAddress].name, isTrusted: true };
} }
if (process.env.TOKEN_DETECTION_V2) {
if (tokenList[toAddress]?.name) {
return { toName: tokenList[toAddress].name, isTrusted: true };
}
} else {
const casedTokenList = useTokenDetection const casedTokenList = useTokenDetection
? tokenList ? tokenList
: Object.keys(tokenList).reduce((acc, base) => { : Object.keys(tokenList).reduce((acc, base) => {
@ -39,6 +44,7 @@ const useAddressDetails = (toAddress) => {
if (casedTokenList[toAddress]?.name) { if (casedTokenList[toAddress]?.name) {
return { toName: casedTokenList[toAddress].name, isTrusted: true }; return { toName: casedTokenList[toAddress].name, isTrusted: true };
} }
}
return { return {
toName: shortenAddress(checksummedAddress), toName: shortenAddress(checksummedAddress),
isTrusted: false, isTrusted: false,

View File

@ -19,6 +19,7 @@ import { isSwapsDefaultTokenSymbol } from '../../shared/modules/swaps.utils';
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils'; import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
import { useEqualityCheck } from './useEqualityCheck'; import { useEqualityCheck } from './useEqualityCheck';
/** TODO: Remove during TOKEN_DETECTION_V2 feature flag clean up */
const shuffledContractMap = shuffle( const shuffledContractMap = shuffle(
Object.entries(contractMap) Object.entries(contractMap)
.map(([address, tokenData]) => ({ .map(([address, tokenData]) => ({
@ -38,10 +39,6 @@ export function getRenderableTokenData(
useTokenDetection, useTokenDetection,
) { ) {
const { symbol, name, address, iconUrl, string, balance, decimals } = token; const { symbol, name, address, iconUrl, string, balance, decimals } = token;
// token from dynamic api list is fetched when useTokenDetection is true
// 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;
const formattedFiat = const formattedFiat =
getTokenFiatAmount( getTokenFiatAmount(
isSwapsDefaultTokenSymbol(symbol, chainId) isSwapsDefaultTokenSymbol(symbol, chainId)
@ -64,11 +61,21 @@ export function getRenderableTokenData(
symbol, symbol,
false, false,
) || ''; ) || '';
const usedIconUrl = let tokenAddress;
iconUrl || let tokenIconUrl;
(tokenList[tokenAddress] && if (process.env.TOKEN_DETECTION_V2) {
`images/contract/${tokenList[tokenAddress].iconUrl}`) || tokenAddress = address?.toLowerCase();
token?.image; tokenIconUrl = tokenList[tokenAddress]?.iconUrl;
} else {
// token from dynamic api list is fetched when useTokenDetection is true
// And since the token.address from allTokens is checksumaddress
// token Address have to be changed to lowercase when we are using dynamic list
tokenAddress = useTokenDetection ? address?.toLowerCase() : address;
tokenIconUrl = useTokenDetection
? tokenList[tokenAddress]?.iconUrl
: `images/contract/${tokenList[tokenAddress].iconUrl}`;
}
const usedIconUrl = iconUrl || tokenIconUrl || token?.image;
return { return {
...token, ...token,
primaryLabel: symbol, primaryLabel: symbol,
@ -97,10 +104,13 @@ export function useTokensToSearch({
const defaultSwapsToken = useSelector(getSwapsDefaultToken, shallowEqual); const defaultSwapsToken = useSelector(getSwapsDefaultToken, shallowEqual);
const tokenList = useSelector(getTokenList, isEqual); const tokenList = useSelector(getTokenList, isEqual);
const useTokenDetection = useSelector(getUseTokenDetection); const useTokenDetection = useSelector(getUseTokenDetection);
let shuffledTokenList = shuffledTokensList;
if (!process.env.TOKEN_DETECTION_V2) {
// token from dynamic api list is fetched when useTokenDetection is true // token from dynamic api list is fetched when useTokenDetection is true
const shuffledTokenList = useTokenDetection shuffledTokenList = useTokenDetection
? shuffledTokensList ? shuffledTokensList
: shuffledContractMap; : shuffledContractMap;
}
const memoizedTopTokens = useEqualityCheck(topTokens); const memoizedTopTokens = useEqualityCheck(topTokens);
const memoizedUsersToken = useEqualityCheck(usersTokens); const memoizedUsersToken = useEqualityCheck(usersTokens);

View File

@ -116,7 +116,9 @@ const mapStateToProps = (state, ownProps) => {
const tokenList = getTokenList(state); const tokenList = getTokenList(state);
const useTokenDetection = getUseTokenDetection(state); const useTokenDetection = getUseTokenDetection(state);
const casedTokenList = useTokenDetection let casedTokenList = tokenList;
if (!process.env.TOKEN_DETECTION_V2) {
casedTokenList = useTokenDetection
? tokenList ? tokenList
: Object.keys(tokenList).reduce((acc, base) => { : Object.keys(tokenList).reduce((acc, base) => {
return { return {
@ -124,6 +126,7 @@ const mapStateToProps = (state, ownProps) => {
[base.toLowerCase()]: tokenList[base], [base.toLowerCase()]: tokenList[base],
}; };
}, {}); }, {});
}
const toName = const toName =
identities[toAddress]?.name || identities[toAddress]?.name ||
casedTokenList[toAddress]?.name || casedTokenList[toAddress]?.name ||

View File

@ -38,10 +38,14 @@ export default class TokenList extends Component {
.fill(undefined) .fill(undefined)
.map((_, i) => { .map((_, i) => {
const { iconUrl, symbol, name, address } = results[i] || {}; const { iconUrl, symbol, name, address } = results[i] || {};
let iconPath = iconUrl;
if (!process.env.TOKEN_DETECTION_V2) {
/** TODO: Remove during TOKEN_DETECTION_V2 feature flag clean up */
// token from dynamic api list is fetched when useTokenDetection is true // token from dynamic api list is fetched when useTokenDetection is true
const iconPath = useTokenDetection iconPath = useTokenDetection
? iconUrl ? iconUrl
: `images/contract/${iconUrl}`; : `images/contract/${iconUrl}`;
}
const tokenAlreadyAdded = checkExistingAddresses(address, tokens); const tokenAlreadyAdded = checkExistingAddresses(address, tokens);
const onClick = () => const onClick = () =>
!tokenAlreadyAdded && onToggleToken(results[i]); !tokenAlreadyAdded && onToggleToken(results[i]);

View File

@ -40,9 +40,10 @@ export default function TokenDetailsPage() {
); );
const aggregators = tokenMetadata?.aggregators?.join(', '); const aggregators = tokenMetadata?.aggregators?.join(', ');
const fileName = tokenMetadata?.iconUrl; const fileName = tokenMetadata?.iconUrl;
const imagePath = useTokenDetection let imagePath = fileName;
? fileName if (!process.env.TOKEN_DETECTION_V2) {
: `images/contract/${fileName}`; imagePath = useTokenDetection ? fileName : `images/contract/${fileName}`;
}
const token = tokens.find(({ address }) => const token = tokens.find(({ address }) =>
isEqualCaseInsensitive(address, tokenAddress), isEqualCaseInsensitive(address, tokenAddress),