1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 02:58:09 +01:00
metamask-extension/ui/app/send.js

233 lines
5.7 KiB
JavaScript
Raw Normal View History

const inherits = require('util').inherits
const Component = require('react').Component
const h = require('react-hyperscript')
const connect = require('react-redux').connect
2016-05-18 21:13:19 +02:00
const Identicon = require('./components/identicon')
const actions = require('./actions')
const util = require('./util')
const numericBalance = require('./util').numericBalance
2016-05-18 21:13:19 +02:00
const formatBalance = require('./util').formatBalance
const addressSummary = require('./util').addressSummary
const AccountPanel = require('./components/account-panel')
const ethUtil = require('ethereumjs-util')
module.exports = connect(mapStateToProps)(SendTransactionScreen)
function mapStateToProps(state) {
var result = {
2016-05-11 21:54:19 +02:00
address: state.metamask.selectedAccount,
accounts: state.metamask.accounts,
identities: state.metamask.identities,
warning: state.appState.warning,
}
result.account = result.accounts[result.address]
result.identity = result.identities[result.address]
result.balance = result.account ? numericBalance(result.account.balance) : null
return result
}
inherits(SendTransactionScreen, Component)
function SendTransactionScreen() {
Component.call(this)
}
SendTransactionScreen.prototype.render = function() {
var state = this.props
2016-05-18 21:13:19 +02:00
var address = state.address
var account = state.account
var identity = state.identity
return (
2016-05-18 21:13:19 +02:00
h('.send-screen.flex-column.flex-grow', [
2016-05-18 21:13:19 +02: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,
},
}, [
// invisible placeholder for later
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,
}),
]),
// small accounts nav
h('i.fa.fa-users.fa-lg.cursor-pointer.color-orange', {
onClick: this.navigateToAccounts.bind(this),
}),
]),
// 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', [
h('div', formatBalance(account && account.balance)),
]),
]),
2016-05-18 21:13:19 +02:00
//
// Required Fields
//
h('h3.flex-center.text-transform-uppercase', {
style: {
background: '#EBEBEB',
color: '#AEAEAE',
marginTop: 32,
marginBottom: 16,
},
}, [
'Send Transaction',
]),
2016-05-18 21:13:19 +02:00
// 'to' field
h('section.flex-row.flex-center', [
h('input.address.large-input', {
placeholder: 'Recipient Address',
})
]),
2016-05-18 21:13:19 +02:00
// 'amount' and send button
h('section.flex-row.flex-center', [
h('input.ether.large-input', {
placeholder: 'Amount',
type: 'number',
2016-05-18 21:13:19 +02:00
style: {
marginRight: 6,
},
}),
2016-05-18 21:13:19 +02:00
h('button.primary', {
onClick: this.onSubmit.bind(this),
style: {
textTransform: 'uppercase',
},
}, 'Send')
]),
2016-05-18 21:13:19 +02:00
//
// Optional Fields
//
h('h3.flex-center.text-transform-uppercase', {
style: {
background: '#EBEBEB',
color: '#AEAEAE',
marginTop: 16,
marginBottom: 16,
},
}, [
2016-05-18 21:13:19 +02:00
'Tranasactional Data (optional)',
]),
2016-05-18 21:13:19 +02:00
// 'data' field
h('section.flex-row.flex-center', [
h('input.txData.large-input', {
placeholder: '0x01234',
style: {
width: '100%',
resize: 'none',
}
}),
]),
// state.warning ? h('span.error', state.warning.split('.')[0]) : null,
])
2016-05-18 21:13:19 +02:00
)
}
2016-05-18 21:13:19 +02:00
SendTransactionScreen.prototype.navigateToAccounts = function(event){
event.stopPropagation()
this.props.dispatch(actions.showAccountsPage())
}
SendTransactionScreen.prototype.back = function() {
var address = this.props.address
this.props.dispatch(actions.backToAccountDetail(address))
}
SendTransactionScreen.prototype.onSubmit = function(event) {
var recipient = document.querySelector('input.address').value
var inputAmount = parseFloat(document.querySelector('input.ether').value)
var currency = document.querySelector('select.currency').value
var value = util.normalizeNumberToWei(inputAmount, currency)
var balance = this.props.balance
if (value.gt(balance)) {
var message = 'Insufficient funds.'
return this.props.dispatch(actions.displayWarning(message))
}
if (recipient.length !== 42) {
var message = 'Recipient address is the incorrect length.'
return this.props.dispatch(actions.displayWarning(message))
}
this.props.dispatch(actions.hideWarning())
this.props.dispatch(actions.showLoadingIndication())
var txParams = {
to: recipient,
from: this.props.address,
value: '0x' + value.toString(16),
}
var txData = document.querySelector('textarea.txData').value
if (txData) txParams.data = txData
this.props.dispatch(actions.signTx(txParams))
}