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

Gas estimation uses block gas limit as fallback if query.estimateGas returns an expected error.

This commit is contained in:
Dan 2018-05-25 11:07:16 -02:30
parent 0f20fce9b7
commit 5a842e440f
2 changed files with 39 additions and 5 deletions

View File

@ -193,14 +193,21 @@ async function estimateGas ({ selectedAddress, selectedToken, data, blockGasLimi
roundDown: '0', roundDown: '0',
toNumericBase: 'hex', toNumericBase: 'hex',
})) }))
// run tx // run tx
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
estimateGasMethod(paramsForGasEstimate, (err, estimatedGas) => { return estimateGasMethod(paramsForGasEstimate, (err, estimatedGas) => {
if (err) { if (err) {
reject(err) const simulationFailed = (
err.message.includes('Transaction execution error.') ||
err.message.includes('gas required exceeds allowance or always failing transaction')
)
if (simulationFailed) {
return resolve(paramsForGasEstimate.gas)
} else {
return reject(err)
}
} }
resolve(estimatedGas.toString(16)) return resolve(estimatedGas.toString(16))
}) })
}) })
} }

View File

@ -241,7 +241,10 @@ describe('send utils', () => {
selectedAddress: 'mockAddress', selectedAddress: 'mockAddress',
to: '0xisContract', to: '0xisContract',
estimateGasMethod: sinon.stub().callsFake( estimateGasMethod: sinon.stub().callsFake(
(data, cb) => cb(null, { toString: (n) => `mockToString:${n}` }) (data, cb) => cb(
data.to.match(/willFailBecauseOf:/) ? { message: data.to.match(/\:(.+)$/)[1] } : null,
{ toString: (n) => `mockToString:${n}` }
)
), ),
} }
const baseExpectedCall = { const baseExpectedCall = {
@ -298,6 +301,30 @@ describe('send utils', () => {
const result = await estimateGas(Object.assign({}, baseMockParams, { to: '0x123' })) const result = await estimateGas(Object.assign({}, baseMockParams, { to: '0x123' }))
assert.equal(result, SIMPLE_GAS_COST) assert.equal(result, SIMPLE_GAS_COST)
}) })
it(`should return the adjusted blockGasLimit if it fails with a 'Transaction execution error.'`, async () => {
const result = await estimateGas(Object.assign({}, baseMockParams, {
to: 'isContract willFailBecauseOf:Transaction execution error.',
}))
assert.equal(result, '0x64x0.95')
})
it(`should return the adjusted blockGasLimit if it fails with a 'gas required exceeds allowance or always failing transaction.'`, async () => {
const result = await estimateGas(Object.assign({}, baseMockParams, {
to: 'isContract willFailBecauseOf:gas required exceeds allowance or always failing transaction.',
}))
assert.equal(result, '0x64x0.95')
})
it(`should reject other errors`, async () => {
try {
await estimateGas(Object.assign({}, baseMockParams, {
to: 'isContract willFailBecauseOf:some other error',
}))
} catch (err) {
assert.deepEqual(err, { message: 'some other error' })
}
})
}) })
describe('estimateGasPriceFromRecentBlocks', () => { describe('estimateGasPriceFromRecentBlocks', () => {