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

Handle and set gas estimation when max mode is clicked (#8176)

* Handle and set gas estimation when max mode is clicked
Add componentDidMount to SendAmountRow to check for maxMode boolean to update amount and gas.
This commit is contained in:
Thomas Huang 2020-03-11 13:13:48 -07:00 committed by GitHub
parent f0f5554846
commit e791882959
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 7 deletions

View File

@ -107,9 +107,11 @@ const mapStateToProps = (state, ownProps) => {
const isMainnet = getIsMainnet(state) const isMainnet = getIsMainnet(state)
const showFiat = Boolean(isMainnet || showFiatInTestnets) const showFiat = Boolean(isMainnet || showFiatInTestnets)
const newTotalEth = maxModeOn ? addHexWEIsToRenderableEth(balance, '0x0') : addHexWEIsToRenderableEth(value, customGasTotal) const isTokenSelected = Boolean(getSelectedToken(state))
const sendAmount = maxModeOn ? subtractHexWEIsFromRenderableEth(balance, customGasTotal) : addHexWEIsToRenderableEth(value, '0x0') const newTotalEth = maxModeOn && !isTokenSelected ? addHexWEIsToRenderableEth(balance, '0x0') : addHexWEIsToRenderableEth(value, customGasTotal)
const sendAmount = maxModeOn && !isTokenSelected ? subtractHexWEIsFromRenderableEth(balance, customGasTotal) : addHexWEIsToRenderableEth(value, '0x0')
const insufficientBalance = maxModeOn ? false : !isBalanceSufficient({ const insufficientBalance = maxModeOn ? false : !isBalanceSufficient({
amount: value, amount: value,

View File

@ -26,12 +26,26 @@ export default class SendAmountRow extends Component {
updateSendAmount: PropTypes.func, updateSendAmount: PropTypes.func,
updateSendAmountError: PropTypes.func, updateSendAmountError: PropTypes.func,
updateGas: PropTypes.func, updateGas: PropTypes.func,
maxModeOn: PropTypes.bool,
} }
static contextTypes = { static contextTypes = {
t: PropTypes.func, t: PropTypes.func,
} }
componentDidUpdate (prevProps) {
const { maxModeOn: prevMaxModeOn, gasTotal: prevGasTotal } = prevProps
const { maxModeOn, amount, gasTotal, selectedToken } = this.props
if (maxModeOn && selectedToken && !prevMaxModeOn) {
this.updateGas(amount)
}
if (prevGasTotal !== gasTotal) {
this.validateAmount(amount)
}
}
updateGas = debounce(this.updateGas.bind(this), 500) updateGas = debounce(this.updateGas.bind(this), 500)
validateAmount (amount) { validateAmount (amount) {
@ -86,17 +100,19 @@ export default class SendAmountRow extends Component {
} }
} }
handleChange = (newAmount) => {
this.validateAmount(newAmount)
this.updateGas(newAmount)
this.updateAmount(newAmount)
}
renderInput () { renderInput () {
const { amount, inError, selectedToken } = this.props const { amount, inError, selectedToken } = this.props
const Component = selectedToken ? UserPreferencedTokenInput : UserPreferencedCurrencyInput const Component = selectedToken ? UserPreferencedTokenInput : UserPreferencedCurrencyInput
return ( return (
<Component <Component
onChange={(newAmount) => { onChange={this.handleChange}
this.validateAmount(newAmount)
this.updateGas(newAmount)
this.updateAmount(newAmount)
}}
error={inError} error={inError}
value={amount} value={amount}
/> />

View File

@ -8,6 +8,7 @@ import {
getSendAmount, getSendAmount,
getSendFromBalance, getSendFromBalance,
getTokenBalance, getTokenBalance,
getSendMaxModeState,
} from '../../send.selectors' } from '../../send.selectors'
import { import {
sendAmountIsInError, sendAmountIsInError,
@ -35,6 +36,7 @@ function mapStateToProps (state) {
primaryCurrency: getPrimaryCurrency(state), primaryCurrency: getPrimaryCurrency(state),
selectedToken: getSelectedToken(state), selectedToken: getSelectedToken(state),
tokenBalance: getTokenBalance(state), tokenBalance: getTokenBalance(state),
maxModeOn: getSendMaxModeState(state),
} }
} }