1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 02:58:09 +01:00
metamask-extension/ui/app/components/token-balance.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-09-06 12:17:49 +02:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const TokenTracker = require('eth-token-tracker')
const connect = require('react-redux').connect
2017-09-06 12:17:49 +02:00
const selectors = require('../selectors')
const log = require('loglevel')
2017-09-06 12:17:49 +02:00
function mapStateToProps (state) {
return {
userAddress: selectors.getSelectedAddress(state),
}
}
module.exports = connect(mapStateToProps)(TokenBalance)
inherits(TokenBalance, Component)
function TokenBalance () {
this.state = {
2017-09-22 03:44:52 +02:00
string: '',
symbol: '',
2017-09-06 12:17:49 +02:00
isLoading: true,
error: null,
}
Component.call(this)
}
TokenBalance.prototype.render = function () {
const state = this.state
const { symbol, string, isLoading } = state
const { balanceOnly } = this.props
2017-09-06 12:17:49 +02:00
return isLoading
? h('span', '')
2017-09-22 03:44:52 +02:00
: h('span.token-balance', [
h('span.token-balance__amount', string),
!balanceOnly && h('span.token-balance__symbol', symbol),
])
2017-09-06 12:17:49 +02:00
}
TokenBalance.prototype.componentDidMount = function () {
this.createFreshTokenTracker()
}
TokenBalance.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, token } = this.props
this.tracker = new TokenTracker({
userAddress,
provider: global.ethereumProvider,
tokens: [token],
pollingInterval: 8000,
})
// Set up listener instances for cleaning up
this.balanceUpdater = this.updateBalance.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.updateBalance(this.tracker.serialize())
})
.catch((reason) => {
log.error(`Problem updating balances`, reason)
this.setState({ isLoading: false })
})
}
TokenBalance.prototype.componentDidUpdate = function (nextProps) {
const {
userAddress: oldAddress,
token: { address: oldTokenAddress },
2017-09-06 12:17:49 +02:00
} = this.props
const {
userAddress: newAddress,
token: { address: newTokenAddress },
2017-09-06 12:17:49 +02:00
} = nextProps
if ((!oldAddress || !newAddress) && (!oldTokenAddress || !newTokenAddress)) return
if ((oldAddress === newAddress) && (oldTokenAddress === newTokenAddress)) return
2017-09-06 12:17:49 +02:00
this.setState({ isLoading: true })
this.createFreshTokenTracker()
}
TokenBalance.prototype.updateBalance = function (tokens = []) {
2017-09-07 08:03:23 +02:00
const [{ string, symbol }] = tokens
2017-09-07 13:24:03 +02:00
2017-09-06 12:17:49 +02:00
this.setState({
2017-09-22 03:44:52 +02:00
string,
symbol,
2017-09-06 12:17:49 +02:00
isLoading: false,
})
}
TokenBalance.prototype.componentWillUnmount = function () {
if (!this.tracker) return
this.tracker.stop()
}