mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Prevent adding already added tokens (#2362)
This commit is contained in:
parent
3fd9c8b57f
commit
a59972dcab
@ -22,14 +22,17 @@ const ethUtil = require('ethereumjs-util')
|
|||||||
const abi = require('human-standard-token-abi')
|
const abi = require('human-standard-token-abi')
|
||||||
const Eth = require('ethjs-query')
|
const Eth = require('ethjs-query')
|
||||||
const EthContract = require('ethjs-contract')
|
const EthContract = require('ethjs-contract')
|
||||||
|
const R = require('ramda')
|
||||||
|
|
||||||
const emptyAddr = '0x0000000000000000000000000000000000000000'
|
const emptyAddr = '0x0000000000000000000000000000000000000000'
|
||||||
|
|
||||||
module.exports = connect(mapStateToProps, mapDispatchToProps)(AddTokenScreen)
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(AddTokenScreen)
|
||||||
|
|
||||||
function mapStateToProps (state) {
|
function mapStateToProps (state) {
|
||||||
|
const { identities, tokens } = state.metamask
|
||||||
return {
|
return {
|
||||||
identities: state.metamask.identities,
|
identities,
|
||||||
|
tokens,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,6 +104,15 @@ AddTokenScreen.prototype.tokenAddressDidChange = function (e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AddTokenScreen.prototype.checkExistingAddresses = function (address) {
|
||||||
|
const tokensList = this.props.tokens
|
||||||
|
const matchesAddress = existingToken => {
|
||||||
|
return existingToken.address.toLowerCase() === address.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
return R.any(matchesAddress)(tokensList)
|
||||||
|
}
|
||||||
|
|
||||||
AddTokenScreen.prototype.validate = function () {
|
AddTokenScreen.prototype.validate = function () {
|
||||||
const errors = {}
|
const errors = {}
|
||||||
const identitiesList = Object.keys(this.props.identities)
|
const identitiesList = Object.keys(this.props.identities)
|
||||||
@ -128,6 +140,11 @@ AddTokenScreen.prototype.validate = function () {
|
|||||||
if (ownAddress) {
|
if (ownAddress) {
|
||||||
errors.customAddress = 'Personal address detected. Input the token contract address.'
|
errors.customAddress = 'Personal address detected. Input the token contract address.'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tokenAlreadyAdded = this.checkExistingAddresses(customAddress)
|
||||||
|
if (tokenAlreadyAdded) {
|
||||||
|
errors.customAddress = 'Token has already been added.'
|
||||||
|
}
|
||||||
} else if (
|
} else if (
|
||||||
Object.entries(selectedTokens)
|
Object.entries(selectedTokens)
|
||||||
.reduce((isEmpty, [ symbol, isSelected ]) => (
|
.reduce((isEmpty, [ symbol, isSelected ]) => (
|
||||||
@ -217,12 +234,14 @@ AddTokenScreen.prototype.renderTokenList = function () {
|
|||||||
return Array(6).fill(undefined)
|
return Array(6).fill(undefined)
|
||||||
.map((_, i) => {
|
.map((_, i) => {
|
||||||
const { logo, symbol, name, address } = results[i] || {}
|
const { logo, symbol, name, address } = results[i] || {}
|
||||||
|
const tokenAlreadyAdded = this.checkExistingAddresses(address)
|
||||||
return Boolean(logo || symbol || name) && (
|
return Boolean(logo || symbol || name) && (
|
||||||
h('div.add-token__token-wrapper', {
|
h('div.add-token__token-wrapper', {
|
||||||
className: classnames('add-token__token-wrapper', {
|
className: classnames({
|
||||||
'add-token__token-wrapper--selected': selectedTokens[address],
|
'add-token__token-wrapper--selected': selectedTokens[address],
|
||||||
|
'add-token__token-wrapper--disabled': tokenAlreadyAdded,
|
||||||
}),
|
}),
|
||||||
onClick: () => this.toggleToken(address, results[i]),
|
onClick: () => !tokenAlreadyAdded && this.toggleToken(address, results[i]),
|
||||||
}, [
|
}, [
|
||||||
h('div.add-token__token-icon', {
|
h('div.add-token__token-icon', {
|
||||||
style: {
|
style: {
|
||||||
@ -233,6 +252,9 @@ AddTokenScreen.prototype.renderTokenList = function () {
|
|||||||
h('div.add-token__token-symbol', symbol),
|
h('div.add-token__token-symbol', symbol),
|
||||||
h('div.add-token__token-name', name),
|
h('div.add-token__token-name', name),
|
||||||
]),
|
]),
|
||||||
|
tokenAlreadyAdded && (
|
||||||
|
h('div.add-token__token-message', 'Already added')
|
||||||
|
),
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
flex-flow: column nowrap;
|
flex-flow: column nowrap;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
position: relative;
|
position: relative;
|
||||||
top: -36px;
|
|
||||||
z-index: 12;
|
z-index: 12;
|
||||||
font-family: 'DIN Next Light';
|
font-family: 'DIN Next Light';
|
||||||
|
|
||||||
@ -189,6 +188,7 @@
|
|||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
border: 2px solid transparent;
|
border: 2px solid transparent;
|
||||||
|
position: relative;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
border: 2px solid rgba($malibu-blue, .5);
|
border: 2px solid rgba($malibu-blue, .5);
|
||||||
@ -197,6 +197,11 @@
|
|||||||
&--selected {
|
&--selected {
|
||||||
border: 2px solid $malibu-blue !important;
|
border: 2px solid $malibu-blue !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&--disabled {
|
||||||
|
opacity: .4;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
&__token-name {
|
&__token-name {
|
||||||
@ -223,6 +228,14 @@
|
|||||||
flex: 0 0 auto;
|
flex: 0 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&__token-message {
|
||||||
|
position: absolute;
|
||||||
|
color: $caribbean-green;
|
||||||
|
font-size: 11px;
|
||||||
|
bottom: 0;
|
||||||
|
left: 85px;
|
||||||
|
}
|
||||||
|
|
||||||
&__confirmation-token-list {
|
&__confirmation-token-list {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-flow: column nowrap;
|
flex-flow: column nowrap;
|
||||||
|
Loading…
Reference in New Issue
Block a user