mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Promisify gas estimation (#8387)
The `estimateGasMethod` function passed to `estimateGas` is now an async function. It is now invoked using `async/await` rather than a Promise constructor. This was done as part of a broader effort to use Promises rather than callbacks when interacting with the background.
This commit is contained in:
parent
0d6dc380b4
commit
4d76f5b7ca
@ -256,24 +256,22 @@ async function estimateGas ({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
// run tx
|
// run tx
|
||||||
return new Promise((resolve, reject) => {
|
try {
|
||||||
return estimateGasMethod(paramsForGasEstimate, (err, estimatedGas) => {
|
const estimatedGas = await estimateGasMethod(paramsForGasEstimate)
|
||||||
if (err) {
|
const estimateWithBuffer = addGasBuffer(estimatedGas.toString(16), blockGasLimit, 1.5)
|
||||||
const simulationFailed = (
|
return ethUtil.addHexPrefix(estimateWithBuffer)
|
||||||
err.message.includes('Transaction execution error.') ||
|
} catch (error) {
|
||||||
err.message.includes('gas required exceeds allowance or always failing transaction')
|
const simulationFailed = (
|
||||||
)
|
error.message.includes('Transaction execution error.') ||
|
||||||
if (simulationFailed) {
|
error.message.includes('gas required exceeds allowance or always failing transaction')
|
||||||
const estimateWithBuffer = addGasBuffer(paramsForGasEstimate.gas, blockGasLimit, 1.5)
|
)
|
||||||
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
|
if (simulationFailed) {
|
||||||
} else {
|
const estimateWithBuffer = addGasBuffer(paramsForGasEstimate.gas, blockGasLimit, 1.5)
|
||||||
return reject(err)
|
return ethUtil.addHexPrefix(estimateWithBuffer)
|
||||||
}
|
} else {
|
||||||
}
|
throw error
|
||||||
const estimateWithBuffer = addGasBuffer(estimatedGas.toString(16), blockGasLimit, 1.5)
|
}
|
||||||
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
|
}
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function addGasBuffer (initialGasLimitHex, blockGasLimitHex, bufferMultiplier = 1.5) {
|
function addGasBuffer (initialGasLimitHex, blockGasLimitHex, bufferMultiplier = 1.5) {
|
||||||
|
@ -301,12 +301,11 @@ describe('send utils', function () {
|
|||||||
selectedAddress: 'mockAddress',
|
selectedAddress: 'mockAddress',
|
||||||
to: '0xisContract',
|
to: '0xisContract',
|
||||||
estimateGasMethod: sinon.stub().callsFake(
|
estimateGasMethod: sinon.stub().callsFake(
|
||||||
({ to }, cb) => {
|
({ to }) => {
|
||||||
const err = typeof to === 'string' && to.match(/willFailBecauseOf:/)
|
if (typeof to === 'string' && to.match(/willFailBecauseOf:/)) {
|
||||||
? new Error(to.match(/:(.+)$/)[1])
|
throw new Error(to.match(/:(.+)$/)[1])
|
||||||
: null
|
}
|
||||||
const result = { toString: (n) => `0xabc${n}` }
|
return { toString: (n) => `0xabc${n}` }
|
||||||
return cb(err, result)
|
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
@ -653,7 +653,7 @@ export function updateGasData ({
|
|||||||
return (dispatch) => {
|
return (dispatch) => {
|
||||||
dispatch(gasLoadingStarted())
|
dispatch(gasLoadingStarted())
|
||||||
return estimateGas({
|
return estimateGas({
|
||||||
estimateGasMethod: background.estimateGas,
|
estimateGasMethod: promisifiedBackground.estimateGas,
|
||||||
blockGasLimit,
|
blockGasLimit,
|
||||||
selectedAddress,
|
selectedAddress,
|
||||||
selectedToken,
|
selectedToken,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user