From 79bcb88db3946260c832402d97e0c800cdeba5a9 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 25 Sep 2017 21:00:32 -0230 Subject: [PATCH] Refactor to store estimated gas and price in local state, return promise from actions. --- ui/app/actions.js | 60 +++++++++++-------------------------- ui/app/reducers/metamask.js | 22 -------------- ui/app/send.js | 37 ++++++++++++----------- 3 files changed, 38 insertions(+), 81 deletions(-) diff --git a/ui/app/actions.js b/ui/app/actions.js index a43809fc0..a0dbbbf11 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -131,15 +131,7 @@ var actions = { 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 confirmSeedWords: confirmSeedWords, showAccountDetail: showAccountDetail, @@ -462,20 +454,30 @@ 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)) + return new Promise((resolve, reject) => { + global.ethQuery.estimateGas({ to, amount }, (err, data) => { + if (err) { + dispatch(actions.displayWarning(err.message)) + return reject(err) + } + dispatch(actions.hideWarning()) + return resolve(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)) + return new Promise((resolve, reject) => { + global.ethQuery.gasPrice((err, data) => { + if (err) { + 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) { log.debug(`background.cancelMessage`) background.cancelMessage(msgData.id) diff --git a/ui/app/reducers/metamask.js b/ui/app/reducers/metamask.js index e78f51f3a..cdc98d05e 100644 --- a/ui/app/reducers/metamask.js +++ b/ui/app/reducers/metamask.js @@ -19,8 +19,6 @@ function reduceMetamask (state, action) { addressBook: [], selectedTokenAddress: null, tokenExchangeRates: {}, - estimatedGas: null, - blockGasPrice: null, }, state.metamask) 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: var stringId = String(action.id) newState = extend(metamaskState, { diff --git a/ui/app/send.js b/ui/app/send.js index 4ce7fc475..033692910 100644 --- a/ui/app/send.js +++ b/ui/app/send.js @@ -18,8 +18,6 @@ const { signTx, estimateGas, getGasPrice, - clearGasEstimate, - clearGasPrice, } = require('./actions') const { stripHexPrefix, addHexPrefix } = require('ethereumjs-util') const { isHex, numericBalance, isValidAddress, allNull } = require('./util') @@ -52,8 +50,6 @@ function mapStateToProps (state) { addressBook, conversionRate, blockGasLimit, - blockGasPrice, - estimatedGas, warning, selectedIdentity, error: warning && warning.split('.')[0], @@ -73,16 +69,15 @@ function SendTransactionScreen () { newTx: { from: '', to: '', - amount: 0, amountToSend: '0x0', gasPrice: null, gas: null, amount: '0x0', - gasPrice: null, - gas: null, txData: null, memo: '', }, + blockGasPrice: null, + estimatedGas: null, activeCurrency: 'USD', tooltipIsOpen: false, errors: {}, @@ -108,11 +103,6 @@ function SendTransactionScreen () { this.renderErrorMessage = this.renderErrorMessage.bind(this) } -SendTransactionScreen.prototype.componentWillMount = function() { - this.props.dispatch(clearGasEstimate()) - this.props.dispatch(clearGasPrice()) -} - SendTransactionScreen.prototype.renderErrorMessage = function(errorType, warning) { const { errors } = this.state const errorMessage = errors[errorType]; @@ -316,11 +306,16 @@ SendTransactionScreen.prototype.render = function () { identities, addressBook, conversionRate, - estimatedGas, - blockGasPrice, } = props - const { blockGasLimit, newTx, activeCurrency, isValid } = this.state + const { + blockGasLimit, + newTx, + activeCurrency, + isValid, + blockGasPrice, + estimatedGas, + } = this.state const { gas, gasPrice } = newTx return ( @@ -386,8 +381,16 @@ 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 })) + Promise.all([ + this.props.dispatch(getGasPrice()), + this.props.dispatch(estimateGas({ to: newTx.to, amount: sendAmount })), + ]) + .then(([blockGasPrice, estimatedGas]) => { + this.setState({ + blockGasPrice, + estimatedGas, + }) + }) } }