2018-08-07 02:03:49 +02:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2019-03-22 00:03:30 +01:00
|
|
|
import Button from '../../components/ui/button'
|
|
|
|
import Identicon from '../../components/ui/identicon'
|
|
|
|
import TokenBalance from '../../components/ui/token-balance'
|
2020-07-03 18:16:31 +02:00
|
|
|
import { getEnvironmentType } from '../../../../app/scripts/lib/util'
|
2021-01-20 17:13:14 +01:00
|
|
|
import { ENVIRONMENT_TYPE_NOTIFICATION } from '../../../../shared/constants/app'
|
2018-08-07 02:03:49 +02:00
|
|
|
|
|
|
|
export default class ConfirmAddSuggestedToken extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2020-11-17 18:39:21 +01:00
|
|
|
trackEvent: PropTypes.func,
|
2018-08-07 02:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
history: PropTypes.object,
|
2018-08-15 01:08:12 +02:00
|
|
|
addToken: PropTypes.func,
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: PropTypes.string.isRequired,
|
2018-08-07 02:03:49 +02:00
|
|
|
pendingTokens: PropTypes.object,
|
|
|
|
removeSuggestedTokens: PropTypes.func,
|
2020-02-14 21:32:56 +01:00
|
|
|
tokens: PropTypes.array,
|
2018-08-07 02:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2020-07-03 18:16:31 +02:00
|
|
|
this._checkPendingTokens()
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidUpdate() {
|
2020-07-03 18:16:31 +02:00
|
|
|
this._checkPendingTokens()
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_checkPendingTokens() {
|
2020-06-01 19:54:32 +02:00
|
|
|
const { mostRecentOverviewPage, pendingTokens = {}, history } = this.props
|
2018-08-07 02:03:49 +02:00
|
|
|
|
2020-07-03 18:16:31 +02:00
|
|
|
if (Object.keys(pendingTokens).length > 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getEnvironmentType() === ENVIRONMENT_TYPE_NOTIFICATION) {
|
|
|
|
global.platform.closeCurrentWindow()
|
|
|
|
} else {
|
2020-06-01 19:54:32 +02:00
|
|
|
history.push(mostRecentOverviewPage)
|
2018-08-07 02:03:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getTokenName(name, symbol) {
|
|
|
|
return typeof name === 'undefined' ? symbol : `${name} (${symbol})`
|
2018-08-07 02:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
addToken,
|
|
|
|
pendingTokens,
|
|
|
|
tokens,
|
|
|
|
removeSuggestedTokens,
|
|
|
|
history,
|
|
|
|
mostRecentOverviewPage,
|
|
|
|
} = this.props
|
2018-08-15 01:08:12 +02:00
|
|
|
const pendingTokenKey = Object.keys(pendingTokens)[0]
|
|
|
|
const pendingToken = pendingTokens[pendingTokenKey]
|
2020-02-14 21:32:56 +01:00
|
|
|
const hasTokenDuplicates = this.checkTokenDuplicates(pendingTokens, tokens)
|
|
|
|
const reusesName = this.checkNameReuse(pendingTokens, tokens)
|
2018-08-07 02:03:49 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="page-container">
|
|
|
|
<div className="page-container__header">
|
|
|
|
<div className="page-container__title">
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('addSuggestedTokens')}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
|
|
|
<div className="page-container__subtitle">
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('likeToAddTokens')}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{hasTokenDuplicates ? (
|
|
|
|
<div className="warning">{this.context.t('knownTokenWarning')}</div>
|
|
|
|
) : null}
|
|
|
|
{reusesName ? (
|
|
|
|
<div className="warning">
|
|
|
|
{this.context.t('reusedTokenNameWarning')}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
|
|
|
<div className="page-container__content">
|
|
|
|
<div className="confirm-add-token">
|
|
|
|
<div className="confirm-add-token__header">
|
|
|
|
<div className="confirm-add-token__token">
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('token')}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
|
|
|
<div className="confirm-add-token__balance">
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('balance')}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="confirm-add-token__token-list">
|
2020-11-03 00:41:28 +01:00
|
|
|
{Object.entries(pendingTokens).map(([address, token]) => {
|
|
|
|
const { name, symbol, image } = token
|
2018-08-07 02:03:49 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="confirm-add-token__token-list-item"
|
|
|
|
key={address}
|
|
|
|
>
|
|
|
|
<div className="confirm-add-token__token confirm-add-token__data">
|
|
|
|
<Identicon
|
|
|
|
className="confirm-add-token__token-icon"
|
|
|
|
diameter={48}
|
|
|
|
address={address}
|
|
|
|
image={image}
|
|
|
|
/>
|
|
|
|
<div className="confirm-add-token__name">
|
|
|
|
{this.getTokenName(name, symbol)}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
|
|
|
<div className="confirm-add-token__balance">
|
|
|
|
<TokenBalance token={token} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})}
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="page-container__footer">
|
2020-02-27 23:00:20 +01:00
|
|
|
<footer>
|
2018-10-05 22:43:27 +02:00
|
|
|
<Button
|
|
|
|
type="default"
|
|
|
|
large
|
|
|
|
className="page-container__footer-button"
|
|
|
|
onClick={() => {
|
2020-11-03 00:41:28 +01:00
|
|
|
removeSuggestedTokens().then(() =>
|
|
|
|
history.push(mostRecentOverviewPage),
|
|
|
|
)
|
2018-10-05 22:43:27 +02:00
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('cancel')}
|
2018-10-05 22:43:27 +02:00
|
|
|
</Button>
|
|
|
|
<Button
|
2019-04-16 21:35:22 +02:00
|
|
|
type="secondary"
|
2018-10-05 22:43:27 +02:00
|
|
|
large
|
|
|
|
className="page-container__footer-button"
|
2020-07-03 18:03:47 +02:00
|
|
|
disabled={pendingTokens.length === 0}
|
2018-10-05 22:43:27 +02:00
|
|
|
onClick={() => {
|
|
|
|
addToken(pendingToken)
|
|
|
|
.then(() => removeSuggestedTokens())
|
2020-11-17 18:39:21 +01:00
|
|
|
.then(() => {
|
|
|
|
this.context.trackEvent({
|
|
|
|
event: 'Token Added',
|
|
|
|
category: 'Wallet',
|
|
|
|
sensitiveProperties: {
|
|
|
|
token_symbol: pendingToken.symbol,
|
|
|
|
token_contract_address: pendingToken.address,
|
|
|
|
token_decimal_precision: pendingToken.decimals,
|
|
|
|
unlisted: pendingToken.unlisted,
|
|
|
|
source: 'dapp',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2020-06-01 19:54:32 +02:00
|
|
|
.then(() => history.push(mostRecentOverviewPage))
|
2018-10-05 22:43:27 +02:00
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('addToken')}
|
2018-10-05 22:43:27 +02:00
|
|
|
</Button>
|
2020-02-27 23:00:20 +01:00
|
|
|
</footer>
|
2018-08-07 02:03:49 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2020-02-14 21:32:56 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
checkTokenDuplicates(pendingTokens, tokens) {
|
2020-02-14 21:32:56 +01:00
|
|
|
const pending = Object.keys(pendingTokens)
|
2020-02-18 00:12:48 +01:00
|
|
|
const existing = tokens.map((token) => token.address)
|
2020-02-14 21:32:56 +01:00
|
|
|
const dupes = pending.filter((proposed) => {
|
|
|
|
return existing.includes(proposed)
|
|
|
|
})
|
|
|
|
|
|
|
|
return dupes.length > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if any pendingTokens both:
|
|
|
|
* - Share a symbol with an existing `tokens` member.
|
|
|
|
* - Does not share an address with that same `tokens` member.
|
|
|
|
* This should be flagged as possibly deceptive or confusing.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
checkNameReuse(pendingTokens, tokens) {
|
2020-02-14 21:32:56 +01:00
|
|
|
const duplicates = Object.keys(pendingTokens)
|
|
|
|
.map((addr) => pendingTokens[addr])
|
|
|
|
.filter((token) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const dupes = tokens
|
|
|
|
.filter((old) => old.symbol === token.symbol)
|
2020-02-18 00:12:48 +01:00
|
|
|
.filter((old) => old.address !== token.address)
|
2020-02-14 21:32:56 +01:00
|
|
|
return dupes.length > 0
|
|
|
|
})
|
|
|
|
return duplicates.length > 0
|
|
|
|
}
|
2018-08-07 02:03:49 +02:00
|
|
|
}
|