From 4d76f5b7cadcfa78c21b208eeed59aaf3a04a97a Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 23 Apr 2020 15:01:38 -0300 Subject: [PATCH] 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. --- ui/app/pages/send/send.utils.js | 34 ++++++++++------------ ui/app/pages/send/tests/send-utils.test.js | 11 ++++--- ui/app/store/actions.js | 2 +- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/ui/app/pages/send/send.utils.js b/ui/app/pages/send/send.utils.js index c0dbf65dd..01a924621 100644 --- a/ui/app/pages/send/send.utils.js +++ b/ui/app/pages/send/send.utils.js @@ -256,24 +256,22 @@ async function estimateGas ({ })) // run tx - return new Promise((resolve, reject) => { - return estimateGasMethod(paramsForGasEstimate, (err, estimatedGas) => { - if (err) { - const simulationFailed = ( - err.message.includes('Transaction execution error.') || - err.message.includes('gas required exceeds allowance or always failing transaction') - ) - if (simulationFailed) { - const estimateWithBuffer = addGasBuffer(paramsForGasEstimate.gas, blockGasLimit, 1.5) - return resolve(ethUtil.addHexPrefix(estimateWithBuffer)) - } else { - return reject(err) - } - } - const estimateWithBuffer = addGasBuffer(estimatedGas.toString(16), blockGasLimit, 1.5) - return resolve(ethUtil.addHexPrefix(estimateWithBuffer)) - }) - }) + try { + const estimatedGas = await estimateGasMethod(paramsForGasEstimate) + const estimateWithBuffer = addGasBuffer(estimatedGas.toString(16), blockGasLimit, 1.5) + return ethUtil.addHexPrefix(estimateWithBuffer) + } catch (error) { + const simulationFailed = ( + error.message.includes('Transaction execution error.') || + error.message.includes('gas required exceeds allowance or always failing transaction') + ) + if (simulationFailed) { + const estimateWithBuffer = addGasBuffer(paramsForGasEstimate.gas, blockGasLimit, 1.5) + return ethUtil.addHexPrefix(estimateWithBuffer) + } else { + throw error + } + } } function addGasBuffer (initialGasLimitHex, blockGasLimitHex, bufferMultiplier = 1.5) { diff --git a/ui/app/pages/send/tests/send-utils.test.js b/ui/app/pages/send/tests/send-utils.test.js index d66de02de..77f8f0443 100644 --- a/ui/app/pages/send/tests/send-utils.test.js +++ b/ui/app/pages/send/tests/send-utils.test.js @@ -301,12 +301,11 @@ describe('send utils', function () { selectedAddress: 'mockAddress', to: '0xisContract', estimateGasMethod: sinon.stub().callsFake( - ({ to }, cb) => { - const err = typeof to === 'string' && to.match(/willFailBecauseOf:/) - ? new Error(to.match(/:(.+)$/)[1]) - : null - const result = { toString: (n) => `0xabc${n}` } - return cb(err, result) + ({ to }) => { + if (typeof to === 'string' && to.match(/willFailBecauseOf:/)) { + throw new Error(to.match(/:(.+)$/)[1]) + } + return { toString: (n) => `0xabc${n}` } } ), } diff --git a/ui/app/store/actions.js b/ui/app/store/actions.js index f0e72b032..692469ca4 100644 --- a/ui/app/store/actions.js +++ b/ui/app/store/actions.js @@ -653,7 +653,7 @@ export function updateGasData ({ return (dispatch) => { dispatch(gasLoadingStarted()) return estimateGas({ - estimateGasMethod: background.estimateGas, + estimateGasMethod: promisifiedBackground.estimateGas, blockGasLimit, selectedAddress, selectedToken,