From da6ba36993b09eb4e0ee8d32d3dbcd978675bd93 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 28 Mar 2023 11:33:54 +0200 Subject: [PATCH] pass gasPrice or maxFeePerGas to estimate gas --- libs/remix-lib/src/execution/txRunner.ts | 1 + libs/remix-lib/src/execution/txRunnerWeb3.ts | 31 +++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/libs/remix-lib/src/execution/txRunner.ts b/libs/remix-lib/src/execution/txRunner.ts index 2babf14a4b..544b9ef1a9 100644 --- a/libs/remix-lib/src/execution/txRunner.ts +++ b/libs/remix-lib/src/execution/txRunner.ts @@ -9,6 +9,7 @@ export type Transaction = { gasLimit: number, useCall: boolean, timestamp?: number + type: '0x1' | '0x2' } export class TxRunner { diff --git a/libs/remix-lib/src/execution/txRunnerWeb3.ts b/libs/remix-lib/src/execution/txRunnerWeb3.ts index c07e1c65e5..c1b3a1ae5e 100644 --- a/libs/remix-lib/src/execution/txRunnerWeb3.ts +++ b/libs/remix-lib/src/execution/txRunnerWeb3.ts @@ -90,7 +90,7 @@ export class TxRunnerWeb3 { } runInNode (from, to, data, value, gasLimit, useCall, timestamp, confirmCb, gasEstimationForceSend, promptCb, callback) { - const tx = { from: from, to: to, data: data, value: value } + const tx = { from: from, to: to, data: data, value: value, type: null, maxFeePerGas: null, gasPrice: null } if (!from) return callback('the value of "from" is not defined. Please make sure an account is selected.') if (useCall) { tx['gas'] = gasLimit @@ -102,15 +102,26 @@ export class TxRunnerWeb3 { }) }) } - this.getWeb3().eth.estimateGas(tx, (err, gasEstimation) => { - if (err && err.message.indexOf('Invalid JSON RPC response') !== -1) { - // // @todo(#378) this should be removed when https://github.com/WalletConnect/walletconnect-monorepo/issues/334 is fixed - callback(new Error('Gas estimation failed because of an unknown internal error. This may indicated that the transaction will fail.')) + this._api.detectNetwork((errNetWork, network) => { + if (errNetWork) { + console.log(errNetWork) + return } - this._api.detectNetwork((errNetWork, network) => { - if (errNetWork) { - console.log(errNetWork) - return + if (network && network.lastBlock) { + if (network.lastBlock.baseFeePerGas) { + // the sending stack (web3.js / metamask need to have the type defined) + // this is to avoid the following issue: https://github.com/MetaMask/metamask-extension/issues/11824 + tx.type = '0x2' + tx.maxFeePerGas = network.lastBlock.baseFeePerGas + 1 // + priority fee + } else { + tx.type = '0x1' + tx.gasPrice = network.lastBlock.baseFeePerGas + } + } + this.getWeb3().eth.estimateGas(tx, (err, gasEstimation) => { + if (err && err.message.indexOf('Invalid JSON RPC response') !== -1) { + // // @todo(#378) this should be removed when https://github.com/WalletConnect/walletconnect-monorepo/issues/334 is fixed + callback(new Error('Gas estimation failed because of an unknown internal error. This may indicated that the transaction will fail.')) } err = network.name === 'VM' ? null : err // just send the tx if "VM" gasEstimationForceSend(err, () => { @@ -142,7 +153,7 @@ export class TxRunnerWeb3 { return callback('Gas required exceeds block gas limit: ' + gasLimit + '. ' + warnEstimation) } }) - }) + }) }) } }