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';
|
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
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 { getApprovalRequestsByType } from '../../selectors';
|
|
|
|
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';
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Array} suggestedAssets - an array of assets suggested to add to the user's wallet
|
|
|
|
* via the RPC method `wallet_watchAsset`
|
|
|
|
* @param {Array} tokens - the list of tokens currently tracked in state
|
|
|
|
* @returns {boolean} Returns true when the list of suggestedAssets contains an entry with
|
|
|
|
* an address that matches an existing token.
|
|
|
|
*/
|
|
|
|
function hasDuplicateAddress(suggestedAssets, tokens) {
|
|
|
|
const duplicate = suggestedAssets.find(({ asset }) => {
|
|
|
|
const dupe = tokens.find(({ address }) => {
|
|
|
|
return isEqualCaseInsensitive(address, asset.address);
|
|
|
|
});
|
|
|
|
return Boolean(dupe);
|
|
|
|
});
|
|
|
|
return Boolean(duplicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Array} suggestedAssets - a list of assets suggested to add to the user's wallet
|
|
|
|
* via RPC method `wallet_watchAsset`
|
|
|
|
* @param {Array} tokens - the list of tokens currently tracked in state
|
|
|
|
* @returns {boolean} Returns true when the list of suggestedAssets contains an entry with both
|
|
|
|
* 1. a symbol that matches an existing token
|
|
|
|
* 2. an address that does not match an existing token
|
|
|
|
*/
|
|
|
|
function hasDuplicateSymbolAndDiffAddress(suggestedAssets, tokens) {
|
|
|
|
const duplicate = suggestedAssets.find(({ asset }) => {
|
|
|
|
const dupe = tokens.find((token) => {
|
|
|
|
return (
|
|
|
|
isEqualCaseInsensitive(token.symbol, asset.symbol) &&
|
|
|
|
!isEqualCaseInsensitive(token.address, asset.address)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
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-05 22:13:22 +02:00
|
|
|
const suggestedAssets = useSelector((metamaskState) =>
|
|
|
|
getApprovalRequestsByType(metamaskState, ApprovalType.WatchAsset).map(
|
|
|
|
({ requestData }) => requestData,
|
|
|
|
),
|
|
|
|
);
|
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 (
|
|
|
|
hasDuplicateAddress(suggestedAssets, tokens) && (
|
|
|
|
<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
|
|
|
);
|
2022-02-01 16:30:15 +01:00
|
|
|
}, [suggestedAssets, tokens, t]);
|
2022-01-25 21:16:56 +01:00
|
|
|
|
2022-02-01 16:30:15 +01:00
|
|
|
const reusedTokenNameActionableMessage = useMemo(() => {
|
|
|
|
return (
|
|
|
|
hasDuplicateSymbolAndDiffAddress(suggestedAssets, tokens) && (
|
|
|
|
<ActionableMessage
|
|
|
|
message={t('reusedTokenNameWarning')}
|
|
|
|
type="warning"
|
|
|
|
withRightButton
|
|
|
|
useIcon
|
|
|
|
iconFillColor="#f8c000"
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, [suggestedAssets, 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(
|
|
|
|
suggestedAssets.map(async ({ 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);
|
|
|
|
}, [dispatch, history, trackEvent, mostRecentOverviewPage, suggestedAssets]);
|
|
|
|
|
2023-06-05 22:13:22 +02:00
|
|
|
const handleCancelClick = useCallback(async () => {
|
|
|
|
await Promise.all(
|
|
|
|
suggestedAssets.map(({ id }) =>
|
|
|
|
dispatch(
|
|
|
|
rejectPendingApproval(
|
|
|
|
id,
|
|
|
|
serializeError(ethErrors.provider.userRejectedRequest()),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
history.push(mostRecentOverviewPage);
|
|
|
|
}, [dispatch, history, mostRecentOverviewPage, suggestedAssets]);
|
|
|
|
|
2022-03-11 03:40:06 +01:00
|
|
|
const goBackIfNoSuggestedAssetsOnFirstRender = () => {
|
2022-01-25 21:16:56 +01:00
|
|
|
if (!suggestedAssets.length) {
|
|
|
|
history.push(mostRecentOverviewPage);
|
|
|
|
}
|
2022-03-11 03:40:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
goBackIfNoSuggestedAssetsOnFirstRender();
|
|
|
|
// 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">
|
2022-01-25 21:16:56 +01:00
|
|
|
{suggestedAssets.map(({ asset }) => {
|
|
|
|
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-05 22:13:22 +02:00
|
|
|
onCancel={handleCancelClick}
|
2023-01-23 16:22:12 +01:00
|
|
|
onSubmit={handleAddTokensClick}
|
|
|
|
disabled={suggestedAssets.length === 0}
|
|
|
|
/>
|
2022-01-25 21:16:56 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConfirmAddSuggestedToken;
|