Skip to content

Commit

Permalink
Fix gas estimation when sending to contracts (#6195)
Browse files Browse the repository at this point in the history
* Fix gas estimation when sending to contracts
* Fix calculating of balance sufficiency and tx params when sending token transaction
  • Loading branch information
danjm authored and whymarrh committed Feb 25, 2019
1 parent 65bfdee commit fdc7eb2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../../../../ducks/gas.duck'
import { getGasLoadingError, gasFeeIsInError, getGasButtonGroupShown } from './send-gas-row.selectors.js'
import { showModal, setGasPrice, setGasLimit, setGasTotal } from '../../../../actions'
import { getAdvancedInlineGasShown, getCurrentEthBalance } from '../../../../selectors'
import { getAdvancedInlineGasShown, getCurrentEthBalance, getSelectedToken } from '../../../../selectors'
import SendGasRow from './send-gas-row.component'

export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(SendGasRow)
Expand All @@ -42,7 +42,7 @@ function mapStateToProps (state) {
const balance = getCurrentEthBalance(state)

const insufficientBalance = !isBalanceSufficient({
amount: getSendAmount(state),
amount: getSelectedToken(state) ? '0x0' : getSendAmount(state),
gasTotal,
balance,
conversionRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ proxyquire('../send-gas-row.container.js', {
'../../../../selectors': {
getCurrentEthBalance: (s) => `mockCurrentEthBalance:${s}`,
getAdvancedInlineGasShown: (s) => `mockAdvancedInlineGasShown:${s}`,
getSelectedToken: () => false,
},
'../../send.selectors.js': {
getConversionRate: (s) => `mockConversionRate:${s}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const actionSpies = {
}
const utilsStubs = {
addressIsNew: sinon.stub().returns(true),
constructTxParams: sinon.stub().returns('mockConstructedTxParams'),
constructTxParams: sinon.stub().returns({
value: 'mockAmount',
}),
constructUpdatedTx: sinon.stub().returns('mockConstructedUpdatedTxParams'),
}

Expand Down Expand Up @@ -115,7 +117,7 @@ describe('send-footer container', () => {
)
assert.deepEqual(
actionSpies.signTokenTx.getCall(0).args,
[ '0xabc', 'mockTo', 'mockAmount', 'mockConstructedTxParams' ]
[ '0xabc', 'mockTo', 'mockAmount', { value: 'mockAmount' } ]
)
})

Expand Down Expand Up @@ -143,7 +145,7 @@ describe('send-footer container', () => {
)
assert.deepEqual(
actionSpies.signTx.getCall(0).args,
[ 'mockConstructedTxParams' ]
[ { value: 'mockAmount' } ]
)
})
})
Expand Down
6 changes: 5 additions & 1 deletion ui/app/components/send/send.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,14 @@ async function estimateGas ({
if (to) {
paramsForGasEstimate.to = to
}

if (!value || value === '0') {
paramsForGasEstimate.value = '0xff'
}
}

// if not, fall back to block gasLimit
paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit, 0.95, {
paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit || '0x5208', 0.95, {
multiplicandBase: 16,
multiplierBase: 10,
roundDown: '0',
Expand Down
3 changes: 2 additions & 1 deletion ui/app/components/send/tests/send-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ describe('send utils', () => {
from: 'mockAddress',
gas: '0x64x0.95',
to: '0xisContract',
value: '0xff',
}

beforeEach(() => {
Expand Down Expand Up @@ -373,7 +374,7 @@ describe('send utils', () => {
assert.equal(baseMockParams.estimateGasMethod.callCount, 1)
assert.deepEqual(
baseMockParams.estimateGasMethod.getCall(0).args[0],
{ gasPrice: undefined, value: undefined, data, from: baseExpectedCall.from, gas: baseExpectedCall.gas},
{ gasPrice: undefined, value: '0xff', data, from: baseExpectedCall.from, gas: baseExpectedCall.gas},
)
assert.equal(result, '0xabc16')
})
Expand Down
2 changes: 1 addition & 1 deletion ui/app/reducers/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function reduceMetamask (state, action) {
tokenBalance: '0x0',
from: '',
to: '',
amount: '0x0',
amount: '0',
memo: '',
errors: {},
maxModeOn: false,
Expand Down

0 comments on commit fdc7eb2

Please sign in to comment.