1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 21:00:13 +01:00
metamask-extension/ui/app/components/pending-tx-details.js

318 lines
8.4 KiB
JavaScript
Raw Normal View History

2016-06-24 21:48:52 +02:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const extend = require('xtend')
2017-02-28 03:18:39 +01:00
const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN
2016-06-24 21:48:52 +02:00
2016-07-07 02:58:46 +02:00
const MiniAccountPanel = require('./mini-account-panel')
2016-09-07 02:28:25 +02:00
const EthBalance = require('./eth-balance')
const util = require('../util')
const addressSummary = util.addressSummary
2016-07-07 02:58:46 +02:00
const nameForAddress = require('../../lib/contract-namer')
const HexInput = require('./hex-as-decimal-input')
2016-07-07 07:48:02 +02:00
2016-06-24 21:48:52 +02:00
module.exports = PendingTxDetails
inherits(PendingTxDetails, Component)
function PendingTxDetails () {
Component.call(this)
}
2016-07-07 02:58:46 +02:00
const PTXP = PendingTxDetails.prototype
2016-06-24 21:48:52 +02:00
2016-07-07 02:58:46 +02:00
PTXP.render = function () {
2016-07-07 06:32:36 +02:00
var props = this.props
var state = this.state || {}
var txData = state.txMeta || props.txData
2016-06-24 21:48:52 +02:00
var txParams = txData.txParams || {}
var address = txParams.from || props.selectedAddress
2016-07-07 06:32:36 +02:00
var identity = props.identities[address] || { address: address }
var account = props.accounts[address]
var balance = account ? account.balance : '0x0'
2016-06-24 21:48:52 +02:00
const gas = state.gas || txParams.gas
const gasPrice = state.gasPrice || txData.gasPrice
var txFee = state.txFee || txData.txFee || ''
var maxCost = state.maxCost || txData.maxCost || ''
2016-07-07 08:35:59 +02:00
var dataLength = txParams.data ? (txParams.data.length - 2) / 2 : 0
2016-07-08 01:54:35 +02:00
var imageify = props.imageifyIdenticons === undefined ? true : props.imageifyIdenticons
log.debug(`rendering gas: ${gas}, gasPrice: ${gasPrice}, txFee: ${txFee}, maxCost: ${maxCost}`)
2016-07-07 02:58:46 +02:00
return (
2016-06-24 21:48:52 +02:00
h('div', [
2016-07-07 02:58:46 +02:00
h('.flex-row.flex-center', {
style: {
maxWidth: '100%',
},
}, [
h(MiniAccountPanel, {
imageSeed: address,
2016-07-08 01:54:35 +02:00
imageifyIdenticons: imageify,
2016-07-07 02:58:46 +02:00
picOrder: 'right',
}, [
h('span.font-small', {
style: {
fontFamily: 'Montserrat Bold, Montserrat, sans-serif',
},
}, identity.name),
h('span.font-small', {
style: {
fontFamily: 'Montserrat Light, Montserrat, sans-serif',
},
}, addressSummary(address, 6, 4, false)),
h('span.font-small', {
style: {
fontFamily: 'Montserrat Light, Montserrat, sans-serif',
},
2016-07-08 01:54:35 +02:00
}, [
2016-09-07 02:28:25 +02:00
h(EthBalance, {
2016-07-08 01:54:35 +02:00
value: balance,
inline: true,
labelColor: '#F7861C',
2016-07-08 02:00:24 +02:00
}),
2016-07-08 01:54:35 +02:00
]),
]),
2016-07-07 02:58:46 +02:00
forwardCarrat(),
2016-07-07 02:58:46 +02:00
this.miniAccountPanelForRecipient(),
]),
2016-06-24 21:48:52 +02:00
2016-07-07 06:32:36 +02:00
h('style', `
.table-box {
margin: 7px 0px 0px 0px;
width: 100%;
2016-07-07 06:32:36 +02:00
}
.table-box .row {
margin: 0px;
background: rgb(236,236,236);
display: flex;
justify-content: space-between;
font-family: Montserrat Light, sans-serif;
font-size: 13px;
padding: 5px 25px;
}
.table-box .row .value {
font-family: Montserrat Regular;
2016-07-07 06:32:36 +02:00
}
`),
h('.table-box', [
h('.row', [
h('.cell.label', 'Amount'),
2016-09-07 02:28:25 +02:00
h(EthBalance, { value: txParams.value }),
2016-06-24 21:48:52 +02:00
]),
2016-07-07 06:32:36 +02:00
h('.cell.row', [
h('.cell.label', 'Max Transaction Fee'),
2016-09-07 02:28:25 +02:00
h(EthBalance, { value: txFee.toString(16) }),
2016-06-24 21:48:52 +02:00
]),
2016-07-07 06:32:36 +02:00
h('.cell.row', {
style: {
fontFamily: 'Montserrat Regular',
background: 'white',
padding: '10px 25px',
2016-07-07 06:32:36 +02:00
},
}, [
h('.cell.label', 'Max Total'),
h('.cell.value', {
style: {
display: 'flex',
alignItems: 'center',
},
}, [
2016-09-07 02:28:25 +02:00
h(EthBalance, {
value: maxCost.toString(16),
inline: true,
labelColor: 'black',
fontSize: '16px',
}),
]),
2016-06-24 21:48:52 +02:00
]),
h('.cell.row', {
2016-07-07 06:32:36 +02:00
}, [
h('.cell.label', 'Gas Limit'),
h('.cell.value', {
}, [
h(HexInput, {
value: gas,
suffix: 'UNITS',
style: {
position: 'relative',
top: '5px',
},
onChange: (newHex) => {
log.info(`Gas limit changed to ${newHex}`)
this.setState({ gas: newHex })
},
}),
2017-02-28 01:09:05 +01:00
]),
]),
h('.cell.row', {
}, [
h('.cell.label', 'Gas Price'),
h('.cell.value', {
}, [
h(HexInput, {
value: gasPrice,
suffix: 'WEI',
style: {
position: 'relative',
top: '5px',
},
onChange: (newHex) => {
log.info(`Gas price changed to: ${newHex}`)
this.setState({ gasPrice: newHex })
},
}),
2017-02-28 01:09:05 +01:00
]),
]),
2016-07-07 06:32:36 +02:00
h('.cell.row', {
style: {
background: '#f7f7f7',
paddingBottom: '0px',
2016-07-07 07:48:02 +02:00
},
2016-07-07 06:32:36 +02:00
}, [
h('.cell.label'),
h('.cell.value', {
style: {
fontFamily: 'Montserrat Light',
fontSize: '11px',
},
}, `Data included: ${dataLength} bytes`),
2016-07-07 07:48:02 +02:00
]),
2016-07-07 06:32:36 +02:00
]), // End of Table
2016-06-24 21:48:52 +02:00
])
)
2016-07-07 02:58:46 +02:00
}
2016-06-24 21:48:52 +02:00
2016-07-07 07:48:02 +02:00
PTXP.miniAccountPanelForRecipient = function () {
2016-07-07 06:32:36 +02:00
var props = this.props
var txData = props.txData
2016-07-07 02:58:46 +02:00
var txParams = txData.txParams || {}
var isContractDeploy = !('to' in txParams)
2016-07-12 00:49:13 +02:00
var imageify = props.imageifyIdenticons === undefined ? true : props.imageifyIdenticons
2016-07-07 02:58:46 +02:00
// If it's not a contract deploy, send to the account
if (!isContractDeploy) {
return h(MiniAccountPanel, {
2016-07-07 07:48:02 +02:00
imageSeed: txParams.to,
2016-07-12 00:49:13 +02:00
imageifyIdenticons: imageify,
2016-07-07 02:58:46 +02:00
picOrder: 'left',
}, [
h('span.font-small', {
style: {
fontFamily: 'Montserrat Bold, Montserrat, sans-serif',
},
}, nameForAddress(txParams.to, props.identities)),
h('span.font-small', {
style: {
fontFamily: 'Montserrat Light, Montserrat, sans-serif',
},
}, addressSummary(txParams.to, 6, 4, false)),
])
2016-07-07 02:58:46 +02:00
} else {
2016-07-07 07:48:02 +02:00
return h(MiniAccountPanel, {
2016-07-12 00:49:13 +02:00
imageifyIdenticons: imageify,
2016-07-07 02:58:46 +02:00
picOrder: 'left',
}, [
h('span.font-small', {
style: {
fontFamily: 'Montserrat Bold, Montserrat, sans-serif',
},
}, 'New Contract'),
])
2016-07-07 02:58:46 +02:00
}
2016-06-24 21:48:52 +02:00
}
2016-07-07 02:58:46 +02:00
PTXP.componentDidUpdate = function (prevProps, prevState) {
const state = this.state || {}
log.debug(`pending-tx-details componentDidUpdate`)
console.log(arguments)
// Only if gas or gasPrice changed:
if (prevState &&
(state.gas !== prevState.gas ||
2017-02-28 03:18:39 +01:00
state.gasPrice !== prevState.gasPrice)) {
log.debug(`recalculating gas since prev state change: ${JSON.stringify({ prevState, state })}`)
2017-02-28 03:18:39 +01:00
this.calculateGas()
2017-02-28 03:33:33 +01:00
// Otherwise this was a recalculation,
// so we should inform the parent:
} else {
if (this.props.onTxChange) {
this.props.onTxChange(this.gatherParams)
}
}
}
PTXP.gatherParams = function () {
log.debug(`pending-tx-details#gatherParams`)
const props = this.props
const state = this.state || {}
const txData = state.txData || props.txData
const txParams = txData.txParams
const gas = state.gas || txParams.gas
const gasPrice = state.gasPrice || txParams.gasPrice
const resultTx = extend(txParams, {
gas,
gasPrice,
})
const resultTxMeta = extend(txData, {
txParams: resultTx,
})
log.debug(`gathered params: ${JSON.stringify(resultTxMeta)}`)
return resultTxMeta
}
PTXP.calculateGas = function () {
const txMeta = this.gatherParams()
2017-02-28 01:09:05 +01:00
log.debug(`pending-tx-details calculating gas for ${JSON.stringify(txMeta)}`)
2017-02-28 03:18:39 +01:00
var txParams = txMeta.txParams
var gasMultiplier = txMeta.gasMultiplier
var gasCost = new BN(ethUtil.stripHexPrefix(txParams.gas || txMeta.estimatedGas), 16)
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice || '0x4a817c800'), 16)
gasPrice = gasPrice.mul(new BN(gasMultiplier * 100), 10).div(new BN(100, 10))
var txFee = gasCost.mul(gasPrice)
var txValue = new BN(ethUtil.stripHexPrefix(txParams.value || '0x0'), 16)
var maxCost = txValue.add(txFee)
this.setState({
txFee: '0x' + txFee.toString('hex'),
maxCost: '0x' + maxCost.toString('hex'),
})
}
function forwardCarrat () {
return (
2016-07-08 01:54:35 +02:00
h('img', {
src: 'images/forward-carrat.svg',
style: {
padding: '5px 6px 0px 10px',
height: '37px',
},
})
2016-07-08 01:54:35 +02:00
)
2016-07-12 00:49:13 +02:00
}