1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 20:57:12 +01:00
metamask-extension/ui/app/components/token-list.js

189 lines
4.7 KiB
JavaScript
Raw Normal View History

const Component = require('react').Component
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const inherits = require('util').inherits
const TokenTracker = require('eth-token-tracker')
const TokenCell = require('./token-cell.js')
const connect = require('react-redux').connect
2017-09-05 10:48:52 +02:00
const selectors = require('../selectors')
const log = require('loglevel')
2017-09-05 10:48:52 +02:00
function mapStateToProps (state) {
return {
network: state.metamask.network,
tokens: state.metamask.tokens,
userAddress: selectors.getSelectedAddress(state),
2018-08-21 17:59:42 +02:00
assetImages: state.metamask.assetImages,
2017-09-05 10:48:52 +02:00
}
}
const defaultTokens = []
const contracts = require('eth-contract-metadata')
for (const address in contracts) {
const contract = contracts[address]
if (contract.erc20) {
contract.address = address
defaultTokens.push(contract)
}
}
TokenList.contextTypes = {
t: PropTypes.func,
}
2017-09-05 10:48:52 +02:00
module.exports = connect(mapStateToProps)(TokenList)
inherits(TokenList, Component)
function TokenList () {
this.state = {
tokens: [],
isLoading: true,
network: null,
}
Component.call(this)
}
TokenList.prototype.render = function () {
2018-08-21 17:59:42 +02:00
const { userAddress, assetImages } = this.props
const state = this.state
const { tokens, isLoading, error } = state
2017-06-15 00:30:03 +02:00
if (isLoading) {
return this.message(this.context.t('loadingTokens'))
2017-06-15 00:30:03 +02:00
}
if (error) {
log.error(error)
2017-08-16 21:53:53 +02:00
return h('.hotFix', {
style: {
padding: '80px',
},
}, [
this.context.t('troubleTokenBalances'),
2017-08-16 21:53:53 +02:00
h('span.hotFix', {
style: {
color: 'rgba(247, 134, 28, 1)',
cursor: 'pointer',
},
onClick: () => {
global.platform.openWindow({
url: `https://ethplorer.io/address/${userAddress}`,
})
},
}, this.context.t('here')),
2017-08-16 21:53:53 +02:00
])
}
return h('div', tokens.map((tokenData) => {
2018-08-23 20:54:40 +02:00
tokenData.image = assetImages[tokenData.address]
return h(TokenCell, tokenData)
}))
}
2017-06-15 03:08:03 +02:00
TokenList.prototype.message = function (body) {
return h('div', {
style: {
display: 'flex',
height: '250px',
alignItems: 'center',
justifyContent: 'center',
2017-06-29 01:36:58 +02:00
padding: '30px',
2017-06-15 03:08:03 +02:00
},
}, body)
}
TokenList.prototype.componentDidMount = function () {
this.createFreshTokenTracker()
}
TokenList.prototype.createFreshTokenTracker = function () {
if (this.tracker) {
// Clean up old trackers when refreshing:
this.tracker.stop()
this.tracker.removeListener('update', this.balanceUpdater)
this.tracker.removeListener('error', this.showError)
}
if (!global.ethereumProvider) return
const { userAddress } = this.props
2017-09-05 10:48:52 +02:00
this.tracker = new TokenTracker({
userAddress,
provider: global.ethereumProvider,
2017-08-16 18:49:23 +02:00
tokens: this.props.tokens,
pollingInterval: 8000,
})
// Set up listener instances for cleaning up
this.balanceUpdater = this.updateBalances.bind(this)
this.showError = (error) => {
this.setState({ error, isLoading: false })
}
this.tracker.on('update', this.balanceUpdater)
this.tracker.on('error', this.showError)
this.tracker.updateBalances()
.then(() => {
this.updateBalances(this.tracker.serialize())
})
.catch((reason) => {
log.error(`Problem updating balances`, reason)
this.setState({ isLoading: false })
})
}
2017-09-06 09:33:39 +02:00
TokenList.prototype.componentDidUpdate = function (nextProps) {
const {
network: oldNet,
userAddress: oldAddress,
tokens,
2017-09-06 09:33:39 +02:00
} = this.props
const {
network: newNet,
userAddress: newAddress,
tokens: newTokens,
2017-09-06 09:33:39 +02:00
} = nextProps
const isLoading = newNet === 'loading'
const missingInfo = !oldNet || !newNet || !oldAddress || !newAddress
const sameUserAndNetwork = oldAddress === newAddress && oldNet === newNet
const shouldUpdateTokens = isLoading || missingInfo || sameUserAndNetwork
const oldTokensLength = tokens ? tokens.length : 0
const tokensLengthUnchanged = oldTokensLength === newTokens.length
if (tokensLengthUnchanged && shouldUpdateTokens) return
2017-09-06 09:33:39 +02:00
this.setState({ isLoading: true })
this.createFreshTokenTracker()
}
TokenList.prototype.updateBalances = function (tokens) {
if (!this.tracker.running) {
return
}
2017-11-13 23:20:42 +01:00
this.setState({ tokens, isLoading: false })
}
TokenList.prototype.componentWillUnmount = function () {
if (!this.tracker) return
this.tracker.stop()
this.tracker.removeListener('update', this.balanceUpdater)
this.tracker.removeListener('error', this.showError)
}
2017-11-02 03:30:33 +01:00
// function uniqueMergeTokens (tokensA, tokensB = []) {
// const uniqueAddresses = []
// const result = []
// tokensA.concat(tokensB).forEach((token) => {
// const normal = normalizeAddress(token.address)
// if (!uniqueAddresses.includes(normal)) {
// uniqueAddresses.push(normal)
// result.push(token)
// }
// })
// return result
// }