Estimate gasPrice and gasLimit in send screen.

feature/default_network_editable
Dan 7 years ago
parent a1d87b821b
commit 88c4226bf1
  1. 57
      ui/app/actions.js
  2. 22
      ui/app/reducers/metamask.js
  3. 54
      ui/app/send.js

@ -129,6 +129,17 @@ var actions = {
cancelAllTx: cancelAllTx, cancelAllTx: cancelAllTx,
viewPendingTx: viewPendingTx, viewPendingTx: viewPendingTx,
VIEW_PENDING_TX: 'VIEW_PENDING_TX', VIEW_PENDING_TX: 'VIEW_PENDING_TX',
// send screen
estimateGas,
updateGasEstimate,
UPDATE_GAS_ESTIMATE: 'UPDATE_GAS_ESTIMATE',
updateGasPrice,
UPDATE_GAS_PRICE: 'UPDATE_GAS_PRICE',
getGasPrice,
CLEAR_GAS_ESTIMATE: 'CLEAR_GAS_ESTIMATE',
CLEAR_GAS_PRICE: 'CLEAR_GAS_PRICE',
clearGasEstimate,
clearGasPrice,
// app messages // app messages
confirmSeedWords: confirmSeedWords, confirmSeedWords: confirmSeedWords,
showAccountDetail: showAccountDetail, showAccountDetail: showAccountDetail,
@ -449,6 +460,26 @@ function signTx (txData) {
} }
} }
function estimateGas ({ to, amount }) {
return (dispatch) => {
global.ethQuery.estimateGas({ to, amount }, (err, data) => {
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.hideWarning())
dispatch(actions.updateGasEstimate(data))
})
}
}
function getGasPrice () {
return (dispatch) => {
global.ethQuery.gasPrice((err, data) => {
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.hideWarning())
dispatch(actions.updateGasPrice(data))
})
}
}
function sendTx (txData) { function sendTx (txData) {
log.info(`actions - sendTx: ${JSON.stringify(txData.txParams)}`) log.info(`actions - sendTx: ${JSON.stringify(txData.txParams)}`)
return (dispatch) => { return (dispatch) => {
@ -506,6 +537,32 @@ function txError (err) {
} }
} }
function updateGasEstimate (gas) {
return {
type: actions.UPDATE_GAS_ESTIMATE,
value: gas,
}
}
function clearGasEstimate () {
return {
type: actions.CLEAR_GAS_ESTIMATE,
}
}
function updateGasPrice (gasPrice) {
return {
type: actions.UPDATE_GAS_PRICE,
value: gasPrice,
}
}
function clearGasPrice () {
return {
type: actions.CLEAR_GAS_PRICE,
}
}
function cancelMsg (msgData) { function cancelMsg (msgData) {
log.debug(`background.cancelMessage`) log.debug(`background.cancelMessage`)
background.cancelMessage(msgData.id) background.cancelMessage(msgData.id)

@ -19,6 +19,8 @@ function reduceMetamask (state, action) {
addressBook: [], addressBook: [],
selectedTokenAddress: null, selectedTokenAddress: null,
tokenExchangeRates: {}, tokenExchangeRates: {},
estimatedGas: null,
blockGasPrice: null,
}, state.metamask) }, state.metamask)
switch (action.type) { switch (action.type) {
@ -74,6 +76,26 @@ function reduceMetamask (state, action) {
}, },
}) })
case actions.UPDATE_GAS_ESTIMATE:
return extend(metamaskState, {
estimatedGas: action.value,
})
case actions.UPDATE_GAS_PRICE:
return extend(metamaskState, {
blockGasPrice: action.value,
})
case actions.CLEAR_GAS_ESTIMATE:
return extend(metamaskState, {
estimatedGas: null,
})
case actions.CLEAR_GAS_PRICE:
return extend(metamaskState, {
blockGasPrice: null,
})
case actions.COMPLETED_TX: case actions.COMPLETED_TX:
var stringId = String(action.id) var stringId = String(action.id)
newState = extend(metamaskState, { newState = extend(metamaskState, {

@ -16,6 +16,10 @@ const {
hideWarning, hideWarning,
addToAddressBook, addToAddressBook,
signTx, signTx,
estimateGas,
getGasPrice,
clearGasEstimate,
clearGasPrice,
} = require('./actions') } = require('./actions')
const { stripHexPrefix, addHexPrefix } = require('ethereumjs-util') const { stripHexPrefix, addHexPrefix } = require('ethereumjs-util')
const { isHex, numericBalance, isValidAddress, allNull } = require('./util') const { isHex, numericBalance, isValidAddress, allNull } = require('./util')
@ -33,6 +37,8 @@ function mapStateToProps (state) {
addressBook, addressBook,
conversionRate, conversionRate,
currentBlockGasLimit: blockGasLimit, currentBlockGasLimit: blockGasLimit,
estimatedGas,
blockGasPrice,
} = state.metamask } = state.metamask
const { warning } = state.appState const { warning } = state.appState
const selectedIdentity = getSelectedIdentity(state) const selectedIdentity = getSelectedIdentity(state)
@ -46,6 +52,8 @@ function mapStateToProps (state) {
addressBook, addressBook,
conversionRate, conversionRate,
blockGasLimit, blockGasLimit,
blockGasPrice,
estimatedGas,
warning, warning,
selectedIdentity, selectedIdentity,
error: warning && warning.split('.')[0], error: warning && warning.split('.')[0],
@ -67,8 +75,11 @@ function SendTransactionScreen () {
to: '', to: '',
amount: 0, amount: 0,
amountToSend: '0x0', amountToSend: '0x0',
gasPrice: '0x5d21dba00', gasPrice: null,
gas: '0x7b0d', gas: null,
amount: '0x0',
gasPrice: null,
gas: null,
txData: null, txData: null,
memo: '', memo: '',
}, },
@ -87,6 +98,7 @@ 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)
@ -96,6 +108,11 @@ function SendTransactionScreen () {
this.renderErrorMessage = this.renderErrorMessage.bind(this) this.renderErrorMessage = this.renderErrorMessage.bind(this)
} }
SendTransactionScreen.prototype.componentWillMount = function() {
this.props.dispatch(clearGasEstimate())
this.props.dispatch(clearGasPrice())
}
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];
@ -159,7 +176,10 @@ SendTransactionScreen.prototype.renderToInput = function (to, identities, addres
}, },
}) })
}, },
onBlur: () => this.setErrorsFor('to'), onBlur: () => {
this.setErrorsFor('to')
this.estimateGasAndPrice()
},
onFocus: () => this.clearErrorsFor('to'), onFocus: () => this.clearErrorsFor('to'),
}), }),
@ -212,7 +232,10 @@ SendTransactionScreen.prototype.renderAmountInput = function (activeCurrency) {
), ),
}) })
}, },
onBlur: () => this.setErrorsFor('amount'), onBlur: () => {
this.setErrorsFor('amount')
this.estimateGasAndPrice()
},
onFocus: () => this.clearErrorsFor('amount'), onFocus: () => this.clearErrorsFor('amount'),
}), }),
@ -293,6 +316,8 @@ SendTransactionScreen.prototype.render = function () {
identities, identities,
addressBook, addressBook,
conversionRate, conversionRate,
estimatedGas,
blockGasPrice,
} = props } = props
const { blockGasLimit, newTx, activeCurrency, isValid } = this.state const { blockGasLimit, newTx, activeCurrency, isValid } = this.state
@ -316,7 +341,13 @@ SendTransactionScreen.prototype.render = function () {
this.renderAmountInput(activeCurrency), this.renderAmountInput(activeCurrency),
this.renderGasInput(gasPrice, gas, activeCurrency, conversionRate, blockGasLimit), this.renderGasInput(
gasPrice || blockGasPrice || '0x0',
gas || estimatedGas || '0x0',
activeCurrency,
conversionRate,
blockGasLimit
),
this.renderMemoInput(), this.renderMemoInput(),
@ -351,6 +382,15 @@ 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) {
this.props.dispatch(getGasPrice())
this.props.dispatch(estimateGas({ to: newTx.to, amount: sendAmount }))
}
}
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))
@ -471,7 +511,7 @@ SendTransactionScreen.prototype.clearErrorsFor = function (field) {
SendTransactionScreen.prototype.onSubmit = function (event) { SendTransactionScreen.prototype.onSubmit = function (event) {
event.preventDefault() event.preventDefault()
const { warning, balance, amountToSend } = this.props const { warning, balance } = this.props
const state = this.state || {} const state = this.state || {}
const recipient = state.newTx.to const recipient = state.newTx.to
@ -489,7 +529,7 @@ SendTransactionScreen.prototype.onSubmit = function (event) {
from: this.state.newTx.from, from: this.state.newTx.from,
to: this.state.newTx.to, to: this.state.newTx.to,
value: amountToSend, value: this.state.newTx.amountToSend,
gas: this.state.newTx.gas, gas: this.state.newTx.gas,
gasPrice: this.state.newTx.gasPrice, gasPrice: this.state.newTx.gasPrice,

Loading…
Cancel
Save