1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

Readjust gas fees when switching networks on the send screen

This commit is contained in:
Alexander Tseung 2018-01-16 16:08:42 -08:00 committed by Chi Kei Chan
parent 668aab11d1
commit ccb80594be
2 changed files with 44 additions and 22 deletions

View File

@ -51,6 +51,7 @@ function mapStateToProps (state) {
amountConversionRate: selectedToken ? tokenToFiatRate : conversionRate,
tokenContract: getSelectedTokenContract(state),
unapprovedTxs: state.metamask.unapprovedTxs,
network: state.metamask.network,
}
}

View File

@ -85,6 +85,20 @@ SendTransactionScreen.prototype.componentWillMount = function () {
const {
updateTokenExchangeRate,
selectedToken = {},
} = this.props
const { symbol } = selectedToken || {}
if (symbol) {
updateTokenExchangeRate(symbol)
}
this.updateGas()
}
SendTransactionScreen.prototype.updateGas = function () {
const {
selectedToken = {},
getGasPrice,
estimateGas,
selectedAddress,
@ -96,17 +110,16 @@ SendTransactionScreen.prototype.componentWillMount = function () {
gasPrice,
gasLimit,
} = this.props
const { symbol } = selectedToken || {}
if (symbol) {
updateTokenExchangeRate(symbol)
}
const tokenBalancePromise = tokenContract
? tokenContract.balanceOf(from.address)
: Promise.resolve()
const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data)
const tokenBalancePromise = tokenContract && tokenContract.balanceOf(from.address)
let newGasTotal
if (!editingTransactionId) {
const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data)
Promise
.all([
getGasPrice(),
@ -114,27 +127,26 @@ SendTransactionScreen.prototype.componentWillMount = function () {
tokenBalancePromise,
])
.then(([gasPrice, gas, usersToken]) => {
const newGasTotal = multiplyCurrencies(gas, gasPrice, {
toNumericBase: 'hex',
multiplicandBase: 16,
multiplierBase: 16,
})
const newGasTotal = this.getGasTotal(gas, gasPrice)
updateGasTotal(newGasTotal)
this.updateSendTokenBalance(usersToken)
})
} else {
newGasTotal = multiplyCurrencies(gasLimit, gasPrice, {
toNumericBase: 'hex',
multiplicandBase: 16,
multiplierBase: 16,
})
const newGasTotal = this.getGasTotal(gasLimit, gasPrice)
updateGasTotal(newGasTotal)
tokenBalancePromise && tokenBalancePromise.then(
usersToken => this.updateSendTokenBalance(usersToken))
tokenBalancePromise
.then(usersToken => this.updateSendTokenBalance(usersToken))
}
}
SendTransactionScreen.prototype.getGasTotal = function (gasLimit, gasPrice) {
return multiplyCurrencies(gasLimit, gasPrice, {
toNumericBase: 'hex',
multiplicandBase: 16,
multiplierBase: 16,
})
}
SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) {
const {
from: { balance },
@ -142,11 +154,14 @@ SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) {
tokenBalance,
amount,
selectedToken,
network,
} = this.props
const {
from: { balance: prevBalance },
gasTotal: prevGasTotal,
tokenBalance: prevTokenBalance,
network: prevNetwork,
} = prevProps
const notFirstRender = [prevBalance, prevGasTotal].every(n => n !== null)
@ -156,8 +171,14 @@ SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) {
const tokenBalanceHasChanged = selectedToken && tokenBalance !== prevTokenBalance
const amountValidationChange = balanceHasChanged || gasTotalHasChange || tokenBalanceHasChanged
if (notFirstRender && amountValidationChange) {
this.validateAmount(amount)
if (notFirstRender) {
if (amountValidationChange) {
this.validateAmount(amount)
}
if (network !== prevNetwork && network !== 'loading') {
this.updateGas()
}
}
}