nifty-wallet/ui/app/send.js

249 lines
6.1 KiB
JavaScript
Raw Normal View History

const inherits = require('util').inherits
2016-08-25 14:10:07 -07:00
const PersistentForm = require('../lib/persistent-form')
const h = require('react-hyperscript')
const connect = require('react-redux').connect
2016-05-18 12:13:19 -07:00
const Identicon = require('./components/identicon')
const actions = require('./actions')
const util = require('./util')
const numericBalance = require('./util').numericBalance
2016-05-18 12:13:19 -07:00
const addressSummary = require('./util').addressSummary
2016-09-06 17:28:25 -07:00
const EthBalance = require('./components/eth-balance')
const ethUtil = require('ethereumjs-util')
module.exports = connect(mapStateToProps)(SendTransactionScreen)
2016-06-21 13:18:32 -07:00
function mapStateToProps (state) {
var result = {
2016-05-11 12:54:19 -07:00
address: state.metamask.selectedAccount,
accounts: state.metamask.accounts,
identities: state.metamask.identities,
warning: state.appState.warning,
}
result.error = result.warning && result.warning.split('.')[0]
result.account = result.accounts[result.address]
result.identity = result.identities[result.address]
result.balance = result.account ? numericBalance(result.account.balance) : null
return result
}
2016-08-25 14:10:07 -07:00
inherits(SendTransactionScreen, PersistentForm)
2016-06-21 13:18:32 -07:00
function SendTransactionScreen () {
2016-08-25 14:10:07 -07:00
PersistentForm.call(this)
}
2016-06-21 13:18:32 -07:00
SendTransactionScreen.prototype.render = function () {
2016-08-25 14:10:07 -07:00
this.persistentFormParentId = 'send-tx-form'
var state = this.props
2016-05-18 12:13:19 -07:00
var address = state.address
var account = state.account
var identity = state.identity
return (
2016-05-18 12:13:19 -07:00
h('.send-screen.flex-column.flex-grow', [
2016-05-18 12:13:19 -07:00
//
// Sender Profile
//
h('.account-data-subsection.flex-column.flex-grow', {
style: {
margin: '0 20px',
},
}, [
// header - identicon + nav
h('.flex-row.flex-space-between', {
style: {
marginTop: 28,
},
}, [
2016-06-24 11:44:18 -07:00
// back button
2016-05-18 12:13:19 -07:00
h('i.fa.fa-arrow-left.fa-lg.cursor-pointer.color-orange', {
onClick: this.back.bind(this),
}),
// large identicon
h('.identicon-wrapper.flex-column.flex-center.select-none', [
h(Identicon, {
diameter: 62,
address: address,
}),
]),
2016-06-24 11:44:18 -07:00
// invisible place holder
h('i.fa.fa-users.fa-lg.invisible'),
2016-05-18 12:13:19 -07:00
]),
// account label
h('h2.font-medium.color-forest.flex-center', {
style: {
paddingTop: 8,
marginBottom: 8,
},
}, identity && identity.name),
// address and getter actions
h('.flex-row.flex-center', {
style: {
marginBottom: 8,
},
}, [
h('div', {
style: {
lineHeight: '16px',
},
}, addressSummary(address)),
]),
// balance
h('.flex-row.flex-center', [
2016-09-06 17:28:25 -07:00
h(EthBalance, {
2016-05-18 13:41:08 -07:00
value: account && account.balance,
2016-06-21 13:18:32 -07:00
}),
2016-05-18 12:13:19 -07:00
]),
]),
2016-05-18 12:13:19 -07:00
//
// Required Fields
//
h('h3.flex-center.text-transform-uppercase', {
style: {
background: '#EBEBEB',
color: '#AEAEAE',
marginTop: 32,
marginBottom: 16,
},
}, [
'Send Transaction',
]),
// error message
state.error && h('span.error.flex-center', state.error),
2016-05-18 12:13:19 -07:00
// 'to' field
h('section.flex-row.flex-center', [
h('input.large-input', {
name: 'address',
placeholder: 'Recipient Address',
dataset: {
2016-08-25 14:10:07 -07:00
persistentFormId: 'recipient-address',
},
2016-06-21 13:18:32 -07:00
}),
]),
2016-05-18 12:13:19 -07:00
// 'amount' and send button
h('section.flex-row.flex-center', [
h('input.large-input', {
name: 'amount',
placeholder: 'Amount',
type: 'number',
2016-05-18 12:13:19 -07:00
style: {
marginRight: 6,
},
dataset: {
2016-08-25 14:10:07 -07:00
persistentFormId: 'tx-amount',
},
}),
2016-05-18 12:13:19 -07:00
h('button.primary', {
onClick: this.onSubmit.bind(this),
style: {
textTransform: 'uppercase',
},
2016-06-21 13:18:32 -07:00
}, 'Send'),
2016-05-18 12:13:19 -07:00
]),
2016-05-18 12:13:19 -07:00
//
// Optional Fields
//
h('h3.flex-center.text-transform-uppercase', {
style: {
background: '#EBEBEB',
color: '#AEAEAE',
marginTop: 16,
marginBottom: 16,
},
}, [
2016-07-18 02:08:00 -07:00
'Transactional Data (optional)',
]),
2016-05-18 12:13:19 -07:00
// 'data' field
h('section.flex-row.flex-center', [
h('input.large-input', {
name: 'txData',
2016-05-18 12:13:19 -07:00
placeholder: '0x01234',
style: {
width: '100%',
resize: 'none',
2016-06-21 13:18:32 -07:00
},
dataset: {
2016-08-25 14:10:07 -07:00
persistentFormId: 'tx-data',
},
2016-05-18 12:13:19 -07:00
}),
]),
])
)
}
2016-06-21 13:18:32 -07:00
SendTransactionScreen.prototype.navigateToAccounts = function (event) {
2016-05-18 12:13:19 -07:00
event.stopPropagation()
this.props.dispatch(actions.showAccountsPage())
}
2016-06-21 13:18:32 -07:00
SendTransactionScreen.prototype.back = function () {
var address = this.props.address
this.props.dispatch(actions.backToAccountDetail(address))
}
2016-06-21 13:18:32 -07:00
SendTransactionScreen.prototype.onSubmit = function () {
const recipient = document.querySelector('input[name="address"]').value
const input = document.querySelector('input[name="amount"]').value
const value = util.normalizeEthStringToWei(input)
const txData = document.querySelector('input[name="txData"]').value
const balance = this.props.balance
2016-06-21 13:56:04 -07:00
let message
if (value.gt(balance)) {
2016-06-21 13:56:04 -07:00
message = 'Insufficient funds.'
return this.props.dispatch(actions.displayWarning(message))
}
if (input < 0) {
message = 'Can not send negative amounts of ETH.'
return this.props.dispatch(actions.displayWarning(message))
}
2016-05-19 14:21:35 -07:00
if ((!util.isValidAddress(recipient) && !txData) || (!recipient && !txData)) {
2016-06-21 13:56:04 -07:00
message = 'Recipient address is invalid.'
return this.props.dispatch(actions.displayWarning(message))
}
this.props.dispatch(actions.hideWarning())
var txParams = {
from: this.props.address,
value: '0x' + value.toString(16),
}
2016-05-19 16:14:16 -07:00
if (recipient) txParams.to = ethUtil.addHexPrefix(recipient)
if (txData) txParams.data = txData
this.props.dispatch(actions.signTx(txParams))
}