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',
// 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)

View File

@ -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, {

View File

@ -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,
})
})
}
}