2016-04-14 00:28:44 +02:00
|
|
|
const inherits = require('util').inherits
|
|
|
|
const Component = require('react').Component
|
|
|
|
const ReactCSSTransitionGroup = require('react-addons-css-transition-group')
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const connect = require('react-redux').connect
|
|
|
|
const actions = require('./actions')
|
2016-05-03 23:32:22 +02:00
|
|
|
const txHelper = require('../lib/tx-helper')
|
2016-08-23 02:29:47 +02:00
|
|
|
const isPopupOrNotification = require('../../app/scripts/lib/is-popup-or-notification')
|
2016-09-15 19:24:05 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
|
|
|
const BN = ethUtil.BN
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2016-06-24 21:48:52 +02:00
|
|
|
const PendingTx = require('./components/pending-tx')
|
2016-05-03 23:32:22 +02:00
|
|
|
const PendingMsg = require('./components/pending-msg')
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
module.exports = connect(mapStateToProps)(ConfirmTxScreen)
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
function mapStateToProps (state) {
|
2016-04-14 00:28:44 +02:00
|
|
|
return {
|
|
|
|
identities: state.metamask.identities,
|
|
|
|
accounts: state.metamask.accounts,
|
|
|
|
selectedAddress: state.metamask.selectedAddress,
|
|
|
|
unconfTxs: state.metamask.unconfTxs,
|
2016-05-03 23:32:22 +02:00
|
|
|
unconfMsgs: state.metamask.unconfMsgs,
|
2016-04-14 00:28:44 +02:00
|
|
|
index: state.appState.currentView.context,
|
2016-05-03 23:32:22 +02:00
|
|
|
warning: state.appState.warning,
|
2016-09-08 21:56:04 +02:00
|
|
|
network: state.metamask.network,
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(ConfirmTxScreen, Component)
|
2016-06-21 22:18:32 +02:00
|
|
|
function ConfirmTxScreen () {
|
2016-04-14 00:28:44 +02:00
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
ConfirmTxScreen.prototype.render = function () {
|
2016-04-14 00:28:44 +02:00
|
|
|
var state = this.props
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2016-09-08 21:56:04 +02:00
|
|
|
var network = state.network
|
2016-05-03 23:32:22 +02:00
|
|
|
var unconfTxs = state.unconfTxs
|
|
|
|
var unconfMsgs = state.unconfMsgs
|
2016-09-08 21:56:04 +02:00
|
|
|
var unconfTxList = txHelper(unconfTxs, unconfMsgs, network)
|
2016-05-04 00:04:15 +02:00
|
|
|
var index = state.index !== undefined ? state.index : 0
|
2016-08-25 00:40:40 +02:00
|
|
|
var txData = unconfTxList[index] || unconfTxList[0] || {}
|
2016-08-23 02:29:47 +02:00
|
|
|
var isNotification = isPopupOrNotification() === 'notification'
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
|
2016-07-07 21:27:34 +02:00
|
|
|
h('.flex-column.flex-grow', [
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
// subtitle and nav
|
|
|
|
h('.section-title.flex-row.flex-center', [
|
2016-08-17 02:32:54 +02:00
|
|
|
!isNotification ? h('i.fa.fa-arrow-left.fa-lg.cursor-pointer', {
|
2016-05-03 23:32:22 +02:00
|
|
|
onClick: this.goHome.bind(this),
|
2016-08-17 02:32:54 +02:00
|
|
|
}) : null,
|
2016-07-07 21:27:34 +02:00
|
|
|
h('h2.page-subtitle', 'Confirm Transaction'),
|
2016-04-14 00:28:44 +02:00
|
|
|
]),
|
|
|
|
|
|
|
|
h('h3', {
|
|
|
|
style: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
display: unconfTxList.length > 1 ? 'block' : 'none',
|
|
|
|
},
|
|
|
|
}, [
|
|
|
|
h('i.fa.fa-arrow-left.fa-lg.cursor-pointer', {
|
|
|
|
style: {
|
|
|
|
display: state.index === 0 ? 'none' : 'inline-block',
|
|
|
|
},
|
|
|
|
onClick: () => state.dispatch(actions.previousTx()),
|
|
|
|
}),
|
2016-05-03 23:44:36 +02:00
|
|
|
` ${state.index + 1} of ${unconfTxList.length} `,
|
2016-04-14 00:28:44 +02:00
|
|
|
h('i.fa.fa-arrow-right.fa-lg.cursor-pointer', {
|
|
|
|
style: {
|
|
|
|
display: state.index + 1 === unconfTxList.length ? 'none' : 'inline-block',
|
|
|
|
},
|
|
|
|
onClick: () => state.dispatch(actions.nextTx()),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
|
2016-05-03 23:32:22 +02:00
|
|
|
warningIfExists(state.warning),
|
|
|
|
|
2016-04-14 00:28:44 +02:00
|
|
|
h(ReactCSSTransitionGroup, {
|
2016-05-14 01:28:46 +02:00
|
|
|
className: 'css-transition-group',
|
|
|
|
transitionName: 'main',
|
2016-04-14 00:28:44 +02:00
|
|
|
transitionEnterTimeout: 300,
|
|
|
|
transitionLeaveTimeout: 300,
|
|
|
|
}, [
|
|
|
|
|
2016-05-03 23:32:22 +02:00
|
|
|
currentTxView({
|
|
|
|
// Properties
|
|
|
|
txData: txData,
|
2016-04-14 00:28:44 +02:00
|
|
|
key: txData.id,
|
2016-05-03 23:32:22 +02:00
|
|
|
selectedAddress: state.selectedAddress,
|
|
|
|
accounts: state.accounts,
|
|
|
|
identities: state.identities,
|
|
|
|
// Actions
|
|
|
|
sendTransaction: this.sendTransaction.bind(this, txData),
|
|
|
|
cancelTransaction: this.cancelTransaction.bind(this, txData),
|
|
|
|
signMessage: this.signMessage.bind(this, txData),
|
|
|
|
cancelMessage: this.cancelMessage.bind(this, txData),
|
|
|
|
}),
|
|
|
|
|
2016-04-14 00:28:44 +02:00
|
|
|
]),
|
2016-05-03 23:32:22 +02:00
|
|
|
])
|
2016-04-14 00:28:44 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:32:22 +02:00
|
|
|
function currentTxView (opts) {
|
|
|
|
if ('txParams' in opts.txData) {
|
|
|
|
// This is a pending transaction
|
2016-06-24 21:48:52 +02:00
|
|
|
return h(PendingTx, opts)
|
2016-05-03 23:32:22 +02:00
|
|
|
} else if ('msgParams' in opts.txData) {
|
|
|
|
// This is a pending message to sign
|
|
|
|
return h(PendingMsg, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
ConfirmTxScreen.prototype.sendTransaction = function (txData, event) {
|
2016-09-15 19:24:05 +02:00
|
|
|
var state = this.props
|
|
|
|
|
|
|
|
var txParams = txData.txParams || {}
|
|
|
|
var address = txParams.from || state.selectedAddress
|
|
|
|
var account = state.accounts[address]
|
|
|
|
var balance = account ? account.balance : '0x0'
|
|
|
|
|
|
|
|
var gasCost = new BN(ethUtil.stripHexPrefix(txParams.gas || txData.estimatedGas), 16)
|
|
|
|
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice || '0x4a817c800'), 16)
|
|
|
|
var txFee = gasCost.mul(gasPrice)
|
|
|
|
var txValue = new BN(ethUtil.stripHexPrefix(txParams.value || '0x0'), 16)
|
|
|
|
var maxCost = txValue.add(txFee)
|
|
|
|
|
|
|
|
var balanceBn = new BN(ethUtil.stripHexPrefix(balance), 16)
|
2016-04-14 00:28:44 +02:00
|
|
|
event.stopPropagation()
|
2016-09-15 19:24:05 +02:00
|
|
|
if (maxCost.gt(balanceBn)) {
|
|
|
|
this.props.dispatch(actions.buyEthView(address))
|
|
|
|
} else {
|
|
|
|
this.props.dispatch(actions.sendTx(txData))
|
|
|
|
}
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
ConfirmTxScreen.prototype.cancelTransaction = function (txData, event) {
|
2016-04-14 00:28:44 +02:00
|
|
|
event.stopPropagation()
|
|
|
|
this.props.dispatch(actions.cancelTx(txData))
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
ConfirmTxScreen.prototype.signMessage = function (msgData, event) {
|
2016-05-03 23:32:22 +02:00
|
|
|
var params = msgData.msgParams
|
|
|
|
params.metamaskId = msgData.id
|
2016-04-14 00:28:44 +02:00
|
|
|
event.stopPropagation()
|
2016-05-03 23:32:22 +02:00
|
|
|
this.props.dispatch(actions.signMsg(params))
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
ConfirmTxScreen.prototype.cancelMessage = function (msgData, event) {
|
2016-05-03 23:32:22 +02:00
|
|
|
event.stopPropagation()
|
|
|
|
this.props.dispatch(actions.cancelMsg(msgData))
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
ConfirmTxScreen.prototype.goHome = function (event) {
|
2016-05-03 23:32:22 +02:00
|
|
|
event.stopPropagation()
|
|
|
|
this.props.dispatch(actions.goHome())
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
function warningIfExists (warning) {
|
2016-09-14 23:34:54 +02:00
|
|
|
if (warning &&
|
|
|
|
// Do not display user rejections on this screen:
|
|
|
|
warning.indexOf('User denied transaction signature') === -1) {
|
2016-05-03 23:32:22 +02:00
|
|
|
return h('span.error', { style: { margin: 'auto' } }, warning)
|
|
|
|
}
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|