2022-03-11 03:40:06 +01:00
|
|
|
import React, { useCallback, useContext, useEffect, useMemo } from 'react';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2023-06-05 22:13:22 +02:00
|
|
|
import { ethErrors, serializeError } from 'eth-rpc-errors';
|
2022-02-01 16:30:15 +01:00
|
|
|
import ActionableMessage from '../../components/ui/actionable-message/actionable-message';
|
2021-02-04 19:15:23 +01:00
|
|
|
import Button from '../../components/ui/button';
|
|
|
|
import Identicon from '../../components/ui/identicon';
|
|
|
|
import TokenBalance from '../../components/ui/token-balance';
|
2023-01-23 16:22:12 +01:00
|
|
|
import { PageContainerFooter } from '../../components/ui/page-container';
|
2022-01-25 21:16:56 +01:00
|
|
|
import { I18nContext } from '../../contexts/i18n';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../contexts/metametrics';
|
2022-03-11 03:40:06 +01:00
|
|
|
import { getMostRecentOverviewPage } from '../../ducks/history/history';
|
|
|
|
import { getTokens } from '../../ducks/metamask/metamask';
|
2022-02-01 16:30:15 +01:00
|
|
|
import ZENDESK_URLS from '../../helpers/constants/zendesk-url';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';
|
2023-06-05 22:13:22 +02:00
|
|
|
import {
|
|
|
|
resolvePendingApproval,
|
|
|
|
rejectPendingApproval,
|
|
|
|
} from '../../store/actions';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
MetaMetricsTokenEventSource,
|
|
|
|
} from '../../../shared/constants/metametrics';
|
2022-07-19 18:07:13 +02:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
AssetType,
|
|
|
|
TokenStandard,
|
2022-07-19 18:07:13 +02:00
|
|
|
} from '../../../shared/constants/transaction';
|
2023-06-15 22:18:12 +02:00
|
|
|
import { getSuggestedTokens } from '../../selectors';
|
2018-08-07 02:03:49 +02:00
|
|
|
|
2022-01-25 21:16:56 +01:00
|
|
|
function getTokenName(name, symbol) {
|
2022-03-11 03:40:06 +01:00
|
|
|
return name === undefined ? symbol : `${name} (${symbol})`;
|
2022-01-25 21:16:56 +01:00
|
|
|
}
|
2022-02-01 16:30:15 +01:00
|
|
|
|
|
|
|
/**
|
2023-06-15 22:18:12 +02:00
|
|
|
* @param {Array} suggestedTokens - an array of assets suggested to add to the user's wallet
|
2022-02-01 16:30:15 +01:00
|
|
|
* via the RPC method `wallet_watchAsset`
|
|
|
|
* @param {Array} tokens - the list of tokens currently tracked in state
|
2023-06-15 22:18:12 +02:00
|
|
|
* @returns {boolean} Returns true when the list of suggestedTokens contains an entry with
|
2022-02-01 16:30:15 +01:00
|
|
|
* an address that matches an existing token.
|
|
|
|
*/
|
2023-06-15 22:18:12 +02:00
|
|
|
function hasDuplicateAddress(suggestedTokens, tokens) {
|
|
|
|
const duplicate = suggestedTokens.find(({ requestData: { asset } }) => {
|
2022-02-01 16:30:15 +01:00
|
|
|
const dupe = tokens.find(({ address }) => {
|
2023-06-15 22:18:12 +02:00
|
|
|
return isEqualCaseInsensitive(address, asset?.address);
|
2022-02-01 16:30:15 +01:00
|
|
|
});
|
|
|
|
return Boolean(dupe);
|
|
|
|
});
|
|
|
|
return Boolean(duplicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-15 22:18:12 +02:00
|
|
|
* @param {Array} suggestedTokens - a list of assets suggested to add to the user's wallet
|
2022-02-01 16:30:15 +01:00
|
|
|
* via RPC method `wallet_watchAsset`
|
|
|
|
* @param {Array} tokens - the list of tokens currently tracked in state
|
2023-06-15 22:18:12 +02:00
|
|
|
* @returns {boolean} Returns true when the list of suggestedTokens contains an entry with both
|
2022-02-01 16:30:15 +01:00
|
|
|
* 1. a symbol that matches an existing token
|
|
|
|
* 2. an address that does not match an existing token
|
|
|
|
*/
|
2023-06-15 22:18:12 +02:00
|
|
|
function hasDuplicateSymbolAndDiffAddress(suggestedTokens, tokens) {
|
|
|
|
const duplicate = suggestedTokens.find(({ requestData: { asset } }) => {
|
2022-02-01 16:30:15 +01:00
|
|
|
const dupe = tokens.find((token) => {
|
|
|
|
return (
|
2023-06-15 22:18:12 +02:00
|
|
|
isEqualCaseInsensitive(token.symbol, asset?.symbol) &&
|
|
|
|
!isEqualCaseInsensitive(token.address, asset?.address)
|
2022-02-01 16:30:15 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
return Boolean(dupe);
|
|
|
|
});
|
|
|
|
return Boolean(duplicate);
|
|
|
|
}
|
|
|
|
|
2022-03-11 03:40:06 +01:00
|
|
|
const ConfirmAddSuggestedToken = () => {
|
2022-01-25 21:16:56 +01:00
|
|
|
const t = useContext(I18nContext);
|
2022-03-11 03:40:06 +01:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const history = useHistory();
|
2020-02-14 21:32:56 +01:00
|
|
|
|
2022-03-11 03:40:06 +01:00
|
|
|
const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage);
|
2023-06-15 22:18:12 +02:00
|
|
|
const suggestedTokens = useSelector(getSuggestedTokens);
|
2022-03-11 03:40:06 +01:00
|
|
|
const tokens = useSelector(getTokens);
|
2022-04-01 21:11:12 +02:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2020-02-14 21:32:56 +01:00
|
|
|
|
2022-02-01 16:30:15 +01:00
|
|
|
const knownTokenActionableMessage = useMemo(() => {
|
|
|
|
return (
|
2023-06-15 22:18:12 +02:00
|
|
|
hasDuplicateAddress(suggestedTokens, tokens) && (
|
2022-02-01 16:30:15 +01:00
|
|
|
<ActionableMessage
|
|
|
|
message={t('knownTokenWarning', [
|
|
|
|
<Button
|
|
|
|
type="link"
|
|
|
|
key="confirm-add-suggested-token-duplicate-warning"
|
|
|
|
className="confirm-add-suggested-token__link"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
target="_blank"
|
|
|
|
href={ZENDESK_URLS.TOKEN_SAFETY_PRACTICES}
|
|
|
|
>
|
|
|
|
{t('learnScamRisk')}
|
|
|
|
</Button>,
|
|
|
|
])}
|
|
|
|
type="warning"
|
|
|
|
withRightButton
|
|
|
|
useIcon
|
|
|
|
iconFillColor="#f8c000"
|
|
|
|
/>
|
|
|
|
)
|
2022-01-25 21:16:56 +01:00
|
|
|
);
|
2023-06-15 22:18:12 +02:00
|
|
|
}, [suggestedTokens, tokens, t]);
|
2022-01-25 21:16:56 +01:00
|
|
|
|
2022-02-01 16:30:15 +01:00
|
|
|
const reusedTokenNameActionableMessage = useMemo(() => {
|
|
|
|
return (
|
2023-06-15 22:18:12 +02:00
|
|
|
hasDuplicateSymbolAndDiffAddress(suggestedTokens, tokens) && (
|
2022-02-01 16:30:15 +01:00
|
|
|
<ActionableMessage
|
|
|
|
message={t('reusedTokenNameWarning')}
|
|
|
|
type="warning"
|
|
|
|
withRightButton
|
|
|
|
useIcon
|
|
|
|
iconFillColor="#f8c000"
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
);
|
2023-06-15 22:18:12 +02:00
|
|
|
}, [suggestedTokens, tokens, t]);
|
2022-01-25 21:16:56 +01:00
|
|
|
|
2022-03-11 03:40:06 +01:00
|
|
|
const handleAddTokensClick = useCallback(async () => {
|
|
|
|
await Promise.all(
|
2023-06-15 22:18:12 +02:00
|
|
|
suggestedTokens.map(async ({ requestData: { asset }, id }) => {
|
2023-06-05 22:13:22 +02:00
|
|
|
await dispatch(resolvePendingApproval(id, null));
|
2022-03-11 03:40:06 +01:00
|
|
|
|
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.TokenAdded,
|
|
|
|
category: MetaMetricsEventCategory.Wallet,
|
2022-03-11 03:40:06 +01:00
|
|
|
sensitiveProperties: {
|
|
|
|
token_symbol: asset.symbol,
|
|
|
|
token_contract_address: asset.address,
|
|
|
|
token_decimal_precision: asset.decimals,
|
|
|
|
unlisted: asset.unlisted,
|
2023-04-27 16:28:08 +02:00
|
|
|
source_connection_method: MetaMetricsTokenEventSource.Dapp,
|
2023-01-18 15:47:29 +01:00
|
|
|
token_standard: TokenStandard.ERC20,
|
|
|
|
asset_type: AssetType.token,
|
2022-03-11 03:40:06 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
history.push(mostRecentOverviewPage);
|
2023-06-15 22:18:12 +02:00
|
|
|
}, [dispatch, history, trackEvent, mostRecentOverviewPage, suggestedTokens]);
|
2022-03-11 03:40:06 +01:00
|
|
|
|
2023-06-15 22:18:12 +02:00
|
|
|
const handleCancelTokenClick = useCallback(async () => {
|
2023-06-05 22:13:22 +02:00
|
|
|
await Promise.all(
|
2023-06-15 22:18:12 +02:00
|
|
|
suggestedTokens.map(({ id }) =>
|
2023-06-05 22:13:22 +02:00
|
|
|
dispatch(
|
|
|
|
rejectPendingApproval(
|
|
|
|
id,
|
|
|
|
serializeError(ethErrors.provider.userRejectedRequest()),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
history.push(mostRecentOverviewPage);
|
2023-06-15 22:18:12 +02:00
|
|
|
}, [dispatch, history, mostRecentOverviewPage, suggestedTokens]);
|
2023-06-05 22:13:22 +02:00
|
|
|
|
2023-06-15 22:18:12 +02:00
|
|
|
const goBackIfNoSuggestedTokensOnFirstRender = () => {
|
|
|
|
if (!suggestedTokens.length) {
|
2022-01-25 21:16:56 +01:00
|
|
|
history.push(mostRecentOverviewPage);
|
|
|
|
}
|
2022-03-11 03:40:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-15 22:18:12 +02:00
|
|
|
goBackIfNoSuggestedTokensOnFirstRender();
|
2022-03-11 03:40:06 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2022-01-25 21:16:56 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="page-container">
|
|
|
|
<div className="page-container__header">
|
|
|
|
<div className="page-container__title">{t('addSuggestedTokens')}</div>
|
|
|
|
<div className="page-container__subtitle">
|
|
|
|
{t('likeToImportTokens')}
|
|
|
|
</div>
|
2022-02-01 16:30:15 +01:00
|
|
|
{knownTokenActionableMessage}
|
|
|
|
{reusedTokenNameActionableMessage}
|
2022-01-25 21:16:56 +01:00
|
|
|
</div>
|
|
|
|
<div className="page-container__content">
|
2022-03-11 03:40:06 +01:00
|
|
|
<div className="confirm-add-suggested-token">
|
|
|
|
<div className="confirm-add-suggested-token__header">
|
|
|
|
<div className="confirm-add-suggested-token__token">
|
|
|
|
{t('token')}
|
|
|
|
</div>
|
|
|
|
<div className="confirm-add-suggested-token__balance">
|
|
|
|
{t('balance')}
|
|
|
|
</div>
|
2022-01-25 21:16:56 +01:00
|
|
|
</div>
|
2022-03-11 03:40:06 +01:00
|
|
|
<div className="confirm-add-suggested-token__token-list">
|
2023-06-15 22:18:12 +02:00
|
|
|
{suggestedTokens.map(({ requestData: { asset } }) => {
|
2022-01-25 21:16:56 +01:00
|
|
|
return (
|
|
|
|
<div
|
2022-03-11 03:40:06 +01:00
|
|
|
className="confirm-add-suggested-token__token-list-item"
|
2022-01-25 21:16:56 +01:00
|
|
|
key={asset.address}
|
|
|
|
>
|
2022-03-11 03:40:06 +01:00
|
|
|
<div className="confirm-add-suggested-token__token confirm-add-suggested-token__data">
|
2022-01-25 21:16:56 +01:00
|
|
|
<Identicon
|
2022-03-11 03:40:06 +01:00
|
|
|
className="confirm-add-suggested-token__token-icon"
|
2022-01-25 21:16:56 +01:00
|
|
|
diameter={48}
|
|
|
|
address={asset.address}
|
|
|
|
image={asset.image}
|
|
|
|
/>
|
2022-03-11 03:40:06 +01:00
|
|
|
<div className="confirm-add-suggested-token__name">
|
2022-01-25 21:16:56 +01:00
|
|
|
{getTokenName(asset.name, asset.symbol)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-03-11 03:40:06 +01:00
|
|
|
<div className="confirm-add-suggested-token__balance">
|
2022-01-25 21:16:56 +01:00
|
|
|
<TokenBalance token={asset} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-23 16:22:12 +01:00
|
|
|
<PageContainerFooter
|
|
|
|
cancelText={t('cancel')}
|
|
|
|
submitText={t('addToken')}
|
2023-06-15 22:18:12 +02:00
|
|
|
onCancel={handleCancelTokenClick}
|
2023-01-23 16:22:12 +01:00
|
|
|
onSubmit={handleAddTokensClick}
|
2023-06-15 22:18:12 +02:00
|
|
|
disabled={suggestedTokens.length === 0}
|
2023-01-23 16:22:12 +01:00
|
|
|
/>
|
2022-01-25 21:16:56 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConfirmAddSuggestedToken;
|