Gets gas and price estimates when send components mount.

feature/default_network_editable
Dan 7 years ago committed by Chi Kei Chan
parent 25c2865076
commit 541b69dda9
  1. 4
      ui/app/actions.js
  2. 32
      ui/app/components/send-token/index.js
  3. 33
      ui/app/send.js

@ -452,10 +452,10 @@ function signTx (txData) {
} }
} }
function estimateGas ({ to, amount }) { function estimateGas () {
return (dispatch) => { return (dispatch) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
global.ethQuery.estimateGas({ to, amount }, (err, data) => { global.ethQuery.estimateGas({}, (err, data) => {
if (err) { if (err) {
dispatch(actions.displayWarning(err.message)) dispatch(actions.displayWarning(err.message))
return reject(err) return reject(err)

@ -57,9 +57,8 @@ function mapDispatchToProps (dispatch) {
dispatch(actions.signTokenTx(tokenAddress, toAddress, amount, txData)) dispatch(actions.signTokenTx(tokenAddress, toAddress, amount, txData))
), ),
updateTokenExchangeRate: token => dispatch(actions.updateTokenExchangeRate(token)), updateTokenExchangeRate: token => dispatch(actions.updateTokenExchangeRate(token)),
estimateGas: ({ to, amount }) => dispatch(actions.estimateGas({ to, amount })), estimateGas: () => dispatch(actions.estimateGas()),
getGasPrice: () => dispatch(actions.getGasPrice()), getGasPrice: () => dispatch(actions.getGasPrice()),
} }
} }
@ -82,27 +81,22 @@ SendTokenScreen.prototype.componentWillMount = function () {
const { const {
updateTokenExchangeRate, updateTokenExchangeRate,
selectedToken: { symbol }, selectedToken: { symbol },
getGasPrice,
estimateGas,
} = this.props } = this.props
updateTokenExchangeRate(symbol) updateTokenExchangeRate(symbol)
}
SendTokenScreen.prototype.estimateGasAndPrice = function () { Promise.all([
const { selectedToken } = this.props getGasPrice(),
const { errors, amount, to } = this.state estimateGas(),
])
if (!errors.to && !errors.amount && amount > 0) { .then(([blockGasPrice, estimatedGas]) => {
Promise.all([ this.setState({
this.props.getGasPrice(), blockGasPrice,
this.props.estimateGas({ to, amount: this.getAmountToSend(amount, selectedToken) }), estimatedGas,
])
.then(([blockGasPrice, estimatedGas]) => {
this.setState({
blockGasPrice,
estimatedGas,
})
}) })
} })
} }
SendTokenScreen.prototype.validate = function () { SendTokenScreen.prototype.validate = function () {
@ -238,7 +232,6 @@ SendTokenScreen.prototype.renderToAddressInput = function () {
}), }),
onBlur: () => { onBlur: () => {
this.setErrorsFor('to') this.setErrorsFor('to')
this.estimateGasAndPrice()
}, },
onFocus: event => { onFocus: event => {
if (to) event.target.select() if (to) event.target.select()
@ -300,7 +293,6 @@ SendTokenScreen.prototype.renderAmountInput = function () {
}), }),
onBlur: () => { onBlur: () => {
this.setErrorsFor('amount') this.setErrorsFor('amount')
this.estimateGasAndPrice()
}, },
onFocus: () => this.clearErrorsFor('amount'), onFocus: () => this.clearErrorsFor('amount'),
}), }),

@ -93,7 +93,6 @@ function SendTransactionScreen () {
this.getAmountToSend = this.getAmountToSend.bind(this) this.getAmountToSend = this.getAmountToSend.bind(this)
this.setErrorsFor = this.setErrorsFor.bind(this) this.setErrorsFor = this.setErrorsFor.bind(this)
this.clearErrorsFor = this.clearErrorsFor.bind(this) this.clearErrorsFor = this.clearErrorsFor.bind(this)
this.estimateGasAndPrice = this.estimateGasAndPrice.bind(this)
this.renderFromInput = this.renderFromInput.bind(this) this.renderFromInput = this.renderFromInput.bind(this)
this.renderToInput = this.renderToInput.bind(this) this.renderToInput = this.renderToInput.bind(this)
@ -103,6 +102,19 @@ function SendTransactionScreen () {
this.renderErrorMessage = this.renderErrorMessage.bind(this) this.renderErrorMessage = this.renderErrorMessage.bind(this)
} }
SendTransactionScreen.prototype.componentWillMount = function () {
Promise.all([
this.props.dispatch(getGasPrice()),
this.props.dispatch(estimateGas()),
])
.then(([blockGasPrice, estimatedGas]) => {
this.setState({
blockGasPrice,
estimatedGas,
})
})
}
SendTransactionScreen.prototype.renderErrorMessage = function(errorType, warning) { SendTransactionScreen.prototype.renderErrorMessage = function(errorType, warning) {
const { errors } = this.state const { errors } = this.state
const errorMessage = errors[errorType]; const errorMessage = errors[errorType];
@ -171,7 +183,6 @@ SendTransactionScreen.prototype.renderToInput = function (to, identities, addres
}, },
onBlur: () => { onBlur: () => {
this.setErrorsFor('to') this.setErrorsFor('to')
this.estimateGasAndPrice()
}, },
onFocus: event => { onFocus: event => {
this.clearErrorsFor('to') this.clearErrorsFor('to')
@ -230,7 +241,6 @@ SendTransactionScreen.prototype.renderAmountInput = function (activeCurrency) {
}, },
onBlur: () => { onBlur: () => {
this.setErrorsFor('amount') this.setErrorsFor('amount')
this.estimateGasAndPrice()
}, },
onFocus: () => this.clearErrorsFor('amount'), onFocus: () => this.clearErrorsFor('amount'),
}), }),
@ -383,23 +393,6 @@ SendTransactionScreen.prototype.setActiveCurrency = function (newCurrency) {
this.setState({ activeCurrency: newCurrency }) this.setState({ activeCurrency: newCurrency })
} }
SendTransactionScreen.prototype.estimateGasAndPrice = function () {
const { errors, sendAmount, newTx } = this.state
if (!errors.to && !errors.amount && newTx.amount > 0) {
Promise.all([
this.props.dispatch(getGasPrice()),
this.props.dispatch(estimateGas({ to: newTx.to, amount: sendAmount })),
])
.then(([blockGasPrice, estimatedGas]) => {
this.setState({
blockGasPrice,
estimatedGas,
})
})
}
}
SendTransactionScreen.prototype.back = function () { SendTransactionScreen.prototype.back = function () {
var address = this.props.address var address = this.props.address
this.props.dispatch(backToAccountDetail(address)) this.props.dispatch(backToAccountDetail(address))

Loading…
Cancel
Save