2022-04-21 20:29:39 +02:00
|
|
|
import React, { useContext, useState } from 'react';
|
2021-11-15 18:28:35 +01:00
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-01-19 19:42:41 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2022-11-24 20:59:07 +01:00
|
|
|
import { isValidHexAddress } from '@metamask/controller-utils';
|
2021-11-15 18:28:35 +01:00
|
|
|
import { useI18nContext } from '../../hooks/useI18nContext';
|
|
|
|
import { DEFAULT_ROUTE } from '../../helpers/constants/routes';
|
2022-02-26 02:17:42 +01:00
|
|
|
import {
|
|
|
|
DISPLAY,
|
|
|
|
FONT_WEIGHT,
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2022-02-26 02:17:42 +01:00
|
|
|
} from '../../helpers/constants/design-system';
|
|
|
|
|
2021-11-15 18:28:35 +01:00
|
|
|
import Box from '../../components/ui/box';
|
2022-02-26 02:17:42 +01:00
|
|
|
import Typography from '../../components/ui/typography';
|
|
|
|
import ActionableMessage from '../../components/ui/actionable-message';
|
2021-11-15 18:28:35 +01:00
|
|
|
import PageContainer from '../../components/ui/page-container';
|
2021-11-26 21:03:35 +01:00
|
|
|
import {
|
2022-11-15 19:49:42 +01:00
|
|
|
addNftVerifyOwnership,
|
2022-04-21 20:29:39 +02:00
|
|
|
getTokenStandardAndDetails,
|
2022-07-18 16:43:30 +02:00
|
|
|
ignoreTokens,
|
2023-02-16 20:23:29 +01:00
|
|
|
setNewNftAddedMessage,
|
|
|
|
updateNftDropDownState,
|
2021-11-26 21:03:35 +01:00
|
|
|
} from '../../store/actions';
|
2022-01-19 19:42:41 +01:00
|
|
|
import FormField from '../../components/ui/form-field';
|
2023-01-19 18:31:13 +01:00
|
|
|
import {
|
|
|
|
getCurrentChainId,
|
|
|
|
getIsMainnet,
|
|
|
|
getSelectedAddress,
|
|
|
|
getUseNftDetection,
|
|
|
|
} from '../../selectors';
|
2023-02-16 20:23:29 +01:00
|
|
|
import { getNftsDropdownState } from '../../ducks/metamask/metamask';
|
|
|
|
import NftsDetectionNotice from '../../components/app/nfts-detection-notice';
|
2022-04-21 20:29:39 +02:00
|
|
|
import { MetaMetricsContext } from '../../contexts/metametrics';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { AssetType } from '../../../shared/constants/transaction';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventName,
|
|
|
|
MetaMetricsTokenEventSource,
|
|
|
|
} from '../../../shared/constants/metametrics';
|
2023-02-27 17:42:02 +01:00
|
|
|
import {
|
2023-04-24 16:19:19 +02:00
|
|
|
ButtonIcon,
|
|
|
|
IconName,
|
|
|
|
ButtonIconSize,
|
|
|
|
} from '../../components/component-library';
|
2021-11-15 18:28:35 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
export default function AddNft() {
|
2021-11-15 18:28:35 +01:00
|
|
|
const t = useI18nContext();
|
|
|
|
const history = useHistory();
|
2021-11-26 21:03:35 +01:00
|
|
|
const dispatch = useDispatch();
|
2022-11-15 19:49:42 +01:00
|
|
|
const useNftDetection = useSelector(getUseNftDetection);
|
2022-01-19 19:42:41 +01:00
|
|
|
const isMainnet = useSelector(getIsMainnet);
|
2023-02-16 20:23:29 +01:00
|
|
|
const nftsDropdownState = useSelector(getNftsDropdownState);
|
2023-01-19 18:31:13 +01:00
|
|
|
const selectedAddress = useSelector(getSelectedAddress);
|
|
|
|
const chainId = useSelector(getCurrentChainId);
|
2022-01-19 15:38:33 +01:00
|
|
|
const addressEnteredOnImportTokensPage =
|
|
|
|
history?.location?.state?.addressEnteredOnImportTokensPage;
|
2023-02-16 20:23:29 +01:00
|
|
|
const contractAddressToConvertFromTokenToNft =
|
2022-01-19 19:42:41 +01:00
|
|
|
history?.location?.state?.tokenAddress;
|
2022-01-19 15:38:33 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
const [nftAddress, setNftAddress] = useState(
|
2022-01-19 19:42:41 +01:00
|
|
|
addressEnteredOnImportTokensPage ??
|
2023-02-16 20:23:29 +01:00
|
|
|
contractAddressToConvertFromTokenToNft ??
|
2022-01-19 19:42:41 +01:00
|
|
|
'',
|
2022-01-19 15:38:33 +01:00
|
|
|
);
|
2021-11-15 18:28:35 +01:00
|
|
|
const [tokenId, setTokenId] = useState('');
|
2021-12-14 00:41:10 +01:00
|
|
|
const [disabled, setDisabled] = useState(true);
|
2023-02-16 20:23:29 +01:00
|
|
|
const [nftAddFailed, setNftAddFailed] = useState(false);
|
2022-04-21 20:29:39 +02:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2021-11-15 18:28:35 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
const handleAddNft = async () => {
|
2021-11-26 21:03:35 +01:00
|
|
|
try {
|
2023-02-16 20:23:29 +01:00
|
|
|
await dispatch(addNftVerifyOwnership(nftAddress, tokenId));
|
|
|
|
const newNftDropdownState = {
|
|
|
|
...nftsDropdownState,
|
2023-01-19 18:31:13 +01:00
|
|
|
[selectedAddress]: {
|
2023-02-16 20:23:29 +01:00
|
|
|
...nftsDropdownState?.[selectedAddress],
|
2023-01-19 18:31:13 +01:00
|
|
|
[chainId]: {
|
2023-02-16 20:23:29 +01:00
|
|
|
...nftsDropdownState?.[selectedAddress]?.[chainId],
|
|
|
|
[nftAddress]: true,
|
2023-01-19 18:31:13 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
dispatch(updateNftDropDownState(newNftDropdownState));
|
2021-11-26 21:03:35 +01:00
|
|
|
} catch (error) {
|
|
|
|
const { message } = error;
|
2023-02-16 20:23:29 +01:00
|
|
|
dispatch(setNewNftAddedMessage(message));
|
|
|
|
setNftAddFailed(true);
|
2021-11-26 21:03:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-02-16 20:23:29 +01:00
|
|
|
if (contractAddressToConvertFromTokenToNft) {
|
2022-01-19 19:42:41 +01:00
|
|
|
await dispatch(
|
2022-07-18 16:43:30 +02:00
|
|
|
ignoreTokens({
|
2023-02-16 20:23:29 +01:00
|
|
|
tokensToIgnore: contractAddressToConvertFromTokenToNft,
|
2022-07-18 16:43:30 +02:00
|
|
|
dontShowLoadingIndicator: true,
|
|
|
|
}),
|
2022-01-19 19:42:41 +01:00
|
|
|
);
|
|
|
|
}
|
2023-02-16 20:23:29 +01:00
|
|
|
dispatch(setNewNftAddedMessage('success'));
|
2022-04-21 20:29:39 +02:00
|
|
|
|
|
|
|
const tokenDetails = await getTokenStandardAndDetails(
|
2023-02-16 20:23:29 +01:00
|
|
|
nftAddress,
|
2022-04-21 20:29:39 +02:00
|
|
|
null,
|
|
|
|
tokenId.toString(),
|
|
|
|
);
|
|
|
|
|
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.TokenAdded,
|
2022-04-21 20:29:39 +02:00
|
|
|
category: 'Wallet',
|
|
|
|
sensitiveProperties: {
|
2023-02-16 20:23:29 +01:00
|
|
|
token_contract_address: nftAddress,
|
2022-04-21 20:29:39 +02:00
|
|
|
token_symbol: tokenDetails?.symbol,
|
|
|
|
tokenId: tokenId.toString(),
|
2023-01-18 15:47:29 +01:00
|
|
|
asset_type: AssetType.NFT,
|
2022-04-21 20:29:39 +02:00
|
|
|
token_standard: tokenDetails?.standard,
|
2023-04-27 16:28:08 +02:00
|
|
|
source_connection_method: MetaMetricsTokenEventSource.Custom,
|
2022-04-21 20:29:39 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-26 21:03:35 +01:00
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
};
|
|
|
|
|
2021-12-14 00:41:10 +01:00
|
|
|
const validateAndSetAddress = (val) => {
|
2022-11-24 20:59:07 +01:00
|
|
|
setDisabled(!isValidHexAddress(val) || !tokenId);
|
2023-02-16 20:23:29 +01:00
|
|
|
setNftAddress(val);
|
2021-12-14 00:41:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const validateAndSetTokenId = (val) => {
|
2023-02-16 20:23:29 +01:00
|
|
|
setDisabled(!isValidHexAddress(nftAddress) || !val || isNaN(Number(val)));
|
2021-12-14 00:41:10 +01:00
|
|
|
setTokenId(val);
|
|
|
|
};
|
|
|
|
|
2021-11-15 18:28:35 +01:00
|
|
|
return (
|
|
|
|
<PageContainer
|
2022-01-19 15:38:33 +01:00
|
|
|
title={t('importNFT')}
|
2021-11-15 18:28:35 +01:00
|
|
|
onSubmit={() => {
|
2023-02-16 20:23:29 +01:00
|
|
|
handleAddNft();
|
2021-11-15 18:28:35 +01:00
|
|
|
}}
|
|
|
|
submitText={t('add')}
|
|
|
|
onCancel={() => {
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
}}
|
|
|
|
onClose={() => {
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
}}
|
2021-12-14 00:41:10 +01:00
|
|
|
disabled={disabled}
|
2021-11-15 18:28:35 +01:00
|
|
|
contentComponent={
|
2022-03-31 16:02:47 +02:00
|
|
|
<Box>
|
2023-02-16 20:23:29 +01:00
|
|
|
{isMainnet && !useNftDetection ? <NftsDetectionNotice /> : null}
|
|
|
|
{nftAddFailed && (
|
2023-01-17 21:26:16 +01:00
|
|
|
<Box marginLeft={4} marginRight={4}>
|
|
|
|
<ActionableMessage
|
|
|
|
type="danger"
|
|
|
|
useIcon
|
|
|
|
iconFillColor="var(--color-error-default)"
|
|
|
|
message={
|
|
|
|
<Box display={DISPLAY.INLINE_FLEX}>
|
|
|
|
<Typography
|
2023-02-02 21:15:26 +01:00
|
|
|
variant={TypographyVariant.H7}
|
2023-01-17 21:26:16 +01:00
|
|
|
fontWeight={FONT_WEIGHT.NORMAL}
|
2023-02-27 17:42:02 +01:00
|
|
|
marginTop={0}
|
2023-01-17 21:26:16 +01:00
|
|
|
>
|
2023-02-10 16:36:58 +01:00
|
|
|
{t('nftAddFailedMessage')}
|
2023-01-17 21:26:16 +01:00
|
|
|
</Typography>
|
2023-02-27 17:42:02 +01:00
|
|
|
<ButtonIcon
|
|
|
|
className="add-nft__close"
|
2023-04-24 16:19:19 +02:00
|
|
|
iconName={IconName.Close}
|
|
|
|
size={ButtonIconSize.Sm}
|
2023-02-27 17:42:02 +01:00
|
|
|
ariaLabel={t('close')}
|
|
|
|
data-testid="add-nft-error-close"
|
2023-02-16 20:23:29 +01:00
|
|
|
onClick={() => setNftAddFailed(false)}
|
2023-01-17 21:26:16 +01:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Box>
|
2022-02-26 02:17:42 +01:00
|
|
|
)}
|
2022-03-31 16:02:47 +02:00
|
|
|
<Box margin={4}>
|
2022-01-19 19:42:41 +01:00
|
|
|
<FormField
|
2022-06-28 02:31:25 +02:00
|
|
|
dataTestId="address"
|
2022-01-19 19:42:41 +01:00
|
|
|
titleText={t('address')}
|
2021-11-15 18:28:35 +01:00
|
|
|
placeholder="0x..."
|
2023-02-16 20:23:29 +01:00
|
|
|
value={nftAddress}
|
2022-02-26 02:17:42 +01:00
|
|
|
onChange={(val) => {
|
|
|
|
validateAndSetAddress(val);
|
2023-02-16 20:23:29 +01:00
|
|
|
setNftAddFailed(false);
|
2022-02-26 02:17:42 +01:00
|
|
|
}}
|
2022-01-19 19:42:41 +01:00
|
|
|
tooltipText={t('importNFTAddressToolTip')}
|
2021-11-15 18:28:35 +01:00
|
|
|
autoFocus
|
|
|
|
/>
|
2022-01-19 19:42:41 +01:00
|
|
|
<FormField
|
2022-06-28 02:31:25 +02:00
|
|
|
dataTestId="token-id"
|
2022-01-19 19:42:41 +01:00
|
|
|
titleText={t('tokenId')}
|
2021-11-15 18:28:35 +01:00
|
|
|
placeholder={t('nftTokenIdPlaceholder')}
|
|
|
|
value={tokenId}
|
2022-01-19 19:42:41 +01:00
|
|
|
onChange={(val) => {
|
|
|
|
validateAndSetTokenId(val);
|
2023-02-16 20:23:29 +01:00
|
|
|
setNftAddFailed(false);
|
2022-01-19 19:42:41 +01:00
|
|
|
}}
|
|
|
|
tooltipText={t('importNFTTokenIdToolTip')}
|
2021-11-15 18:28:35 +01:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|