Skip to content

Commit

Permalink
Fix gas estimate for tokens (MetaMask#7753)
Browse files Browse the repository at this point in the history
* Make gas estimate update on debounced token amount change, not just on blur after change

* Updated tests

* Ensure `updateGas` is bound early

Co-authored-by: Mark Stacey <[email protected]>
  • Loading branch information
2 people authored and yqrashawn committed Jan 15, 2020
1 parent 4ed8984 commit bda1f82
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 35 deletions.
1 change: 1 addition & 0 deletions test/e2e/metamask-ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ describe('MetaMask', function () {
driver,
By.xpath(`//button[contains(text(), 'Next')]`)
)
await driver.wait(until.elementIsEnabled(nextScreen))
await nextScreen.click()
await delay(regularDelayMs)
})
Expand Down
2 changes: 1 addition & 1 deletion ui/app/components/ui/token-input/token-input.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class TokenInput extends PureComponent {
}

handleBlur = () => {
this.props.onBlur(this.state.hexValue)
this.props.onBlur && this.props.onBlur(this.state.hexValue)
}

renderConversionComponent () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import debounce from 'lodash.debounce'
import SendRowWrapper from '../send-row-wrapper'
import AmountMaxButton from './amount-max-button'
import UserPreferencedCurrencyInput from '../../../../components/app/user-preferenced-currency-input'
Expand Down Expand Up @@ -30,6 +31,8 @@ export default class SendAmountRow extends Component {
t: PropTypes.func,
}

updateGas = debounce(this.updateGas.bind(this), 500)

