From f0c608540854d5dfd260d23a7ba69630a7de31c8 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Tue, 10 May 2022 12:55:45 +0200 Subject: [PATCH 1/6] fixes maxPriorityFeePerGas in _handleTxPricing --- packages/web3-core-method/src/index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index a82f66a15a1..5900349362e 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -861,6 +861,18 @@ function _handleTxPricing(method, tx) { call: 'eth_gasPrice', params: 0 })).createFunction(method.requestManager); + var getFeeHistory = new Method({ + name: "getFeeHistory", + call: "eth_feeHistory", + params: 3, + inputFormatter: [ + utils.numberToHex, + function (blockNumber) { + return blockNumber ? utils.toHex(blockNumber) : "latest"; + }, + null, + ], + }).createFunction(method.requestManager); Promise.all([ getBlockByNumber(), @@ -883,7 +895,13 @@ function _handleTxPricing(method, tx) { maxFeePerGas = tx.gasPrice; delete tx.gasPrice; } else { - maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x9502F900'; // 2.5 Gwei + const feeHistory = await getFeeHistory(1); + const [baseFee] = feeHistory.baseFeePerGas; + const priorityFee = utils.numberToHex( + utils.hexToNumber(gasPrice) - utils.hexToNumber(baseFee) + ); + + maxPriorityFeePerGas = tx.maxPriorityFeePerGas || priorityFee || '0x9502F900'; // 2.5 Gwei maxFeePerGas = tx.maxFeePerGas || utils.toHex( utils.toBN(block.baseFeePerGas) From 4242abb9354436e492f76aaa24aa9c3d630c1037 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Tue, 10 May 2022 13:01:18 +0200 Subject: [PATCH 2/6] updates changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f14cac31c2e..8c43b3a7a0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -548,10 +548,12 @@ Released with 1.0.0-beta.37 code base. - Fix static tuple encoding (#4673) (#4884) - Fix bug in handleRevert logic for eth_sendRawTransaction (#4902) - Fix resolve type of getBlock function (#4911) +- Fix resolve issue with to low priority fee on polygon (#4857) ### Changed - Replace deprecated String.prototype.substr() (#4855) - Exporting AbiCoder as coder (#4937) +- `web3-core-method._handleTxPricing` uses now `eth_feeHistory` for priority fee calculation on EIP-1559 compatible chains (#5018) ### Added - Exposing `web3.eth.Contract.setProvider()` as per public documentation (#4822) (#5001) From 09fa3b0b65875e86553815eb993b098822c6b5fb Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Tue, 10 May 2022 13:06:05 +0200 Subject: [PATCH 3/6] adds missing async flag in _handleTxPricing --- packages/web3-core-method/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index 5900349362e..a1bbd215680 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -877,7 +877,7 @@ function _handleTxPricing(method, tx) { Promise.all([ getBlockByNumber(), getGasPrice() - ]).then(responses => { + ]).then(async responses => { const [block, gasPrice] = responses; if ( (tx.type === '0x2' || tx.type === undefined) && From 4dc61bb8f464599938db910699a831015b67e39c Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Tue, 10 May 2022 13:14:56 +0200 Subject: [PATCH 4/6] fixes test with new eth_feeHistory request --- test/contract.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/contract.js b/test/contract.js index 9a7fa16894d..f8f8cffeb01 100644 --- a/test/contract.js +++ b/test/contract.js @@ -2684,6 +2684,10 @@ var runTests = function(contractFactory) { provider.injectResult('0x45656456456456'); + provider.injectValidation(function (payload) { + assert.equal(payload.method, 'eth_feeHistory') + assert.deepEqual(payload.params, ['0x1', 'latest', null]) + }) provider.injectValidation(function (payload) { assert.equal(payload.method, 'eth_sendTransaction'); From c7b868df070607f5295fb50f6ce691f99bb94197 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Wed, 11 May 2022 10:12:30 +0200 Subject: [PATCH 5/6] improves maxPriorityFeePerGas in _handleTxPricing --- packages/web3-core-method/src/index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index a1bbd215680..6dd4d351257 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -895,13 +895,16 @@ function _handleTxPricing(method, tx) { maxFeePerGas = tx.gasPrice; delete tx.gasPrice; } else { - const feeHistory = await getFeeHistory(1); - const [baseFee] = feeHistory.baseFeePerGas; - const priorityFee = utils.numberToHex( - utils.hexToNumber(gasPrice) - utils.hexToNumber(baseFee) - ); - - maxPriorityFeePerGas = tx.maxPriorityFeePerGas || priorityFee || '0x9502F900'; // 2.5 Gwei + let maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x9502F900'; // 2.5 Gwei + + // if no priority fee is set in the tx try to get it from the rpc + if(maxPriorityFeePerGas === '0x9502F900') { + const feeHistory = await getFeeHistory(1); + if(feeHistory && feeHistory.baseFeePerGas) { + const [baseFee] = feeHistory.baseFeePerGas; + maxPriorityFeePerGas = utils.numberToHex(utils.hexToNumber(gasPrice) - utils.hexToNumber(baseFee)); + } + } maxFeePerGas = tx.maxFeePerGas || utils.toHex( utils.toBN(block.baseFeePerGas) From 482786b32f62ac6da07925a58d69be9217aba7b3 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Wed, 11 May 2022 10:37:00 +0200 Subject: [PATCH 6/6] fixes variable scope in _handleTxPricing --- packages/web3-core-method/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index 6dd4d351257..645dc779005 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -895,7 +895,7 @@ function _handleTxPricing(method, tx) { maxFeePerGas = tx.gasPrice; delete tx.gasPrice; } else { - let maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x9502F900'; // 2.5 Gwei + maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x9502F900'; // 2.5 Gwei // if no priority fee is set in the tx try to get it from the rpc if(maxPriorityFeePerGas === '0x9502F900') {