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-10-19 20:23:02 +02:00
|
|
|
const {
|
|
|
|
conversionGreaterThan,
|
|
|
|
} = 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 19:22:23 +02:00
|
|
|
|
2018-04-07 00:29:51 +02:00
|
|
|
import PageContainer from './components/page-container/page-container.component'
|
|
|
|
import SendHeader from './components/send_/send-header/send-header.container'
|
2018-04-26 18:38:38 +02:00
|
|
|
import SendContent from './components/send_/send-content/send-content.component'
|
|
|
|
import SendFooter from './components/send_/send-footer/send-footer.container'
|
2018-04-07 00:29:51 +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
|
|
|
|
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,
|
2018-04-27 21:03:00 +02:00
|
|
|
setGasTotal,
|
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)
|
2018-04-27 21:03:00 +02:00
|
|
|
setGasTotal(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)
|
2018-04-27 21:03:00 +02:00
|
|
|
setGasTotal(newGasTotal)
|
2017-11-08 17:44:48 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 18:43:12 +02:00
|
|
|
|
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-04-27 12:41:18 +02:00
|
|
|
amountError = 'insufficientFunds'
|
2018-03-06 04:53:29 +01:00
|
|
|
} else if (verifyTokenBalance && !sufficientTokens) {
|
2018-04-27 12:41:18 +02:00
|
|
|
amountError = 'insufficientTokens'
|
2017-10-17 22:13:20 +02:00
|
|
|
} else if (amountLessThanZero) {
|
2018-04-27 12:41:18 +02:00
|
|
|
amountError = 'negativeETH'
|
2017-10-17 22:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
updateSendErrors({ amount: amountError })
|
|
|
|
}
|
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
SendTransactionScreen.prototype.render = function () {
|
2018-04-26 18:38:38 +02:00
|
|
|
const { history } = this.props
|
|
|
|
|
2017-10-17 15:58:53 +02:00
|
|
|
return (
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-04-07 00:29:51 +02:00
|
|
|
h(PageContainer, [
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-04-07 00:29:51 +02:00
|
|
|
h(SendHeader),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-04-26 18:38:38 +02:00
|
|
|
h(SendContent),
|
2017-10-06 06:34:00 +02:00
|
|
|
|
2018-04-26 18:38:38 +02:00
|
|
|
h(SendFooter, { history }),
|
2017-10-05 17:44:45 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|