validateAmount (amount) {
const {
amountConversionRate,
Expand Down Expand Up @@ -90,8 +93,8 @@ export default class SendAmountRow extends Component {

return (
<Component
onChange={newAmount => this.validateAmount(newAmount)}
onBlur={newAmount => {
onChange={newAmount => {
this.validateAmount(newAmount)
this.updateGas(newAmount)
this.updateAmount(newAmount)
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import SendRowWrapper from '../../send-row-wrapper/send-row-wrapper.component'
import AmountMaxButton from '../amount-max-button/amount-max-button.container'
import UserPreferencedTokenInput from '../../../../../components/app/user-preferenced-token-input'

import timeout from '../../../../../../lib/test-timeout'

const propsMethodSpies = {
setMaxModeTo: sinon.spy(),
updateSendAmount: sinon.spy(),
Expand Down Expand Up @@ -154,26 +156,25 @@ describe('SendAmountRow Component', function () {
)
})

it('should render the UserPreferencedTokenInput with the correct props', () => {
const { onBlur, onChange, error, value } = wrapper
it('should render the UserPreferencedTokenInput with the correct props', async () => {
const { onChange, error, value } = wrapper
.find(SendRowWrapper)
.childAt(1)
.props()
assert.equal(error, false)
assert.equal(value, 'mockAmount')
assert.equal(SendAmountRow.prototype.updateGas.callCount, 0)
assert.equal(SendAmountRow.prototype.updateAmount.callCount, 0)
onBlur('mockNewAmount')
assert.equal(SendAmountRow.prototype.updateGas.callCount, 1)
assert.equal(SendAmountRow.prototype.validateAmount.callCount, 0)
onChange('mockNewAmount')
await timeout(501)
assert.deepEqual(SendAmountRow.prototype.updateGas.getCall(0).args, [
'mockNewAmount',
])
assert.equal(SendAmountRow.prototype.updateAmount.callCount, 1)
assert.deepEqual(SendAmountRow.prototype.updateAmount.getCall(0).args, [
'mockNewAmount',
])
assert.equal(SendAmountRow.prototype.validateAmount.callCount, 0)
onChange('mockNewAmount')
assert.equal(SendAmountRow.prototype.validateAmount.callCount, 1)
assert.deepEqual(SendAmountRow.prototype.validateAmount.getCall(0).args, [
'mockNewAmount',
Expand Down
5 changes: 4 additions & 1 deletion ui/app/pages/send/send-footer/send-footer.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class SendFooter extends Component {
update: PropTypes.func,
sendErrors: PropTypes.object,
gasEstimateType: PropTypes.string,
gasIsLoading: PropTypes.bool,
}

static contextTypes = {
Expand Down Expand Up @@ -109,6 +110,7 @@ export default class SendFooter extends Component {
gasTotal,
to,
gasLimit,
gasIsLoading,
} = this.props
const missingTokenBalance = selectedToken && !tokenBalance
const gasLimitTooLow = gasLimit < 5208 // 5208 is hex value of 21000, minimum gas limit
Expand All @@ -117,7 +119,8 @@ export default class SendFooter extends Component {
!gasTotal ||
missingTokenBalance ||
!(data || to) ||
gasLimitTooLow
gasLimitTooLow ||
gasIsLoading
return shouldBeDisabled
}

Expand Down
2 changes: 2 additions & 0 deletions ui/app/pages/send/send-footer/send-footer.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getUnapprovedTxs,
getSendErrors,
} from '../send.selectors'
import { getGasIsLoading } from '../../../selectors/selectors'
import { isSendFormInError } from './send-footer.selectors'
import {
addressIsNew,
Expand Down Expand Up @@ -61,6 +62,7 @@ function mapStateToProps (state) {
unapprovedTxs: getUnapprovedTxs(state),
sendErrors: getSendErrors(state),
gasEstimateType,
gasIsLoading: getGasIsLoading(state),
}
}

Expand Down
13 changes: 13 additions & 0 deletions ui/app/pages/send/send-footer/tests/send-footer-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,42 @@ describe('SendFooter Component', function () {
'should return true if inError is truthy': {
inError: true,
expectedResult: true,
gasIsLoading: false,
},
'should return true if gasTotal is falsy': {
inError: false,
gasTotal: false,
expectedResult: true,
gasIsLoading: false,
},
'should return true if to is truthy': {
to: '0xsomevalidAddress',
inError: false,
gasTotal: false,
expectedResult: true,
gasIsLoading: false,
},
'should return true if selectedToken is truthy and tokenBalance is falsy': {
selectedToken: true,
tokenBalance: null,
expectedResult: true,
gasIsLoading: false,
},
'should return true if gasIsLoading is truthy but all other params are falsy': {
inError: false,
gasTotal: null,
selectedToken: null,
tokenBalance: 0,
expectedResult: true,
gasIsLoading: true,
},
'should return false if inError is false and all other params are truthy': {
inError: false,
gasTotal: '0x123',
selectedToken: true,
tokenBalance: 123,
expectedResult: false,
gasIsLoading: false,
},
}
Object.entries(config).map(([description, obj]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'assert'
import proxyquire from 'proxyquire'
import sinon from 'sinon'

let mapStateToProps
let mapDispatchToProps

const actionSpies = {
Expand All @@ -22,8 +21,7 @@ const utilsStubs = {

proxyquire('../send-footer.container.js', {
'react-redux': {
connect: (ms, md) => {
mapStateToProps = ms
connect: (_, md) => {
mapDispatchToProps = md
return () => ({})
},
Expand Down Expand Up @@ -56,28 +54,6 @@ proxyquire('../send-footer.container.js', {
})

describe('send-footer container', () => {
describe('mapStateToProps()', () => {
it('should map the correct properties to props', () => {
assert.deepEqual(mapStateToProps('mockState'), {
amount: 'mockAmount:mockState',
data: 'mockHexData:mockState',
selectedToken: 'mockSelectedToken:mockState',
editingTransactionId: 'mockEditingTransactionId:mockState',
from: 'mockFromObject:mockState',
gasLimit: 'mockGasLimit:mockState',
gasPrice: 'mockGasPrice:mockState',
gasTotal: 'mockGasTotal:mockState',
inError: 'mockInError:mockState',
to: 'mockTo:mockState',
toAccounts: 'mockToAccounts:mockState',
tokenBalance: 'mockTokenBalance:mockState',
unapprovedTxs: 'mockUnapprovedTxs:mockState',
sendErrors: 'mockSendErrors:mockState',
gasEstimateType: 'mockGasEstimateType:mockState',
})
})
})

describe('mapDispatchToProps()', () => {
let dispatchSpy
let mapDispatchToPropsObject
Expand Down

0 comments on commit bda1f82

Please sign in to comment.