|
|
@ -2,6 +2,8 @@ const { inherits } = require('util') |
|
|
|
const PersistentForm = require('../lib/persistent-form') |
|
|
|
const PersistentForm = require('../lib/persistent-form') |
|
|
|
const h = require('react-hyperscript') |
|
|
|
const h = require('react-hyperscript') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
|
|
|
|
|
|
|
|
const Identicon = require('./components/identicon') |
|
|
|
const Identicon = require('./components/identicon') |
|
|
|
const FromDropdown = require('./components/send/from-dropdown') |
|
|
|
const FromDropdown = require('./components/send/from-dropdown') |
|
|
|
const ToAutoComplete = require('./components/send/to-autocomplete') |
|
|
|
const ToAutoComplete = require('./components/send/to-autocomplete') |
|
|
@ -9,15 +11,24 @@ const CurrencyDisplay = require('./components/send/currency-display') |
|
|
|
const MemoTextArea = require('./components/send/memo-textarea') |
|
|
|
const MemoTextArea = require('./components/send/memo-textarea') |
|
|
|
const GasFeeDisplay = require('./components/send/gas-fee-display-v2') |
|
|
|
const GasFeeDisplay = require('./components/send/gas-fee-display-v2') |
|
|
|
|
|
|
|
|
|
|
|
const { MIN_GAS_TOTAL } = require('./components/send/send-constants') |
|
|
|
const { |
|
|
|
|
|
|
|
MIN_GAS_TOTAL, |
|
|
|
|
|
|
|
MIN_GAS_PRICE_HEX, |
|
|
|
|
|
|
|
MIN_GAS_LIMIT_HEX, |
|
|
|
|
|
|
|
} = require('./components/send/send-constants') |
|
|
|
|
|
|
|
|
|
|
|
const { |
|
|
|
const { |
|
|
|
multiplyCurrencies, |
|
|
|
multiplyCurrencies, |
|
|
|
conversionGreaterThan, |
|
|
|
conversionGreaterThan, |
|
|
|
|
|
|
|
subtractCurrencies, |
|
|
|
} = require('./conversion-util') |
|
|
|
} = require('./conversion-util') |
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
calcTokenAmount, |
|
|
|
|
|
|
|
} = require('./token-util') |
|
|
|
const { |
|
|
|
const { |
|
|
|
isBalanceSufficient, |
|
|
|
isBalanceSufficient, |
|
|
|
} = require('./components/send/send-utils.js') |
|
|
|
isTokenBalanceSufficient, |
|
|
|
|
|
|
|
} = require('./components/send/send-utils') |
|
|
|
const { isValidAddress } = require('./util') |
|
|
|
const { isValidAddress } = require('./util') |
|
|
|
|
|
|
|
|
|
|
|
module.exports = SendTransactionScreen |
|
|
|
module.exports = SendTransactionScreen |
|
|
@ -40,6 +51,37 @@ function SendTransactionScreen () { |
|
|
|
this.validateAmount = this.validateAmount.bind(this) |
|
|
|
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 () { |
|
|
|
SendTransactionScreen.prototype.componentWillMount = function () { |
|
|
|
const { |
|
|
|
const { |
|
|
|
updateTokenExchangeRate, |
|
|
|
updateTokenExchangeRate, |
|
|
@ -49,32 +91,24 @@ SendTransactionScreen.prototype.componentWillMount = function () { |
|
|
|
selectedAddress, |
|
|
|
selectedAddress, |
|
|
|
data, |
|
|
|
data, |
|
|
|
updateGasTotal, |
|
|
|
updateGasTotal, |
|
|
|
|
|
|
|
from, |
|
|
|
|
|
|
|
tokenContract, |
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
const { symbol } = selectedToken || {} |
|
|
|
const { symbol } = selectedToken || {} |
|
|
|
|
|
|
|
|
|
|
|
const estimateGasParams = { |
|
|
|
|
|
|
|
from: selectedAddress, |
|
|
|
|
|
|
|
gas: '746a528800', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (symbol) { |
|
|
|
if (symbol) { |
|
|
|
updateTokenExchangeRate(symbol) |
|
|
|
updateTokenExchangeRate(symbol) |
|
|
|
Object.assign(estimateGasParams, { value: '0x0' }) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (data) { |
|
|
|
const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data) |
|
|
|
Object.assign(estimateGasParams, { data }) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Promise |
|
|
|
Promise |
|
|
|
.all([ |
|
|
|
.all([ |
|
|
|
getGasPrice(), |
|
|
|
getGasPrice(), |
|
|
|
estimateGas({ |
|
|
|
estimateGas(estimateGasParams), |
|
|
|
from: selectedAddress, |
|
|
|
tokenContract && tokenContract.balanceOf(from.address) |
|
|
|
gas: '746a528800', |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
]) |
|
|
|
]) |
|
|
|
.then(([gasPrice, gas]) => { |
|
|
|
.then(([gasPrice, gas, usersToken]) => { |
|
|
|
|
|
|
|
|
|
|
|
const newGasTotal = multiplyCurrencies(gas, gasPrice, { |
|
|
|
const newGasTotal = multiplyCurrencies(gas, gasPrice, { |
|
|
|
toNumericBase: 'hex', |
|
|
|
toNumericBase: 'hex', |
|
|
@ -82,9 +116,36 @@ SendTransactionScreen.prototype.componentWillMount = function () { |
|
|
|
multiplierBase: 16, |
|
|
|
multiplierBase: 16, |
|
|
|
}) |
|
|
|
}) |
|
|
|
updateGasTotal(newGasTotal) |
|
|
|
updateGasTotal(newGasTotal) |
|
|
|
|
|
|
|
this.updateSendTokenBalance(usersToken) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) { |
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
from: { balance }, |
|
|
|
|
|
|
|
gasTotal, |
|
|
|
|
|
|
|
tokenBalance, |
|
|
|
|
|
|
|
amount, |
|
|
|
|
|
|
|
selectedToken, |
|
|
|
|
|
|
|
} = this.props |
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
from: { balance: prevBalance }, |
|
|
|
|
|
|
|
gasTotal: prevGasTotal, |
|
|
|
|
|
|
|
tokenBalance: prevTokenBalance, |
|
|
|
|
|
|
|
} = prevProps |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const notFirstRender = [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 (notFirstRender && amountValidationChange) { |
|
|
|
|
|
|
|
this.validateAmount(amount) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.renderHeaderIcon = function () { |
|
|
|
SendTransactionScreen.prototype.renderHeaderIcon = function () { |
|
|
|
const { selectedToken } = this.props |
|
|
|
const { selectedToken } = this.props |
|
|
|
|
|
|
|
|
|
|
@ -144,12 +205,24 @@ SendTransactionScreen.prototype.renderErrorMessage = function (errorType) { |
|
|
|
: null |
|
|
|
: null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.renderFromRow = function () { |
|
|
|
SendTransactionScreen.prototype.renderFromRow = function () { |
|
|
|
const { |
|
|
|
const { |
|
|
|
from, |
|
|
|
from, |
|
|
|
fromAccounts, |
|
|
|
fromAccounts, |
|
|
|
conversionRate, |
|
|
|
conversionRate, |
|
|
|
updateSendFrom, |
|
|
|
|
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
|
|
|
|
|
|
|
|
const { fromDropdownOpen } = this.state |
|
|
|
const { fromDropdownOpen } = this.state |
|
|
@ -163,7 +236,7 @@ SendTransactionScreen.prototype.renderFromRow = function () { |
|
|
|
dropdownOpen: fromDropdownOpen, |
|
|
|
dropdownOpen: fromDropdownOpen, |
|
|
|
accounts: fromAccounts, |
|
|
|
accounts: fromAccounts, |
|
|
|
selectedAccount: from, |
|
|
|
selectedAccount: from, |
|
|
|
onSelect: updateSendFrom, |
|
|
|
onSelect: newFrom => this.handleFromChange(newFrom), |
|
|
|
openDropdown: () => this.setState({ fromDropdownOpen: true }), |
|
|
|
openDropdown: () => this.setState({ fromDropdownOpen: true }), |
|
|
|
closeDropdown: () => this.setState({ fromDropdownOpen: false }), |
|
|
|
closeDropdown: () => this.setState({ fromDropdownOpen: false }), |
|
|
|
conversionRate, |
|
|
|
conversionRate, |
|
|
@ -227,9 +300,40 @@ SendTransactionScreen.prototype.handleAmountChange = function (value) { |
|
|
|
const amount = value |
|
|
|
const amount = value |
|
|
|
const { updateSendAmount } = this.props |
|
|
|
const { updateSendAmount } = this.props |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.validateAmount(amount) |
|
|
|
updateSendAmount(amount) |
|
|
|
updateSendAmount(amount) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.setAmountToMax = function () { |
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
from: { balance }, |
|
|
|
|
|
|
|
gasTotal, |
|
|
|
|
|
|
|
updateSendAmount, |
|
|
|
|
|
|
|
updateSendErrors, |
|
|
|
|
|
|
|
updateGasPrice, |
|
|
|
|
|
|
|
updateGasLimit, |
|
|
|
|
|
|
|
updateGasTotal, |
|
|
|
|
|
|
|
tokenBalance, |
|
|
|
|
|
|
|
selectedToken, |
|
|
|
|
|
|
|
} = this.props |
|
|
|
|
|
|
|
const { decimals } = selectedToken || {} |
|
|
|
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const maxAmount = selectedToken |
|
|
|
|
|
|
|
? multiplyCurrencies(tokenBalance, multiplier, {toNumericBase: 'hex'}) |
|
|
|
|
|
|
|
: subtractCurrencies( |
|
|
|
|
|
|
|
ethUtil.addHexPrefix(balance), |
|
|
|
|
|
|
|
ethUtil.addHexPrefix(gasTotal), |
|
|
|
|
|
|
|
{ toNumericBase: 'hex' } |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateSendErrors({ amount: null }) |
|
|
|
|
|
|
|
updateGasPrice(MIN_GAS_PRICE_HEX) |
|
|
|
|
|
|
|
updateGasLimit(MIN_GAS_LIMIT_HEX) |
|
|
|
|
|
|
|
updateGasTotal(MIN_GAS_TOTAL) |
|
|
|
|
|
|
|
updateSendAmount(maxAmount) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.validateAmount = function (value) { |
|
|
|
SendTransactionScreen.prototype.validateAmount = function (value) { |
|
|
|
const { |
|
|
|
const { |
|
|
|
from: { balance }, |
|
|
|
from: { balance }, |
|
|
@ -239,21 +343,31 @@ SendTransactionScreen.prototype.validateAmount = function (value) { |
|
|
|
primaryCurrency, |
|
|
|
primaryCurrency, |
|
|
|
selectedToken, |
|
|
|
selectedToken, |
|
|
|
gasTotal, |
|
|
|
gasTotal, |
|
|
|
|
|
|
|
tokenBalance, |
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
|
|
|
|
const { decimals } = selectedToken || {} |
|
|
|
const amount = value |
|
|
|
const amount = value |
|
|
|
|
|
|
|
|
|
|
|
let amountError = null |
|
|
|
let amountError = null |
|
|
|
|
|
|
|
|
|
|
|
const sufficientBalance = isBalanceSufficient({ |
|
|
|
const sufficientBalance = isBalanceSufficient({ |
|
|
|
amount, |
|
|
|
amount: selectedToken ? '0x0' : amount, |
|
|
|
gasTotal, |
|
|
|
gasTotal, |
|
|
|
balance, |
|
|
|
balance, |
|
|
|
primaryCurrency, |
|
|
|
primaryCurrency, |
|
|
|
selectedToken, |
|
|
|
|
|
|
|
amountConversionRate, |
|
|
|
amountConversionRate, |
|
|
|
conversionRate, |
|
|
|
conversionRate, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let sufficientTokens |
|
|
|
|
|
|
|
if (selectedToken) { |
|
|
|
|
|
|
|
sufficientTokens = isTokenBalanceSufficient({ |
|
|
|
|
|
|
|
tokenBalance, |
|
|
|
|
|
|
|
amount, |
|
|
|
|
|
|
|
decimals, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const amountLessThanZero = conversionGreaterThan( |
|
|
|
const amountLessThanZero = conversionGreaterThan( |
|
|
|
{ value: 0, fromNumericBase: 'dec' }, |
|
|
|
{ value: 0, fromNumericBase: 'dec' }, |
|
|
|
{ value: amount, fromNumericBase: 'hex' }, |
|
|
|
{ value: amount, fromNumericBase: 'hex' }, |
|
|
@ -261,6 +375,8 @@ SendTransactionScreen.prototype.validateAmount = function (value) { |
|
|
|
|
|
|
|
|
|
|
|
if (!sufficientBalance) { |
|
|
|
if (!sufficientBalance) { |
|
|
|
amountError = 'Insufficient funds.' |
|
|
|
amountError = 'Insufficient funds.' |
|
|
|
|
|
|
|
} else if (selectedToken && !sufficientTokens) { |
|
|
|
|
|
|
|
amountError = 'Insufficient tokens.' |
|
|
|
} else if (amountLessThanZero) { |
|
|
|
} else if (amountLessThanZero) { |
|
|
|
amountError = 'Can not send negative amounts of ETH.' |
|
|
|
amountError = 'Can not send negative amounts of ETH.' |
|
|
|
} |
|
|
|
} |
|
|
@ -275,15 +391,19 @@ SendTransactionScreen.prototype.renderAmountRow = function () { |
|
|
|
convertedCurrency, |
|
|
|
convertedCurrency, |
|
|
|
amountConversionRate, |
|
|
|
amountConversionRate, |
|
|
|
errors, |
|
|
|
errors, |
|
|
|
|
|
|
|
amount, |
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
|
|
|
|
|
|
|
|
const { amount } = this.state |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return h('div.send-v2__form-row', [ |
|
|
|
return h('div.send-v2__form-row', [ |
|
|
|
|
|
|
|
|
|
|
|
h('div.send-v2__form-label', [ |
|
|
|
h('div.send-v2__form-label', [ |
|
|
|
'Amount:', |
|
|
|
'Amount:', |
|
|
|
this.renderErrorMessage('amount'), |
|
|
|
this.renderErrorMessage('amount'), |
|
|
|
|
|
|
|
!errors.amount && h('div.send-v2__amount-max', { |
|
|
|
|
|
|
|
onClick: (event) => { |
|
|
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
|
|
this.setAmountToMax() |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}, [ 'Max' ]), |
|
|
|
]), |
|
|
|
]), |
|
|
|
|
|
|
|
|
|
|
|
h('div.send-v2__form-field', [ |
|
|
|
h('div.send-v2__form-field', [ |
|
|
@ -292,10 +412,9 @@ SendTransactionScreen.prototype.renderAmountRow = function () { |
|
|
|
primaryCurrency, |
|
|
|
primaryCurrency, |
|
|
|
convertedCurrency, |
|
|
|
convertedCurrency, |
|
|
|
selectedToken, |
|
|
|
selectedToken, |
|
|
|
value: amount, |
|
|
|
value: amount || '0x0', |
|
|
|
conversionRate: amountConversionRate, |
|
|
|
conversionRate: amountConversionRate, |
|
|
|
handleChange: this.handleAmountChange, |
|
|
|
handleChange: this.handleAmountChange, |
|
|
|
validate: this.validateAmount, |
|
|
|
|
|
|
|
}), |
|
|
|
}), |
|
|
|
]), |
|
|
|
]), |
|
|
|
|
|
|
|
|
|
|
@ -335,8 +454,7 @@ SendTransactionScreen.prototype.renderGasRow = function () { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SendTransactionScreen.prototype.renderMemoRow = function () { |
|
|
|
SendTransactionScreen.prototype.renderMemoRow = function () { |
|
|
|
const { updateSendMemo } = this.props |
|
|
|
const { updateSendMemo, memo } = this.props |
|
|
|
const { memo } = this.state |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return h('div.send-v2__form-row', [ |
|
|
|
return h('div.send-v2__form-row', [ |
|
|
|
|
|
|
|
|
|
|
@ -383,7 +501,7 @@ SendTransactionScreen.prototype.renderFooter = function () { |
|
|
|
errors: { amount: amountError, to: toError }, |
|
|
|
errors: { amount: amountError, to: toError }, |
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
|
|
|
|
|
|
|
|
const noErrors = amountError === null && toError === null |
|
|
|
const noErrors = !amountError && toError === null |
|
|
|
const errorClass = noErrors ? '' : '__disabled' |
|
|
|
const errorClass = noErrors ? '' : '__disabled' |
|
|
|
|
|
|
|
|
|
|
|
return h('div.send-v2__footer', [ |
|
|
|
return h('div.send-v2__footer', [ |
|
|
@ -437,7 +555,7 @@ SendTransactionScreen.prototype.onSubmit = function (event) { |
|
|
|
errors: { amount: amountError, to: toError }, |
|
|
|
errors: { amount: amountError, to: toError }, |
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
|
|
|
|
|
|
|
|
const noErrors = amountError === null && toError === null |
|
|
|
const noErrors = !amountError && toError === null |
|
|
|
|
|
|
|
|
|
|
|
if (!noErrors) { |
|
|
|
if (!noErrors) { |
|
|
|
return |
|
|
|
return |
|
|
|