1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Refactor to store estimated gas and price in local state, return promise from actions.

This commit is contained in:
Dan 2017-09-25 21:00:32 -02:30
parent 88c4226bf1
commit 79bcb88db3
3 changed files with 38 additions and 81 deletions

View File

@ -131,15 +131,7 @@ var actions = {
VIEW_PENDING_TX: 'VIEW_PENDING_TX', VIEW_PENDING_TX: 'VIEW_PENDING_TX',
// send screen // send screen
estimateGas, estimateGas,
updateGasEstimate,
UPDATE_GAS_ESTIMATE: 'UPDATE_GAS_ESTIMATE',
updateGasPrice,
UPDATE_GAS_PRICE: 'UPDATE_GAS_PRICE',
getGasPrice, 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,
@ -462,20 +454,30 @@ function signTx (txData) {
function estimateGas ({ to, amount }) { function estimateGas ({ to, amount }) {
return (dispatch) => { return (dispatch) => {
global.ethQuery.estimateGas({ to, amount }, (err, data) => { return new Promise((resolve, reject) => {
if (err) return dispatch(actions.displayWarning(err.message)) global.ethQuery.estimateGas({ to, amount }, (err, data) => {
dispatch(actions.hideWarning()) if (err) {
dispatch(actions.updateGasEstimate(data)) dispatch(actions.displayWarning(err.message))
return reject(err)
}
dispatch(actions.hideWarning())
return resolve(data)
})
}) })
} }
} }
function getGasPrice () { function getGasPrice () {
return (dispatch) => { return (dispatch) => {
global.ethQuery.gasPrice((err, data) => { return new Promise((resolve, reject) => {
if (err) return dispatch(actions.displayWarning(err.message)) global.ethQuery.gasPrice((err, data) => {
dispatch(actions.hideWarning()) if (err) {
dispatch(actions.updateGasPrice(data)) dispatch(actions.displayWarning(err.message))
return reject(err)
}
dispatch(actions.hideWarning())
return resolve(data)
})
}) })
} }
} }
@ -537,32 +539,6 @@ 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)

View File

@ -19,8 +19,6 @@ 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) {
@ -76,26 +74,6 @@ 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, {

View File

@ -18,8 +18,6 @@ const {
signTx, signTx,
estimateGas, estimateGas,
getGasPrice, 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')
@ -52,8 +50,6 @@ 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],
@ -73,16 +69,15 @@ function SendTransactionScreen () {
newTx: { newTx: {
from: '', from: '',
to: '', to: '',
amount: 0,
amountToSend: '0x0', amountToSend: '0x0',
gasPrice: null, gasPrice: null,
gas: null, gas: null,
amount: '0x0', amount: '0x0',
gasPrice: null,
gas: null,
txData: null, txData: null,
memo: '', memo: '',
}, },
blockGasPrice: null,
estimatedGas: null,
activeCurrency: 'USD', activeCurrency: 'USD',
tooltipIsOpen: false, tooltipIsOpen: false,
errors: {}, errors: {},
@ -108,11 +103,6 @@ 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];
@ -316,11 +306,16 @@ 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,
blockGasPrice,
estimatedGas,
} = this.state
const { gas, gasPrice } = newTx const { gas, gasPrice } = newTx
return ( return (
@ -386,8 +381,16 @@ SendTransactionScreen.prototype.estimateGasAndPrice = function () {
const { errors, sendAmount, newTx } = this.state const { errors, sendAmount, newTx } = this.state
if (!errors.to && !errors.amount && newTx.amount > 0) { if (!errors.to && !errors.amount && newTx.amount > 0) {
this.props.dispatch(getGasPrice()) Promise.all([
this.props.dispatch(estimateGas({ to: newTx.to, amount: sendAmount })) this.props.dispatch(getGasPrice()),
this.props.dispatch(estimateGas({ to: newTx.to, amount: sendAmount })),
])
.then(([blockGasPrice, estimatedGas]) => {
this.setState({
blockGasPrice,
estimatedGas,
})
})
} }
} }