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) diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index a82f66a15a1..645dc779005 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -861,11 +861,23 @@ 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(), getGasPrice() - ]).then(responses => { + ]).then(async responses => { const [block, gasPrice] = responses; if ( (tx.type === '0x2' || tx.type === undefined) && @@ -883,7 +895,16 @@ function _handleTxPricing(method, tx) { maxFeePerGas = tx.gasPrice; delete tx.gasPrice; } else { - 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') { + 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) 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');