2017-09-22 07:25:16 +02:00
|
|
|
const Component = require('react').Component
|
2018-03-29 17:00:44 +02:00
|
|
|
const connect = require('react-redux').connect
|
2017-09-22 07:25:16 +02:00
|
|
|
const h = require('react-hyperscript')
|
2018-04-14 09:00:48 +02:00
|
|
|
const PropTypes = require('prop-types')
|
2017-09-22 07:25:16 +02:00
|
|
|
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')
|
2018-04-28 01:15:36 +02:00
|
|
|
const { getSymbolAndDecimals } = require('../../token-util')
|
2017-09-22 07:25:16 +02:00
|
|
|
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')
|
2018-05-01 03:28:34 +02:00
|
|
|
const Loading = require('../loading-screen')
|
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,
|
2018-04-28 01:15:36 +02:00
|
|
|
tokens: existingTokens,
|
2017-09-22 07:25:16 +02:00
|
|
|
} = state.metamask
|
|
|
|
const accounts = state.metamask.accounts
|
|
|
|
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
|
|
|
|
return {
|
|
|
|
conversionRate,
|
|
|
|
identities,
|
|
|
|
selectedAddress,
|
2018-04-28 01:15:36 +02:00
|
|
|
existingTokens,
|
2017-09-22 07:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:00:48 +02:00
|
|
|
PendingTx.prototype.componentDidMount = function () {
|
|
|
|
this.setTokenData()
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingTx.prototype.componentDidUpdate = function (prevProps, prevState) {
|
|
|
|
if (prevState.isFetching) {
|
|
|
|
this.setTokenData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingTx.prototype.setTokenData = async function () {
|
2018-04-28 01:15:36 +02:00
|
|
|
const { existingTokens } = this.props
|
2017-09-22 07:25:16 +02:00
|
|
|
const txMeta = this.gatherTxMeta()
|
|
|
|
const txParams = txMeta.txParams || {}
|
|
|
|
|
2018-04-14 09:00:48 +02:00
|
|
|
if (txMeta.loadingDefaults) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-22 23:34:56 +02:00
|
|
|
if (!txParams.to) {
|
|
|
|
return this.setState({
|
|
|
|
transactionType: TX_TYPES.DEPLOY_CONTRACT,
|
|
|
|
isFetching: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-21 21:12:59 +01:00
|
|
|
// inspect tx data for supported special confirmation screens
|
|
|
|
let isTokenTransaction = false
|
|
|
|
if (txParams.data) {
|
|
|
|
const tokenData = abiDecoder.decodeMethod(txParams.data)
|
|
|
|
const { name: tokenMethodName } = tokenData || {}
|
|
|
|
isTokenTransaction = (tokenMethodName === 'transfer')
|
|
|
|
}
|
2017-09-22 23:34:56 +02:00
|
|
|
|
2018-03-20 12:37:23 +01:00
|
|
|
if (isTokenTransaction) {
|
2018-04-28 01:15:36 +02:00
|
|
|
const { symbol, decimals } = await getSymbolAndDecimals(txParams.to, existingTokens)
|
|
|
|
|
2018-04-28 02:03:56 +02:00
|
|
|
this.setState({
|
|
|
|
transactionType: TX_TYPES.SEND_TOKEN,
|
|
|
|
tokenAddress: txParams.to,
|
|
|
|
tokenSymbol: symbol,
|
|
|
|
tokenDecimals: decimals,
|
|
|
|
isFetching: false,
|
|
|
|
})
|
2018-03-20 12:37:23 +01:00
|
|
|
} else {
|
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) {
|
2018-04-14 09:00:48 +02:00
|
|
|
return h(Loading, {
|
2018-04-18 09:56:52 +02:00
|
|
|
fullScreen: true,
|
|
|
|
loadingMessage: this.context.t('generatingTransaction'),
|
2018-04-14 09:00:48 +02:00
|
|
|
})
|
2017-09-22 07:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2018-04-18 09:56:52 +02:00
|
|
|
return h(Loading, {
|
|
|
|
fullScreen: true,
|
|
|
|
})
|
2017-09-22 07:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-14 09:00:48 +02:00
|
|
|
|
|
|
|
PendingTx.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|