2017-10-05 17:44:45 +02:00
|
|
|
const { inherits } = require('util')
|
2018-03-29 17:00:44 +02:00
|
|
|
const PropTypes = require('prop-types')
|
2017-10-05 17:44:45 +02:00
|
|
|
const PersistentForm = require('../lib/persistent-form')
|
|
|
|
const h = require('react-hyperscript')
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
const ethAbi = require('ethereumjs-abi')
|
2017-10-26 18:43:12 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
|
|
|
|
2017-10-06 06:34:00 +02:00
|
|
|
const FromDropdown = require('./components/send/from-dropdown')
|
2018-03-26 15:30:54 +02:00
|
|
|
const EnsInput = require('./components/ens-input')
|
2017-10-09 18:25:23 +02:00
|
|
|
const CurrencyDisplay = require('./components/send/currency-display')
|
2017-10-13 22:19:22 +02:00
|
|
|
const MemoTextArea = require('./components/send/memo-textarea')
|
|
|
|
const GasFeeDisplay = require('./components/send/gas-fee-display-v2')
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-10-26 18:43:12 +02:00
|
|
|
const {
|
2017-12-20 23:36:58 +01:00
|
|
|
TOKEN_TRANSFER_FUNCTION_SIGNATURE,
|
2017-10-26 18:43:12 +02:00
|
|
|
} = require('./components/send/send-constants')
|
2017-10-18 21:23:35 +02:00
|
|
|
|
2017-10-19 20:23:02 +02:00
|
|
|
const {
|
|
|
|
multiplyCurrencies,
|
|
|
|
conversionGreaterThan,
|
2017-10-26 18:43:12 +02:00
|
|
|
subtractCurrencies,
|
2017-10-19 20:23:02 +02:00
|
|
|
} = require('./conversion-util')
|
2017-10-30 18:48:50 +01:00
|
|
|
const {
|
|
|
|
calcTokenAmount,
|
|
|
|
} = require('./token-util')
|
2017-10-22 22:14:03 +02:00
|
|
|
const {
|
|
|
|
isBalanceSufficient,
|
2017-10-30 18:48:50 +01:00
|
|
|
isTokenBalanceSufficient,
|
2018-03-28 03:08:04 +02:00
|
|
|
getGasTotal,
|
2017-10-30 18:48:50 +01:00
|
|
|
} = require('./components/send/send-utils')
|
2017-10-17 22:13:20 +02:00
|
|
|
const { isValidAddress } = require('./util')
|
2018-04-02 11:59:49 +02:00
|
|
|
const { CONFIRM_TRANSACTION_ROUTE, DEFAULT_ROUTE } = require('./routes')
|
2018-05-03 19:51:15 +02:00
|
|
|
const Button = require('./components/button')
|
2017-10-17 19:22:23 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
SendTransactionScreen.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
module.exports = SendTransactionScreen
|
2017-10-05 17:44:45 +02:00
|
|
|
|
|
|
|
inherits(SendTransactionScreen, PersistentForm)
|
|
|
|
function SendTransactionScreen () {
|
|
|
|
PersistentForm.call(this)
|
|
|
|
|
|
|
|
this.state = {
|
2017-10-23 18:24:47 +02:00
|
|
|
fromDropdownOpen: false,
|
|
|
|
toDropdownOpen: false,
|
2017-10-17 22:13:20 +02:00
|
|
|
errors: {
|
|
|
|
to: null,
|
|
|
|
amount: null,
|
|
|
|
},
|
2018-03-20 10:17:45 +01:00
|
|
|
gasLoadingError: false,
|
2017-10-05 17:44:45 +02:00
|
|
|
}
|
2017-10-17 15:58:53 +02:00
|
|
|
|
|
|
|
this.handleToChange = this.handleToChange.bind(this)
|
|
|
|
this.handleAmountChange = this.handleAmountChange.bind(this)
|
2017-10-17 22:13:20 +02:00
|
|
|
this.validateAmount = this.validateAmount.bind(this)
|
2017-10-05 17:44:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 18:48:50 +01:00
|
|
|
const getParamsForGasEstimate = function (selectedAddress, symbol, data) {
|
|
|
|
const estimatedGasParams = {
|
|
|
|
from: selectedAddress,
|
|
|
|
gas: '746a528800',
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symbol) {
|
|
|
|
Object.assign(estimatedGasParams, { value: '0x0' })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
Object.assign(estimatedGasParams, { data })
|
|
|
|
}
|
|
|
|
|
|
|
|
return estimatedGasParams
|
|
|
|
}
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.updateSendTokenBalance = function (usersToken) {
|
|
|
|
if (!usersToken) return
|
|
|
|
|
|
|
|
const {
|
|
|
|
selectedToken = {},
|
|
|
|
updateSendTokenBalance,
|
|
|
|
} = this.props
|
|
|
|
const { decimals } = selectedToken || {}
|
|
|
|
const tokenBalance = calcTokenAmount(usersToken.balance.toString(), decimals)
|
|
|
|
|
|
|
|
updateSendTokenBalance(tokenBalance)
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
SendTransactionScreen.prototype.componentWillMount = function () {
|
2018-01-17 01:08:42 +01:00
|
|
|
this.updateGas()
|
|
|
|
}
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.updateGas = function () {
|
|
|
|
const {
|
|
|
|
selectedToken = {},
|
2017-10-13 22:19:22 +02:00
|
|
|
getGasPrice,
|
|
|
|
estimateGas,
|
|
|
|
selectedAddress,
|
|
|
|
data,
|
2017-10-17 19:22:23 +02:00
|
|
|
updateGasTotal,
|
2017-10-30 18:48:50 +01:00
|
|
|
from,
|
|
|
|
tokenContract,
|
2017-11-08 17:44:48 +01:00
|
|
|
editingTransactionId,
|
|
|
|
gasPrice,
|
|
|
|
gasLimit,
|
2017-10-13 22:19:22 +02:00
|
|
|
} = this.props
|
|
|
|
|
2018-01-17 01:08:42 +01:00
|
|
|
const { symbol } = selectedToken || {}
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-01-17 01:08:42 +01:00
|
|
|
const tokenBalancePromise = tokenContract
|
|
|
|
? tokenContract.balanceOf(from.address)
|
|
|
|
: Promise.resolve()
|
2018-01-18 05:47:01 +01:00
|
|
|
tokenBalancePromise
|
|
|
|
.then(usersToken => this.updateSendTokenBalance(usersToken))
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2017-11-08 17:44:48 +01:00
|
|
|
if (!editingTransactionId) {
|
2018-01-17 01:08:42 +01:00
|
|
|
const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data)
|
|
|
|
|
2017-11-08 17:44:48 +01:00
|
|
|
Promise
|
|
|
|
.all([
|
|
|
|
getGasPrice(),
|
|
|
|
estimateGas(estimateGasParams),
|
|
|
|
])
|
2018-01-18 05:47:01 +01:00
|
|
|
.then(([gasPrice, gas]) => {
|
2018-03-28 03:08:04 +02:00
|
|
|
const newGasTotal = getGasTotal(gas, gasPrice)
|
2017-11-08 17:44:48 +01:00
|
|
|
updateGasTotal(newGasTotal)
|
2018-03-20 10:17:45 +01:00
|
|
|
this.setState({ gasLoadingError: false })
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.setState({ gasLoadingError: true })
|
2017-10-17 19:22:23 +02:00
|
|
|
})
|
2017-11-08 17:44:48 +01:00
|
|
|
} else {
|
2018-03-28 03:08:04 +02:00
|
|
|
const newGasTotal = getGasTotal(gasLimit, gasPrice)
|
2017-11-08 17:44:48 +01:00
|
|
|
updateGasTotal(newGasTotal)
|
|
|
|
}
|
2017-10-13 22:19:22 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 18:48:50 +01:00
|
|
|
SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) {
|
|
|
|
const {
|
|
|
|
from: { balance },
|
|
|
|
gasTotal,
|
|
|
|
tokenBalance,
|
|
|
|
amount,
|
|
|
|
selectedToken,
|
2018-01-17 01:08:42 +01:00
|
|
|
network,
|
2017-10-30 18:48:50 +01:00
|
|
|
} = this.props
|
2018-01-17 01:08:42 +01:00
|
|
|
|
2017-10-30 18:48:50 +01:00
|
|
|
const {
|
|
|
|
from: { balance: prevBalance },
|
|
|
|
gasTotal: prevGasTotal,
|
|
|
|
tokenBalance: prevTokenBalance,
|
2018-01-17 01:08:42 +01:00
|
|
|
network: prevNetwork,
|
2017-10-30 18:48:50 +01:00
|
|
|
} = prevProps
|
|
|
|
|
2018-01-18 05:47:01 +01:00
|
|
|
const uninitialized = [prevBalance, prevGasTotal].every(n => n === null)
|
2017-10-30 18:48:50 +01:00
|
|
|
|
|
|
|
const balanceHasChanged = balance !== prevBalance
|
|
|
|
const gasTotalHasChange = gasTotal !== prevGasTotal
|
|
|
|
const tokenBalanceHasChanged = selectedToken && tokenBalance !== prevTokenBalance
|
|
|
|
const amountValidationChange = balanceHasChanged || gasTotalHasChange || tokenBalanceHasChanged
|
|
|
|
|
2018-01-18 05:47:01 +01:00
|
|
|
if (!uninitialized) {
|
2018-01-17 01:08:42 +01:00
|
|
|
if (amountValidationChange) {
|
|
|
|
this.validateAmount(amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (network !== prevNetwork && network !== 'loading') {
|
|
|
|
this.updateGas()
|
|
|
|
}
|
2017-10-30 18:48:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:07:00 +01:00
|
|
|
SendTransactionScreen.prototype.renderHeader = function () {
|
2018-03-28 22:21:53 +02:00
|
|
|
const { selectedToken, clearSend, history } = this.props
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-02-07 06:07:00 +01:00
|
|
|
return h('div.page-container__header', [
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
h('div.page-container__title', selectedToken ? this.context.t('sendTokens') : this.context.t('sendETH')),
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
h('div.page-container__subtitle', this.context.t('onlySendToEtherAddress')),
|
2017-10-17 15:58:53 +02:00
|
|
|
|
2018-02-13 07:39:15 +01:00
|
|
|
h('div.page-container__header-close', {
|
|
|
|
onClick: () => {
|
|
|
|
clearSend()
|
2018-04-02 11:59:49 +02:00
|
|
|
history.push(DEFAULT_ROUTE)
|
2018-02-13 07:39:15 +01:00
|
|
|
},
|
|
|
|
}),
|
2017-10-17 15:58:53 +02:00
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2017-11-02 13:15:59 +01:00
|
|
|
SendTransactionScreen.prototype.renderErrorMessage = function (errorType) {
|
2017-10-17 22:13:20 +02:00
|
|
|
const { errors } = this.props
|
2017-11-02 13:15:59 +01:00
|
|
|
const errorMessage = errors[errorType]
|
2017-10-18 21:23:35 +02:00
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
return errorMessage
|
2017-11-02 13:15:59 +01:00
|
|
|
? h('div.send-v2__error', [ errorMessage ])
|
2017-10-17 22:13:20 +02:00
|
|
|
: null
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:48:50 +01:00
|
|
|
SendTransactionScreen.prototype.handleFromChange = async function (newFrom) {
|
|
|
|
const {
|
|
|
|
updateSendFrom,
|
|
|
|
tokenContract,
|
|
|
|
} = this.props
|
|
|
|
|
|
|
|
if (tokenContract) {
|
|
|
|
const usersToken = await tokenContract.balanceOf(newFrom.address)
|
|
|
|
this.updateSendTokenBalance(usersToken)
|
|
|
|
}
|
|
|
|
updateSendFrom(newFrom)
|
|
|
|
}
|
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.renderFromRow = function () {
|
2017-10-13 22:19:22 +02:00
|
|
|
const {
|
2017-10-17 19:22:23 +02:00
|
|
|
from,
|
2017-10-17 17:46:36 +02:00
|
|
|
fromAccounts,
|
2017-10-13 22:19:22 +02:00
|
|
|
conversionRate,
|
|
|
|
} = this.props
|
|
|
|
|
2017-10-23 18:24:47 +02:00
|
|
|
const { fromDropdownOpen } = this.state
|
2017-10-17 15:58:53 +02:00
|
|
|
|
|
|
|
return h('div.send-v2__form-row', [
|
|
|
|
|
|
|
|
h('div.send-v2__form-label', 'From:'),
|
|
|
|
|
2017-10-19 19:53:56 +02:00
|
|
|
h('div.send-v2__form-field', [
|
|
|
|
h(FromDropdown, {
|
2017-10-23 18:24:47 +02:00
|
|
|
dropdownOpen: fromDropdownOpen,
|
2017-10-19 19:53:56 +02:00
|
|
|
accounts: fromAccounts,
|
|
|
|
selectedAccount: from,
|
2017-10-30 18:48:50 +01:00
|
|
|
onSelect: newFrom => this.handleFromChange(newFrom),
|
2017-10-23 18:24:47 +02:00
|
|
|
openDropdown: () => this.setState({ fromDropdownOpen: true }),
|
|
|
|
closeDropdown: () => this.setState({ fromDropdownOpen: false }),
|
2017-10-19 19:53:56 +02:00
|
|
|
conversionRate,
|
|
|
|
}),
|
|
|
|
]),
|
2017-10-17 15:58:53 +02:00
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2018-03-26 15:30:54 +02:00
|
|
|
SendTransactionScreen.prototype.handleToChange = function (to, nickname = '') {
|
2017-10-25 10:12:53 +02:00
|
|
|
const {
|
|
|
|
updateSendTo,
|
|
|
|
updateSendErrors,
|
|
|
|
} = this.props
|
2017-10-17 22:13:20 +02:00
|
|
|
let toError = null
|
|
|
|
|
|
|
|
if (!to) {
|
2018-03-29 17:00:44 +02:00
|
|
|
toError = this.context.t('required')
|
2017-10-17 22:13:20 +02:00
|
|
|
} else if (!isValidAddress(to)) {
|
2018-03-29 17:00:44 +02:00
|
|
|
toError = this.context.t('invalidAddressRecipient')
|
2017-10-17 22:13:20 +02:00
|
|
|
}
|
2017-10-17 15:58:53 +02:00
|
|
|
|
2018-03-26 15:30:54 +02:00
|
|
|
updateSendTo(to, nickname)
|
2017-10-17 22:13:20 +02:00
|
|
|
updateSendErrors({ to: toError })
|
2017-10-17 15:58:53 +02:00
|
|
|
}
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.renderToRow = function () {
|
2018-03-26 15:30:54 +02:00
|
|
|
const { toAccounts, errors, to, network } = this.props
|
2017-10-23 18:24:47 +02:00
|
|
|
|
|
|
|
const { toDropdownOpen } = this.state
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
return h('div.send-v2__form-row', [
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
h('div.send-v2__form-label', [
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
this.context.t('to'),
|
2017-10-17 22:13:20 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
this.renderErrorMessage(this.context.t('to')),
|
2017-10-17 22:13:20 +02:00
|
|
|
|
|
|
|
]),
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-10-19 19:53:56 +02:00
|
|
|
h('div.send-v2__form-field', [
|
2018-03-26 15:30:54 +02:00
|
|
|
h(EnsInput, {
|
|
|
|
name: 'address',
|
|
|
|
placeholder: 'Recipient Address',
|
|
|
|
network,
|
2017-10-19 19:53:56 +02:00
|
|
|
to,
|
2017-10-23 18:24:47 +02:00
|
|
|
accounts: Object.entries(toAccounts).map(([key, account]) => account),
|
|
|
|
dropdownOpen: toDropdownOpen,
|
|
|
|
openDropdown: () => this.setState({ toDropdownOpen: true }),
|
|
|
|
closeDropdown: () => this.setState({ toDropdownOpen: false }),
|
2017-10-19 19:53:56 +02:00
|
|
|
onChange: this.handleToChange,
|
|
|
|
inError: Boolean(errors.to),
|
|
|
|
}),
|
|
|
|
]),
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
])
|
|
|
|
}
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.handleAmountChange = function (value) {
|
|
|
|
const amount = value
|
2017-11-16 19:14:25 +01:00
|
|
|
const { updateSendAmount, setMaxModeTo } = this.props
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-11-16 19:14:25 +01:00
|
|
|
setMaxModeTo(false)
|
2017-11-08 00:36:26 +01:00
|
|
|
this.validateAmount(amount)
|
2017-10-17 19:22:23 +02:00
|
|
|
updateSendAmount(amount)
|
2017-10-17 15:58:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 18:43:12 +02:00
|
|
|
SendTransactionScreen.prototype.setAmountToMax = function () {
|
|
|
|
const {
|
|
|
|
from: { balance },
|
|
|
|
updateSendAmount,
|
|
|
|
updateSendErrors,
|
2017-10-30 19:07:30 +01:00
|
|
|
tokenBalance,
|
|
|
|
selectedToken,
|
2017-11-16 19:14:25 +01:00
|
|
|
gasTotal,
|
2017-10-26 18:43:12 +02:00
|
|
|
} = this.props
|
2017-10-30 19:07:30 +01:00
|
|
|
const { decimals } = selectedToken || {}
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
2017-10-26 18:43:12 +02:00
|
|
|
|
2017-10-30 19:07:30 +01:00
|
|
|
const maxAmount = selectedToken
|
|
|
|
? multiplyCurrencies(tokenBalance, multiplier, {toNumericBase: 'hex'})
|
|
|
|
: subtractCurrencies(
|
|
|
|
ethUtil.addHexPrefix(balance),
|
2017-11-16 19:14:25 +01:00
|
|
|
ethUtil.addHexPrefix(gasTotal),
|
2017-10-30 19:07:30 +01:00
|
|
|
{ toNumericBase: 'hex' }
|
|
|
|
)
|
2017-10-26 18:43:12 +02:00
|
|
|
|
|
|
|
updateSendErrors({ amount: null })
|
2017-11-16 19:14:25 +01:00
|
|
|
|
2017-10-26 18:43:12 +02:00
|
|
|
updateSendAmount(maxAmount)
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
SendTransactionScreen.prototype.validateAmount = function (value) {
|
|
|
|
const {
|
|
|
|
from: { balance },
|
|
|
|
updateSendErrors,
|
|
|
|
amountConversionRate,
|
|
|
|
conversionRate,
|
|
|
|
primaryCurrency,
|
2017-10-19 20:23:02 +02:00
|
|
|
selectedToken,
|
|
|
|
gasTotal,
|
2017-10-30 18:48:50 +01:00
|
|
|
tokenBalance,
|
2017-10-17 22:13:20 +02:00
|
|
|
} = this.props
|
2017-10-30 18:48:50 +01:00
|
|
|
const { decimals } = selectedToken || {}
|
2017-10-17 22:13:20 +02:00
|
|
|
const amount = value
|
|
|
|
|
|
|
|
let amountError = null
|
2018-01-18 05:47:01 +01:00
|
|
|
|
|
|
|
let sufficientBalance = true
|
|
|
|
|
|
|
|
if (gasTotal) {
|
|
|
|
sufficientBalance = isBalanceSufficient({
|
|
|
|
amount: selectedToken ? '0x0' : amount,
|
|
|
|
gasTotal,
|
|
|
|
balance,
|
|
|
|
primaryCurrency,
|
|
|
|
amountConversionRate,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
2017-10-19 20:23:02 +02:00
|
|
|
|
2018-03-06 04:53:29 +01:00
|
|
|
const verifyTokenBalance = selectedToken && tokenBalance !== null
|
2017-10-30 18:48:50 +01:00
|
|
|
let sufficientTokens
|
2018-03-06 04:53:29 +01:00
|
|
|
if (verifyTokenBalance) {
|
2017-10-30 18:48:50 +01:00
|
|
|
sufficientTokens = isTokenBalanceSufficient({
|
|
|
|
tokenBalance,
|
|
|
|
amount,
|
|
|
|
decimals,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
const amountLessThanZero = conversionGreaterThan(
|
|
|
|
{ value: 0, fromNumericBase: 'dec' },
|
|
|
|
{ value: amount, fromNumericBase: 'hex' },
|
|
|
|
)
|
|
|
|
|
2018-01-18 05:47:01 +01:00
|
|
|
if (conversionRate && !sufficientBalance) {
|
2018-03-29 17:00:44 +02:00
|
|
|
amountError = this.context.t('insufficientFunds')
|
2018-03-06 04:53:29 +01:00
|
|
|
} else if (verifyTokenBalance && !sufficientTokens) {
|
2018-03-29 17:00:44 +02:00
|
|
|
amountError = this.context.t('insufficientTokens')
|
2017-10-17 22:13:20 +02:00
|
|
|
} else if (amountLessThanZero) {
|
2018-03-29 17:00:44 +02:00
|
|
|
amountError = this.context.t('negativeETH')
|
2017-10-17 22:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
updateSendErrors({ amount: amountError })
|
|
|
|
}
|
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.renderAmountRow = function () {
|
|
|
|
const {
|
|
|
|
selectedToken,
|
|
|
|
primaryCurrency = 'ETH',
|
2017-10-19 04:09:26 +02:00
|
|
|
convertedCurrency,
|
2017-10-17 19:22:23 +02:00
|
|
|
amountConversionRate,
|
2017-10-17 22:13:20 +02:00
|
|
|
errors,
|
2017-10-30 18:48:50 +01:00
|
|
|
amount,
|
2017-11-16 19:14:25 +01:00
|
|
|
setMaxModeTo,
|
|
|
|
maxModeOn,
|
2018-03-06 03:42:06 +01:00
|
|
|
gasTotal,
|
2017-10-17 15:58:53 +02:00
|
|
|
} = this.props
|
2017-11-08 17:44:48 +01:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
return h('div.send-v2__form-row', [
|
2017-11-09 23:23:10 +01:00
|
|
|
|
2018-03-06 03:42:06 +01:00
|
|
|
h('div.send-v2__form-label', [
|
2017-10-17 22:13:20 +02:00
|
|
|
'Amount:',
|
|
|
|
this.renderErrorMessage('amount'),
|
2018-03-06 03:42:06 +01:00
|
|
|
!errors.amount && gasTotal && h('div.send-v2__amount-max', {
|
2017-10-26 18:43:12 +02:00
|
|
|
onClick: (event) => {
|
|
|
|
event.preventDefault()
|
2017-11-16 19:14:25 +01:00
|
|
|
setMaxModeTo(true)
|
2017-10-26 18:43:12 +02:00
|
|
|
this.setAmountToMax()
|
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, [ !maxModeOn ? this.context.t('max') : '' ]),
|
2017-10-17 22:13:20 +02:00
|
|
|
]),
|
2017-10-17 15:58:53 +02:00
|
|
|
|
2017-10-19 19:53:56 +02:00
|
|
|
h('div.send-v2__form-field', [
|
|
|
|
h(CurrencyDisplay, {
|
|
|
|
inError: Boolean(errors.amount),
|
|
|
|
primaryCurrency,
|
2017-10-19 04:09:26 +02:00
|
|
|
convertedCurrency,
|
2017-10-21 03:26:18 +02:00
|
|
|
selectedToken,
|
2017-10-30 23:04:21 +01:00
|
|
|
value: amount || '0x0',
|
2017-10-19 19:53:56 +02:00
|
|
|
conversionRate: amountConversionRate,
|
|
|
|
handleChange: this.handleAmountChange,
|
|
|
|
}),
|
|
|
|
]),
|
2017-10-17 15:58:53 +02:00
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.renderGasRow = function () {
|
|
|
|
const {
|
|
|
|
conversionRate,
|
2017-10-19 04:09:26 +02:00
|
|
|
convertedCurrency,
|
2017-10-17 15:58:53 +02:00
|
|
|
showCustomizeGasModal,
|
2018-01-18 05:47:01 +01:00
|
|
|
gasTotal,
|
2017-10-17 15:58:53 +02:00
|
|
|
} = this.props
|
2018-03-20 10:17:45 +01:00
|
|
|
const { gasLoadingError } = this.state
|
2017-10-05 17:44:45 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
return h('div.send-v2__form-row', [
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
h('div.send-v2__form-label', this.context.t('gasFee')),
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2017-10-19 19:53:56 +02:00
|
|
|
h('div.send-v2__form-field', [
|
|
|
|
|
|
|
|
h(GasFeeDisplay, {
|
|
|
|
gasTotal,
|
|
|
|
conversionRate,
|
2017-10-19 04:09:26 +02:00
|
|
|
convertedCurrency,
|
2017-10-19 19:53:56 +02:00
|
|
|
onClick: showCustomizeGasModal,
|
2018-03-20 10:17:45 +01:00
|
|
|
gasLoadingError,
|
2017-10-19 19:53:56 +02:00
|
|
|
}),
|
2017-10-21 03:26:18 +02:00
|
|
|
|
|
|
|
]),
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
])
|
|
|
|
}
|
2017-10-06 12:30:45 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.renderMemoRow = function () {
|
2017-10-30 18:48:50 +01:00
|
|
|
const { updateSendMemo, memo } = this.props
|
2017-10-06 12:30:45 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
return h('div.send-v2__form-row', [
|
2017-10-06 12:30:45 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
h('div.send-v2__form-label', 'Transaction Memo:'),
|
2017-10-06 12:30:45 +02:00
|
|
|
|
2017-10-19 19:53:56 +02:00
|
|
|
h('div.send-v2__form-field', [
|
|
|
|
h(MemoTextArea, {
|
|
|
|
memo,
|
|
|
|
onChange: (event) => updateSendMemo(event.target.value),
|
2017-10-21 03:26:18 +02:00
|
|
|
}),
|
2017-10-19 19:53:56 +02:00
|
|
|
]),
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
])
|
|
|
|
}
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.renderForm = function () {
|
2018-03-20 00:12:09 +01:00
|
|
|
return h('.page-container__content', {}, [
|
|
|
|
h('.send-v2__form', [
|
|
|
|
this.renderFromRow(),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-03-20 00:12:09 +01:00
|
|
|
this.renderToRow(),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-03-20 00:12:09 +01:00
|
|
|
this.renderAmountRow(),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-03-20 00:12:09 +01:00
|
|
|
this.renderGasRow(),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-03-20 00:12:09 +01:00
|
|
|
// this.renderMemoRow(),
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-03-20 00:12:09 +01:00
|
|
|
]),
|
2017-10-17 15:58:53 +02:00
|
|
|
])
|
|
|
|
}
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.renderFooter = function () {
|
2017-10-21 04:36:42 +02:00
|
|
|
const {
|
|
|
|
clearSend,
|
2018-01-18 05:47:01 +01:00
|
|
|
gasTotal,
|
2018-03-06 04:53:29 +01:00
|
|
|
tokenBalance,
|
|
|
|
selectedToken,
|
2017-10-25 07:43:49 +02:00
|
|
|
errors: { amount: amountError, to: toError },
|
2017-11-29 05:24:35 +01:00
|
|
|
history,
|
2017-10-21 04:36:42 +02:00
|
|
|
} = this.props
|
2017-10-25 07:43:49 +02:00
|
|
|
|
2018-04-28 01:15:36 +02:00
|
|
|
const missingTokenBalance = selectedToken && (tokenBalance === null || tokenBalance === undefined)
|
2017-10-30 23:04:21 +01:00
|
|
|
const noErrors = !amountError && toError === null
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-02-07 06:07:00 +01:00
|
|
|
return h('div.page-container__footer', [
|
2018-05-03 19:51:15 +02:00
|
|
|
h(Button, {
|
2018-05-29 23:33:29 +02:00
|
|
|
type: 'default',
|
2018-05-03 19:51:15 +02:00
|
|
|
large: true,
|
|
|
|
className: 'page-container__footer-button',
|
2017-10-21 04:05:22 +02:00
|
|
|
onClick: () => {
|
|
|
|
clearSend()
|
2018-04-02 11:59:49 +02:00
|
|
|
history.push(DEFAULT_ROUTE)
|
2017-10-21 04:05:22 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('cancel')),
|
2018-05-03 19:51:15 +02:00
|
|
|
h(Button, {
|
|
|
|
type: 'primary',
|
|
|
|
large: true,
|
|
|
|
className: 'page-container__footer-button',
|
2018-03-06 04:53:29 +01:00
|
|
|
disabled: !noErrors || !gasTotal || missingTokenBalance,
|
2017-10-25 07:43:49 +02:00
|
|
|
onClick: event => this.onSubmit(event),
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('next')),
|
2017-10-17 15:58:53 +02:00
|
|
|
])
|
|
|
|
}
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.render = function () {
|
|
|
|
return (
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-02-07 06:07:00 +01:00
|
|
|
h('div.page-container', [
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
this.renderHeader(),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
this.renderForm(),
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
this.renderFooter(),
|
2017-10-05 17:44:45 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
2017-10-12 19:29:03 +02:00
|
|
|
|
2018-03-26 15:30:54 +02:00
|
|
|
SendTransactionScreen.prototype.addToAddressBookIfNew = function (newAddress, nickname = '') {
|
2017-10-17 17:46:36 +02:00
|
|
|
const { toAccounts, addToAddressBook } = this.props
|
|
|
|
if (!toAccounts.find(({ address }) => newAddress === address)) {
|
|
|
|
// TODO: nickname, i.e. addToAddressBook(recipient, nickname)
|
2018-03-26 15:30:54 +02:00
|
|
|
addToAddressBook(newAddress, nickname)
|
2017-10-17 17:46:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
SendTransactionScreen.prototype.getEditedTx = function () {
|
|
|
|
const {
|
|
|
|
from: {address: from},
|
|
|
|
to,
|
|
|
|
amount,
|
|
|
|
gasLimit: gas,
|
|
|
|
gasPrice,
|
|
|
|
selectedToken,
|
|
|
|
editingTransactionId,
|
|
|
|
unapprovedTxs,
|
|
|
|
} = this.props
|
|
|
|
|
2017-12-20 23:41:18 +01:00
|
|
|
const editingTx = {
|
|
|
|
...unapprovedTxs[editingTransactionId],
|
|
|
|
txParams: {
|
|
|
|
from: ethUtil.addHexPrefix(from),
|
|
|
|
gas: ethUtil.addHexPrefix(gas),
|
|
|
|
gasPrice: ethUtil.addHexPrefix(gasPrice),
|
2017-12-22 04:03:01 +01:00
|
|
|
},
|
2017-12-20 18:17:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedToken) {
|
2017-12-20 23:36:58 +01:00
|
|
|
const data = TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(
|
2017-12-20 18:17:16 +01:00
|
|
|
ethAbi.rawEncode(['address', 'uint256'], [to, ethUtil.addHexPrefix(amount)]),
|
|
|
|
x => ('00' + x.toString(16)).slice(-2)
|
|
|
|
).join('')
|
|
|
|
|
|
|
|
Object.assign(editingTx.txParams, {
|
|
|
|
value: ethUtil.addHexPrefix('0'),
|
|
|
|
to: ethUtil.addHexPrefix(selectedToken.address),
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
} else {
|
2018-04-05 10:04:12 +02:00
|
|
|
const { data } = unapprovedTxs[editingTransactionId].txParams
|
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
Object.assign(editingTx.txParams, {
|
|
|
|
value: ethUtil.addHexPrefix(amount),
|
|
|
|
to: ethUtil.addHexPrefix(to),
|
2018-03-19 22:32:58 +01:00
|
|
|
data,
|
2017-12-20 18:17:16 +01:00
|
|
|
})
|
2018-04-05 10:04:12 +02:00
|
|
|
|
|
|
|
if (typeof editingTx.txParams.data === 'undefined') {
|
|
|
|
delete editingTx.txParams.data
|
|
|
|
}
|
2017-12-20 18:17:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return editingTx
|
|
|
|
}
|
|
|
|
|
2017-10-12 19:29:03 +02:00
|
|
|
SendTransactionScreen.prototype.onSubmit = function (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
const {
|
2017-10-17 19:22:23 +02:00
|
|
|
from: {address: from},
|
2018-03-27 08:10:34 +02:00
|
|
|
to: _to,
|
2017-10-12 19:29:03 +02:00
|
|
|
amount,
|
|
|
|
gasLimit: gas,
|
|
|
|
gasPrice,
|
|
|
|
signTokenTx,
|
|
|
|
signTx,
|
2017-12-20 18:17:16 +01:00
|
|
|
updateTx,
|
2017-10-12 19:29:03 +02:00
|
|
|
selectedToken,
|
2017-11-08 17:44:48 +01:00
|
|
|
editingTransactionId,
|
2018-03-26 15:30:54 +02:00
|
|
|
toNickname,
|
2017-10-25 07:43:49 +02:00
|
|
|
errors: { amount: amountError, to: toError },
|
2017-10-12 19:29:03 +02:00
|
|
|
} = this.props
|
|
|
|
|
2017-10-30 23:04:21 +01:00
|
|
|
const noErrors = !amountError && toError === null
|
2017-10-25 07:43:49 +02:00
|
|
|
|
|
|
|
if (!noErrors) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-27 08:10:34 +02:00
|
|
|
const to = ethUtil.addHexPrefix(_to)
|
|
|
|
|
2018-03-26 15:30:54 +02:00
|
|
|
this.addToAddressBookIfNew(to, toNickname)
|
2017-10-17 17:46:36 +02:00
|
|
|
|
2017-11-08 17:44:48 +01:00
|
|
|
if (editingTransactionId) {
|
2017-12-20 18:17:16 +01:00
|
|
|
const editedTx = this.getEditedTx()
|
|
|
|
updateTx(editedTx)
|
|
|
|
} else {
|
2017-11-08 17:44:48 +01:00
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
const txParams = {
|
|
|
|
from,
|
|
|
|
value: '0',
|
|
|
|
gas,
|
|
|
|
gasPrice,
|
|
|
|
}
|
2017-10-12 19:29:03 +02:00
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
if (!selectedToken) {
|
|
|
|
txParams.value = amount
|
|
|
|
txParams.to = to
|
|
|
|
}
|
2017-10-12 19:29:03 +02:00
|
|
|
|
2018-04-02 21:08:51 +02:00
|
|
|
Object.keys(txParams).forEach(key => {
|
|
|
|
txParams[key] = ethUtil.addHexPrefix(txParams[key])
|
|
|
|
})
|
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
selectedToken
|
|
|
|
? signTokenTx(selectedToken.address, to, amount, txParams)
|
|
|
|
: signTx(txParams)
|
|
|
|
}
|
2018-02-01 23:13:08 +01:00
|
|
|
|
|
|
|
this.props.history.push(CONFIRM_TRANSACTION_ROUTE)
|
2017-10-12 19:29:03 +02:00
|
|
|
}
|