2017-09-22 07:25:16 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const { connect } = require('react-redux')
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const clone = require('clone')
|
|
|
|
const abi = require('human-standard-token-abi')
|
|
|
|
const abiDecoder = require('abi-decoder')
|
|
|
|
abiDecoder.addABI(abi)
|
|
|
|
const inherits = require('util').inherits
|
|
|
|
const actions = require('../../actions')
|
|
|
|
const util = require('../../util')
|
|
|
|
const ConfirmSendEther = require('./confirm-send-ether')
|
2017-09-22 23:34:56 +02:00
|
|
|
const ConfirmSendToken = require('./confirm-send-token')
|
2017-09-27 04:21:04 +02:00
|
|
|
const ConfirmDeployContract = require('./confirm-deploy-contract')
|
2017-09-22 07:25:16 +02:00
|
|
|
|
|
|
|
const TX_TYPES = {
|
|
|
|
DEPLOY_CONTRACT: 'deploy_contract',
|
|
|
|
SEND_ETHER: 'send_ether',
|
|
|
|
SEND_TOKEN: 'send_token',
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(PendingTx)
|
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
|
|
|
const {
|
|
|
|
conversionRate,
|
|
|
|
identities,
|
|
|
|
} = state.metamask
|
|
|
|
const accounts = state.metamask.accounts
|
|
|
|
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
|
|
|
|
return {
|
|
|
|
conversionRate,
|
|
|
|
identities,
|
|
|
|
selectedAddress,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return {
|
|
|
|
backToAccountDetail: address => dispatch(actions.backToAccountDetail(address)),
|
|
|
|
cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(PendingTx, Component)
|
|
|
|
function PendingTx () {
|
|
|
|
Component.call(this)
|
|
|
|
this.state = {
|
|
|
|
isFetching: true,
|
|
|
|
transactionType: '',
|
2017-09-22 23:34:56 +02:00
|
|
|
tokenAddress: '',
|
|
|
|
tokenSymbol: '',
|
|
|
|
tokenDecimals: '',
|
2017-09-22 07:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 23:34:56 +02:00
|
|
|
PendingTx.prototype.componentWillMount = async function () {
|
2017-09-22 07:25:16 +02:00
|
|
|
const txMeta = this.gatherTxMeta()
|
|
|
|
const txParams = txMeta.txParams || {}
|
|
|
|
|
2017-09-22 23:34:56 +02:00
|
|
|
if (!txParams.to) {
|
|
|
|
return this.setState({
|
|
|
|
transactionType: TX_TYPES.DEPLOY_CONTRACT,
|
|
|
|
isFetching: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-09-22 07:25:16 +02:00
|
|
|
const token = util.getContractAtAddress(txParams.to)
|
2017-09-22 23:34:56 +02:00
|
|
|
const results = await Promise.all([
|
|
|
|
token.symbol(),
|
|
|
|
token.decimals(),
|
|
|
|
])
|
|
|
|
|
|
|
|
const [ symbol, decimals ] = results
|
|
|
|
|
|
|
|
if (symbol[0] && decimals[0]) {
|
2017-09-22 07:25:16 +02:00
|
|
|
this.setState({
|
2017-09-22 23:34:56 +02:00
|
|
|
transactionType: TX_TYPES.SEND_TOKEN,
|
|
|
|
tokenAddress: txParams.to,
|
|
|
|
tokenSymbol: symbol[0],
|
|
|
|
tokenDecimals: decimals[0],
|
2017-09-22 07:25:16 +02:00
|
|
|
isFetching: false,
|
|
|
|
})
|
2017-09-22 23:34:56 +02:00
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
transactionType: TX_TYPES.SEND_ETHER,
|
|
|
|
isFetching: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2017-09-22 07:25:16 +02:00
|
|
|
this.setState({
|
2017-09-22 23:34:56 +02:00
|
|
|
transactionType: TX_TYPES.SEND_ETHER,
|
2017-09-22 07:25:16 +02:00
|
|
|
isFetching: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingTx.prototype.gatherTxMeta = function () {
|
|
|
|
const props = this.props
|
|
|
|
const state = this.state
|
|
|
|
const txData = clone(state.txData) || clone(props.txData)
|
|
|
|
|
|
|
|
return txData
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingTx.prototype.render = function () {
|
2017-09-22 23:34:56 +02:00
|
|
|
const {
|
|
|
|
isFetching,
|
|
|
|
transactionType,
|
|
|
|
tokenAddress,
|
|
|
|
tokenSymbol,
|
|
|
|
tokenDecimals,
|
|
|
|
} = this.state
|
|
|
|
|
|
|
|
const { sendTransaction } = this.props
|
2017-09-22 07:25:16 +02:00
|
|
|
|
|
|
|
if (isFetching) {
|
|
|
|
return h('noscript')
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (transactionType) {
|
|
|
|
case TX_TYPES.SEND_ETHER:
|
2017-09-22 23:34:56 +02:00
|
|
|
return h(ConfirmSendEther, {
|
|
|
|
txData: this.gatherTxMeta(),
|
|
|
|
sendTransaction,
|
|
|
|
})
|
|
|
|
case TX_TYPES.SEND_TOKEN:
|
|
|
|
return h(ConfirmSendToken, {
|
|
|
|
txData: this.gatherTxMeta(),
|
|
|
|
sendTransaction,
|
|
|
|
token: {
|
|
|
|
address: tokenAddress,
|
|
|
|
symbol: tokenSymbol,
|
|
|
|
decimals: tokenDecimals,
|
|
|
|
},
|
|
|
|
})
|
2017-09-27 04:21:04 +02:00
|
|
|
case TX_TYPES.DEPLOY_CONTRACT:
|
|
|
|
return h(ConfirmDeployContract, {
|
|
|
|
txData: this.gatherTxMeta(),
|
|
|
|
sendTransaction,
|
|
|
|
})
|
2017-09-22 07:25:16 +02:00
|
|
|
default:
|
|
|
|
return h('noscript')
|
|
|
|
}
|
|
|
|
}
|