1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

mainnet tokens autopopulate add token form fields when on custom networks #12087 (#12800)

Co-authored-by: Alex Donesky <alex.donesky@consensys.net>
This commit is contained in:
dragana8 2022-01-05 19:19:28 +01:00 committed by GitHub
parent e694059b28
commit fc185e3139
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -1604,6 +1604,9 @@
"mainnet": { "mainnet": {
"message": "Ethereum Mainnet" "message": "Ethereum Mainnet"
}, },
"mainnetToken": {
"message": "This address matches a known mainnet token address. Recheck the contract address and network for the token you are trying to add."
},
"makeAnotherSwap": { "makeAnotherSwap": {
"message": "Create a new swap" "message": "Create a new swap"
}, },

View File

@ -1,6 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { getTokenTrackerLink } from '@metamask/etherscan-link'; import { getTokenTrackerLink } from '@metamask/etherscan-link';
import contractMap from '@metamask/contract-metadata';
import { import {
checkExistingAddresses, checkExistingAddresses,
getURLHostName, getURLHostName,
@ -64,6 +65,7 @@ class ImportToken extends Component {
forceEditSymbol: false, forceEditSymbol: false,
symbolAutoFilled: false, symbolAutoFilled: false,
decimalAutoFilled: false, decimalAutoFilled: false,
mainnetTokenWarning: null,
}; };
componentDidMount() { componentDidMount() {
@ -192,6 +194,7 @@ class ImportToken extends Component {
tokenSelectorError: null, tokenSelectorError: null,
symbolAutoFilled: false, symbolAutoFilled: false,
decimalAutoFilled: false, decimalAutoFilled: false,
mainnetTokenWarning: null,
}); });
const addressIsValid = isValidHexAddress(customAddress, { const addressIsValid = isValidHexAddress(customAddress, {
@ -199,6 +202,12 @@ class ImportToken extends Component {
}); });
const standardAddress = addHexPrefix(customAddress).toLowerCase(); const standardAddress = addHexPrefix(customAddress).toLowerCase();
const isMainnetToken = Object.keys(contractMap).some(
(key) => key.toLowerCase() === customAddress.toLowerCase(),
);
const isMainnetNetwork = this.props.chainId === '0x1';
switch (true) { switch (true) {
case !addressIsValid: case !addressIsValid:
this.setState({ this.setState({
@ -209,6 +218,16 @@ class ImportToken extends Component {
customDecimalsError: null, customDecimalsError: null,
}); });
break;
case isMainnetToken && !isMainnetNetwork:
this.setState({
mainnetTokenWarning: this.context.t('mainnetToken'),
customSymbol: '',
customDecimals: 0,
customSymbolError: null,
customDecimalsError: null,
});
break; break;
case Boolean(this.props.identities[standardAddress]): case Boolean(this.props.identities[standardAddress]):
this.setState({ this.setState({
@ -270,6 +289,7 @@ class ImportToken extends Component {
forceEditSymbol, forceEditSymbol,
symbolAutoFilled, symbolAutoFilled,
decimalAutoFilled, decimalAutoFilled,
mainnetTokenWarning,
} = this.state; } = this.state;
const { chainId, rpcPrefs } = this.props; const { chainId, rpcPrefs } = this.props;
@ -310,7 +330,7 @@ class ImportToken extends Component {
type="text" type="text"
value={customAddress} value={customAddress}
onChange={(e) => this.handleCustomAddressChange(e.target.value)} onChange={(e) => this.handleCustomAddressChange(e.target.value)}
error={customAddressError} error={customAddressError || mainnetTokenWarning}
fullWidth fullWidth
autoFocus autoFocus
margin="normal" margin="normal"