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.
220 lines
7.5 KiB
220 lines
7.5 KiB
3 years ago
|
import React, { useCallback, useContext, useEffect, useMemo } from 'react';
|
||
|
import { useDispatch, useSelector } from 'react-redux';
|
||
|
import { useHistory } from 'react-router-dom';
|
||
3 years ago
|
import ActionableMessage from '../../components/ui/actionable-message/actionable-message';
|
||
4 years ago
|
import Button from '../../components/ui/button';
|
||
|
import Identicon from '../../components/ui/identicon';
|
||
|
import TokenBalance from '../../components/ui/token-balance';
|
||
3 years ago
|
import { I18nContext } from '../../contexts/i18n';
|
||
3 years ago
|
import { MetaMetricsContext as NewMetaMetricsContext } from '../../contexts/metametrics.new';
|
||
|
import { getMostRecentOverviewPage } from '../../ducks/history/history';
|
||
|
import { getTokens } from '../../ducks/metamask/metamask';
|
||
3 years ago
|
import ZENDESK_URLS from '../../helpers/constants/zendesk-url';
|
||
3 years ago
|
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';
|
||
3 years ago
|
import { getSuggestedAssets } from '../../selectors';
|
||
|
import { rejectWatchAsset, acceptWatchAsset } from '../../store/actions';
|
||
6 years ago
|
|
||
3 years ago
|
function getTokenName(name, symbol) {
|
||
3 years ago
|
return name === undefined ? symbol : `${name} (${symbol})`;
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @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);
|
||
|
}
|
||
|
|
||
3 years ago
|
const ConfirmAddSuggestedToken = () => {
|
||
3 years ago
|
const t = useContext(I18nContext);
|
||
3 years ago
|
const dispatch = useDispatch();
|
||
|
const history = useHistory();
|
||
5 years ago
|
|
||
3 years ago
|
const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage);
|
||
|
const suggestedAssets = useSelector(getSuggestedAssets);
|
||
|
const tokens = useSelector(getTokens);
|
||
|
|
||
|
const trackEvent = useContext(NewMetaMetricsContext);
|
||
5 years ago
|
|
||
3 years ago
|
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"
|
||
|
/>
|
||
|
)
|
||
3 years ago
|
);
|
||
3 years ago
|
}, [suggestedAssets, tokens, t]);
|
||
3 years ago
|
|
||
3 years ago
|
const reusedTokenNameActionableMessage = useMemo(() => {
|
||
|
return (
|
||
|
hasDuplicateSymbolAndDiffAddress(suggestedAssets, tokens) && (
|
||
|
<ActionableMessage
|
||
|
message={t('reusedTokenNameWarning')}
|
||
|
type="warning"
|
||
|
withRightButton
|
||
|
useIcon
|
||
|
iconFillColor="#f8c000"
|
||
|
/>
|
||
|
)
|
||
|
);
|
||
|
}, [suggestedAssets, tokens, t]);
|
||
3 years ago
|
|
||
3 years ago
|
const handleAddTokensClick = useCallback(async () => {
|
||
|
await Promise.all(
|
||
|
suggestedAssets.map(async ({ asset, id }) => {
|
||
|
await dispatch(acceptWatchAsset(id));
|
||
|
|
||
|
trackEvent({
|
||
|
event: 'Token Added',
|
||
|
category: 'Wallet',
|
||
|
sensitiveProperties: {
|
||
|
token_symbol: asset.symbol,
|
||
|
token_contract_address: asset.address,
|
||
|
token_decimal_precision: asset.decimals,
|
||
|
unlisted: asset.unlisted,
|
||
|
source: 'dapp',
|
||
|
},
|
||
|
});
|
||
|
}),
|
||
|
);
|
||
|
|
||
|
history.push(mostRecentOverviewPage);
|
||
|
}, [dispatch, history, trackEvent, mostRecentOverviewPage, suggestedAssets]);
|
||
|
|
||
|
const goBackIfNoSuggestedAssetsOnFirstRender = () => {
|
||
3 years ago
|
if (!suggestedAssets.length) {
|
||
|
history.push(mostRecentOverviewPage);
|
||
|
}
|
||
3 years ago
|
};
|
||
|
|
||
|
useEffect(() => {
|
||
|
goBackIfNoSuggestedAssetsOnFirstRender();
|
||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
|
}, []);
|
||
3 years ago
|
|
||
|
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>
|
||
3 years ago
|
{knownTokenActionableMessage}
|
||
|
{reusedTokenNameActionableMessage}
|
||
3 years ago
|
</div>
|
||
|
<div className="page-container__content">
|
||
3 years ago
|
<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>
|
||
3 years ago
|
</div>
|
||
3 years ago
|
<div className="confirm-add-suggested-token__token-list">
|
||
3 years ago
|
{suggestedAssets.map(({ asset }) => {
|
||
|
return (
|
||
|
<div
|
||
3 years ago
|
className="confirm-add-suggested-token__token-list-item"
|
||
3 years ago
|
key={asset.address}
|
||
|
>
|
||
3 years ago
|
<div className="confirm-add-suggested-token__token confirm-add-suggested-token__data">
|
||
3 years ago
|
<Identicon
|
||
3 years ago
|
className="confirm-add-suggested-token__token-icon"
|
||
3 years ago
|
diameter={48}
|
||
|
address={asset.address}
|
||
|
image={asset.image}
|
||
|
/>
|
||
3 years ago
|
<div className="confirm-add-suggested-token__name">
|
||
3 years ago
|
{getTokenName(asset.name, asset.symbol)}
|
||
|
</div>
|
||
|
</div>
|
||
3 years ago
|
<div className="confirm-add-suggested-token__balance">
|
||
3 years ago
|
<TokenBalance token={asset} />
|
||
|
</div>
|
||
|
</div>
|
||
|
);
|
||
|
})}
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div className="page-container__footer">
|
||
|
<footer>
|
||
|
<Button
|
||
|
type="secondary"
|
||
|
large
|
||
|
className="page-container__footer-button"
|
||
|
onClick={async () => {
|
||
|
await Promise.all(
|
||
3 years ago
|
suggestedAssets.map(({ id }) => dispatch(rejectWatchAsset(id))),
|
||
3 years ago
|
);
|
||
|
history.push(mostRecentOverviewPage);
|
||
|
}}
|
||
|
>
|
||
|
{t('cancel')}
|
||
|
</Button>
|
||
|
<Button
|
||
|
type="primary"
|
||
|
large
|
||
|
className="page-container__footer-button"
|
||
|
disabled={suggestedAssets.length === 0}
|
||
3 years ago
|
onClick={handleAddTokensClick}
|
||
3 years ago
|
>
|
||
|
{t('addToken')}
|
||
|
</Button>
|
||
|
</footer>
|
||
|
</div>
|
||
|
</div>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
export default ConfirmAddSuggestedToken;
|