1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 07:23:21 +01:00
metamask-extension/ui/app/send-v2.js

232 lines
5.3 KiB
JavaScript
Raw Normal View History

const { inherits } = require('util')
const PropTypes = require('prop-types')
const PersistentForm = require('../lib/persistent-form')
const h = require('react-hyperscript')
const {
conversionGreaterThan,
} = require('./conversion-util')
const {
calcTokenAmount,
} = require('./token-util')
const {
isBalanceSufficient,
isTokenBalanceSufficient,
getGasTotal,
} = require('./components/send/send-utils')
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
SendTransactionScreen.contextTypes = {
t: PropTypes.func,
}
module.exports = SendTransactionScreen
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,
},
gasLoadingError: false,
}
2017-10-17 22:13:20 +02:00
this.validateAmount = this.validateAmount.bind(this)
}
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)
}
SendTransactionScreen.prototype.componentWillMount = function () {
this.updateGas()
}
SendTransactionScreen.prototype.updateGas = function () {
const {
selectedToken = {},
getGasPrice,
estimateGas,
selectedAddress,
data,
setGasTotal,
from,
tokenContract,
2017-11-08 17:44:48 +01:00
editingTransactionId,
gasPrice,
gasLimit,
} = this.props
const { symbol } = selectedToken || {}
const tokenBalancePromise = tokenContract
? tokenContract.balanceOf(from.address)
: Promise.resolve()
tokenBalancePromise
.then(usersToken => this.updateSendTokenBalance(usersToken))
2017-11-08 17:44:48 +01:00
if (!editingTransactionId) {
const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data)
2017-11-08 17:44:48 +01:00
Promise
.all([
getGasPrice(),
estimateGas(estimateGasParams),
])
.then(([gasPrice, gas]) => {
const newGasTotal = getGasTotal(gas, gasPrice)
setGasTotal(newGasTotal)
this.setState({ gasLoadingError: false })
})
.catch(err => {
this.setState({ gasLoadingError: true })
})
2017-11-08 17:44:48 +01:00
} else {
const newGasTotal = getGasTotal(gasLimit, gasPrice)
setGasTotal(newGasTotal)
2017-11-08 17:44:48 +01:00
}
}
SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) {
const {
from: { balance },
gasTotal,
tokenBalance,
amount,
selectedToken,
network,
} = this.props
const {
from: { balance: prevBalance },
gasTotal: prevGasTotal,
tokenBalance: prevTokenBalance,
network: prevNetwork,
} = prevProps
const uninitialized = [prevBalance, prevGasTotal].every(n => n === null)
const balanceHasChanged = balance !== prevBalance
const gasTotalHasChange = gasTotal !== prevGasTotal
const tokenBalanceHasChanged = selectedToken && tokenBalance !== prevTokenBalance
const amountValidationChange = balanceHasChanged || gasTotalHasChange || tokenBalanceHasChanged
if (!uninitialized) {
if (amountValidationChange) {
this.validateAmount(amount)
}
if (network !== prevNetwork && network !== 'loading') {
this.updateGas()
}
}
}
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,
selectedToken,
gasTotal,
tokenBalance,
2017-10-17 22:13:20 +02:00
} = this.props
const { decimals } = selectedToken || {}
2017-10-17 22:13:20 +02:00
const amount = value
let amountError = null
let sufficientBalance = true
if (gasTotal) {
sufficientBalance = isBalanceSufficient({
amount: selectedToken ? '0x0' : amount,
gasTotal,
balance,
primaryCurrency,
amountConversionRate,
conversionRate,
})
}
const verifyTokenBalance = selectedToken && tokenBalance !== null
let sufficientTokens
if (verifyTokenBalance) {
sufficientTokens = isTokenBalanceSufficient({
tokenBalance,
amount,
decimals,
})
}
2017-10-17 22:13:20 +02:00
const amountLessThanZero = conversionGreaterThan(
{ value: 0, fromNumericBase: 'dec' },
{ value: amount, fromNumericBase: 'hex' },
)
if (conversionRate && !sufficientBalance) {
2018-04-27 12:41:18 +02:00
amountError = 'insufficientFunds'
} 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 })
}
SendTransactionScreen.prototype.render = function () {
2018-04-26 18:38:38 +02:00
const { history } = this.props
return (
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),
2018-04-26 18:38:38 +02:00
h(SendFooter, { history }),
])
)
}