mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Confirm send token detects if balance is sufficient for gas.
This commit is contained in:
parent
8e6ab7df05
commit
74ac3bb2a7
@ -16,7 +16,7 @@ const {
|
|||||||
const {
|
const {
|
||||||
getGasTotal,
|
getGasTotal,
|
||||||
isBalanceSufficient,
|
isBalanceSufficient,
|
||||||
} = require('../send/send-utils')
|
} = require('../send/send-utils')
|
||||||
const GasFeeDisplay = require('../send/gas-fee-display-v2')
|
const GasFeeDisplay = require('../send/gas-fee-display-v2')
|
||||||
const t = require('../../../i18n')
|
const t = require('../../../i18n')
|
||||||
const SenderToRecipient = require('../sender-to-recipient')
|
const SenderToRecipient = require('../sender-to-recipient')
|
||||||
@ -343,7 +343,7 @@ ConfirmSendEther.prototype.render = function () {
|
|||||||
className: classnames({
|
className: classnames({
|
||||||
'confirm-screen-section-column--with-error': errors['insufficientFunds'],
|
'confirm-screen-section-column--with-error': errors['insufficientFunds'],
|
||||||
'confirm-screen-section-column': !errors['insufficientFunds'],
|
'confirm-screen-section-column': !errors['insufficientFunds'],
|
||||||
})
|
}),
|
||||||
}, [
|
}, [
|
||||||
h('span.confirm-screen-label', [ t('total') + ' ' ]),
|
h('span.confirm-screen-label', [ t('total') + ' ' ]),
|
||||||
h('div.confirm-screen-total-box__subtitle', [ t('amountPlusGas') ]),
|
h('div.confirm-screen-total-box__subtitle', [ t('amountPlusGas') ]),
|
||||||
|
@ -17,9 +17,14 @@ const {
|
|||||||
multiplyCurrencies,
|
multiplyCurrencies,
|
||||||
addCurrencies,
|
addCurrencies,
|
||||||
} = require('../../conversion-util')
|
} = require('../../conversion-util')
|
||||||
|
const {
|
||||||
|
getGasTotal,
|
||||||
|
isBalanceSufficient,
|
||||||
|
} = require('../send/send-utils')
|
||||||
const {
|
const {
|
||||||
calcTokenAmount,
|
calcTokenAmount,
|
||||||
} = require('../../token-util')
|
} = require('../../token-util')
|
||||||
|
const classnames = require('classnames')
|
||||||
|
|
||||||
const { MIN_GAS_PRICE_HEX } = require('../send/send-constants')
|
const { MIN_GAS_PRICE_HEX } = require('../send/send-constants')
|
||||||
|
|
||||||
@ -41,9 +46,10 @@ function mapStateToProps (state, ownProps) {
|
|||||||
identities,
|
identities,
|
||||||
currentCurrency,
|
currentCurrency,
|
||||||
} = state.metamask
|
} = state.metamask
|
||||||
|
const accounts = state.metamask.accounts
|
||||||
const selectedAddress = getSelectedAddress(state)
|
const selectedAddress = getSelectedAddress(state)
|
||||||
const tokenExchangeRate = getTokenExchangeRate(state, symbol)
|
const tokenExchangeRate = getTokenExchangeRate(state, symbol)
|
||||||
|
const { balance } = accounts[selectedAddress]
|
||||||
return {
|
return {
|
||||||
conversionRate,
|
conversionRate,
|
||||||
identities,
|
identities,
|
||||||
@ -53,6 +59,7 @@ function mapStateToProps (state, ownProps) {
|
|||||||
currentCurrency: currentCurrency.toUpperCase(),
|
currentCurrency: currentCurrency.toUpperCase(),
|
||||||
send: state.metamask.send,
|
send: state.metamask.send,
|
||||||
tokenContract: getSelectedTokenContract(state),
|
tokenContract: getSelectedTokenContract(state),
|
||||||
|
balance,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,6 +131,7 @@ function mapDispatchToProps (dispatch, ownProps) {
|
|||||||
}))
|
}))
|
||||||
dispatch(actions.showModal({ name: 'CUSTOMIZE_GAS' }))
|
dispatch(actions.showModal({ name: 'CUSTOMIZE_GAS' }))
|
||||||
},
|
},
|
||||||
|
updateSendErrors: error => dispatch(actions.updateSendErrors(error)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,7 +309,7 @@ ConfirmSendToken.prototype.renderGasFee = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConfirmSendToken.prototype.renderTotalPlusGas = function () {
|
ConfirmSendToken.prototype.renderTotalPlusGas = function () {
|
||||||
const { token: { symbol }, currentCurrency } = this.props
|
const { token: { symbol }, currentCurrency, send: { errors } } = this.props
|
||||||
const { fiat: fiatAmount, token: tokenAmount } = this.getAmount()
|
const { fiat: fiatAmount, token: tokenAmount } = this.getAmount()
|
||||||
const { fiat: fiatGas, token: tokenGas } = this.getGasFee()
|
const { fiat: fiatGas, token: tokenGas } = this.getGasFee()
|
||||||
|
|
||||||
@ -321,7 +329,12 @@ ConfirmSendToken.prototype.renderTotalPlusGas = function () {
|
|||||||
)
|
)
|
||||||
: (
|
: (
|
||||||
h('section.flex-row.flex-center.confirm-screen-row.confirm-screen-total-box ', [
|
h('section.flex-row.flex-center.confirm-screen-row.confirm-screen-total-box ', [
|
||||||
h('div.confirm-screen-section-column', [
|
h('div', {
|
||||||
|
className: classnames({
|
||||||
|
'confirm-screen-section-column--with-error': errors['insufficientFunds'],
|
||||||
|
'confirm-screen-section-column': !errors['insufficientFunds'],
|
||||||
|
}),
|
||||||
|
}, [
|
||||||
h('span.confirm-screen-label', [ t('total') + ' ' ]),
|
h('span.confirm-screen-label', [ t('total') + ' ' ]),
|
||||||
h('div.confirm-screen-total-box__subtitle', [ t('amountPlusGas') ]),
|
h('div.confirm-screen-total-box__subtitle', [ t('amountPlusGas') ]),
|
||||||
]),
|
]),
|
||||||
@ -330,10 +343,20 @@ ConfirmSendToken.prototype.renderTotalPlusGas = function () {
|
|||||||
h('div.confirm-screen-row-info', `${tokenAmount} ${symbol}`),
|
h('div.confirm-screen-row-info', `${tokenAmount} ${symbol}`),
|
||||||
h('div.confirm-screen-row-detail', `+ ${fiatGas} ${currentCurrency} ${t('gas')}`),
|
h('div.confirm-screen-row-detail', `+ ${fiatGas} ${currentCurrency} ${t('gas')}`),
|
||||||
]),
|
]),
|
||||||
|
|
||||||
|
this.renderErrorMessage('insufficientFunds'),
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConfirmSendToken.prototype.renderErrorMessage = function (message) {
|
||||||
|
const { send: { errors } } = this.props
|
||||||
|
|
||||||
|
return errors[message]
|
||||||
|
? h('div.confirm-screen-error', [ errors[message] ])
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
|
||||||
ConfirmSendToken.prototype.render = function () {
|
ConfirmSendToken.prototype.render = function () {
|
||||||
const { editTransaction } = this.props
|
const { editTransaction } = this.props
|
||||||
const txMeta = this.gatherTxMeta()
|
const txMeta = this.gatherTxMeta()
|
||||||
@ -448,18 +471,44 @@ ConfirmSendToken.prototype.render = function () {
|
|||||||
|
|
||||||
ConfirmSendToken.prototype.onSubmit = function (event) {
|
ConfirmSendToken.prototype.onSubmit = function (event) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
const { updateSendErrors } = this.props
|
||||||
const txMeta = this.gatherTxMeta()
|
const txMeta = this.gatherTxMeta()
|
||||||
const valid = this.checkValidity()
|
const valid = this.checkValidity()
|
||||||
|
const balanceIsSufficient = this.isBalanceSufficient(txMeta)
|
||||||
this.setState({ valid, submitting: true })
|
this.setState({ valid, submitting: true })
|
||||||
|
|
||||||
if (valid && this.verifyGasParams()) {
|
if (valid && this.verifyGasParams() && balanceIsSufficient) {
|
||||||
this.props.sendTransaction(txMeta, event)
|
this.props.sendTransaction(txMeta, event)
|
||||||
|
} else if (!balanceIsSufficient) {
|
||||||
|
updateSendErrors({ insufficientFunds: t('insufficientFunds') })
|
||||||
} else {
|
} else {
|
||||||
this.props.dispatch(actions.displayWarning(t('invalidGasParams')))
|
updateSendErrors({ invalidGasParams: t('invalidGasParams') })
|
||||||
this.setState({ submitting: false })
|
this.setState({ submitting: false })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConfirmSendToken.prototype.isBalanceSufficient = function (txMeta) {
|
||||||
|
const {
|
||||||
|
balance,
|
||||||
|
conversionRate,
|
||||||
|
} = this.props
|
||||||
|
const {
|
||||||
|
txParams: {
|
||||||
|
gas,
|
||||||
|
gasPrice,
|
||||||
|
},
|
||||||
|
} = txMeta
|
||||||
|
const gasTotal = getGasTotal(gas, gasPrice)
|
||||||
|
|
||||||
|
return isBalanceSufficient({
|
||||||
|
amount: '0',
|
||||||
|
gasTotal,
|
||||||
|
balance,
|
||||||
|
conversionRate,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ConfirmSendToken.prototype.cancel = function (event, txMeta) {
|
ConfirmSendToken.prototype.cancel = function (event, txMeta) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
const { cancelTransaction } = this.props
|
const { cancelTransaction } = this.props
|
||||||
|
Loading…
Reference in New Issue
Block a user