From 61d5070afa09c11a695e62d3b5ff06fd9c246709 Mon Sep 17 00:00:00 2001 From: sirpy Date: Fri, 7 Jan 2022 04:28:17 +0200 Subject: [PATCH 1/6] add: custom transaction polling interval (#4584) * add: custom transaction polling interval allow to set via options the interval which polls for TX status * add: transactionPollingInterval to contract * add: pass transactionPollingInterval to method * Update CHANGELOG.md * add: transactionPollingInterval docs Co-authored-by: Nazar Hussain --- CHANGELOG.md | 3 ++- docs/web3-eth-contract.rst | 21 +++++++++++++++++++++ packages/web3-core-method/src/index.js | 3 ++- packages/web3-eth-contract/src/index.js | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 182437c18f0..7c55ba11b97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -484,7 +484,8 @@ Released with 1.0.0-beta.37 code base. ### Added - `maxPriorityFeePerGas` and `maxFeePerGas` added to `Transaction` and `TransactionConfig` interfaces (#4232) (#4585) - +- `transactionPollingInterval` added to web3, contract and method constructor options. defaults to 1 second. (#4584) +- ### Fixed - Fix readthedoc's build for web3js documentation (#4425) - Fix response sorting for batch requests (#4250) diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index 7490f416638..a915873a8b8 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -380,6 +380,27 @@ Returns ------------------------------------------------------------------------------ +.. _eth-contract-module-transactionpollinginterval: + +transactionPollingInterval +===================== + +.. code-block:: javascript + + web3.eth.Contract.transactionPollingInterval + contract.transactionPollingInterval // on contract instance + +The ``transactionPollingInterval`` is used over HTTP connections. This option defines the number of seconds between Web3 calls for a receipt which confirms that a transaction was mined by the network. + + +------- +Returns +------- + +``number``: The current value of transactionPollingInterval (default: 1000) + +------------------------------------------------------------------------------ + .. _eth-contract-module-handlerevert: handleRevert diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index afffc8560a1..e50a286b06e 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -56,6 +56,7 @@ var Method = function Method(options) { this.transactionBlockTimeout = options.transactionBlockTimeout || 50; this.transactionConfirmationBlocks = options.transactionConfirmationBlocks || 24; this.transactionPollingTimeout = options.transactionPollingTimeout || 750; + this.transactionPollingInterval = options.transactionPollingInterval || 1000; this.blockHeaderTimeout = options.blockHeaderTimeout || 10; // 10 seconds this.defaultCommon = options.defaultCommon; this.defaultChain = options.defaultChain; @@ -553,7 +554,7 @@ Method.prototype._confirmTransaction = function (defer, result, payload) { let blockHeaderArrived = false; const startInterval = () => { - intervalId = setInterval(checkConfirmation.bind(null, existingReceipt, true), 1000); + intervalId = setInterval(checkConfirmation.bind(null, existingReceipt, true), method.transactionPollingInterval); }; // If provider do not support event subscription use polling diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index 65c028379e5..c04758d22ea 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -234,6 +234,19 @@ var Contract = function Contract(jsonInterface, address, options) { }, enumerable: true }); + Object.defineProperty(this, 'transactionPollingInterval', { + get: function () { + if (_this.options.transactionPollingInterval === 0) { + return _this.options.transactionPollingInterval; + } + + return _this.options.transactionPollingInterval || this.constructor.transactionPollingInterval; + }, + set: function (val) { + _this.options.transactionPollingInterval = val; + }, + enumerable: true + }); Object.defineProperty(this, 'transactionConfirmationBlocks', { get: function () { if (_this.options.transactionConfirmationBlocks === 0) { @@ -1045,6 +1058,7 @@ Contract.prototype._executeMethod = function _executeMethod(){ transactionBlockTimeout: _this._parent.transactionBlockTimeout, transactionConfirmationBlocks: _this._parent.transactionConfirmationBlocks, transactionPollingTimeout: _this._parent.transactionPollingTimeout, + transactionPollingInterval: _this._parent.transactionPollingInterval, defaultCommon: _this._parent.defaultCommon, defaultChain: _this._parent.defaultChain, defaultHardfork: _this._parent.defaultHardfork, From c72a64db065416814ea73c14f11659549e70fb3e Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Thu, 6 Jan 2022 18:09:26 -1000 Subject: [PATCH 2/6] Add transactionPollingInterval to web3-eth --- docs/web3-eth.rst | 20 ++++++++++++++++++++ packages/web3-eth/src/index.js | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 3194ef7287d..6f059724b36 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -475,6 +475,26 @@ Example web3.eth.transactionPollingTimeout = 1000; +------------------------------------------------------------------------------ + +.. _eth-module-transactionpollinginterval: + +transactionPollingInterval +===================== + +.. code-block:: javascript + + web3.eth.transactionPollingInterval + +The ``transactionPollingInterval`` is used over HTTP connections. This option defines the number of seconds between Web3 calls for a receipt which confirms that a transaction was mined by the network. + + +------- +Returns +------- + +``number``: The current value of transactionPollingInterval (default: 1000) + ------------------------------------------------------------------------------ .. _web3-module-handlerevert: diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index a7efae31034..daffdee042b 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -100,6 +100,7 @@ var Eth = function Eth() { var transactionBlockTimeout = 50; var transactionConfirmationBlocks = 24; var transactionPollingTimeout = 750; + var transactionPollingInterval = 1000; var blockHeaderTimeout = 10; // 10 seconds var maxListenersWarningThreshold = 100; var defaultChain, defaultHardfork, defaultCommon; @@ -189,6 +190,23 @@ var Eth = function Eth() { }, enumerable: true }); + Object.defineProperty(this, 'transactionPollingInterval', { + get: function () { + return transactionPollingInterval; + }, + set: function (val) { + transactionPollingInterval = val; + + // also set on the Contract object + _this.Contract.transactionPollingInterval = transactionPollingInterval; + + // update defaultBlock + methods.forEach(function(method) { + method.transactionPollingInterval = transactionPollingInterval; + }); + }, + enumerable: true + }); Object.defineProperty(this, 'transactionConfirmationBlocks', { get: function () { return transactionConfirmationBlocks; @@ -351,6 +369,7 @@ var Eth = function Eth() { this.Contract.transactionBlockTimeout = this.transactionBlockTimeout; this.Contract.transactionConfirmationBlocks = this.transactionConfirmationBlocks; this.Contract.transactionPollingTimeout = this.transactionPollingTimeout; + this.Contract.transactionPollingInterval = this.transactionPollingInterval; this.Contract.blockHeaderTimeout = this.blockHeaderTimeout; this.Contract.handleRevert = this.handleRevert; this.Contract._requestManager = this._requestManager; @@ -680,6 +699,7 @@ var Eth = function Eth() { method.transactionBlockTimeout = _this.transactionBlockTimeout; method.transactionConfirmationBlocks = _this.transactionConfirmationBlocks; method.transactionPollingTimeout = _this.transactionPollingTimeout; + method.transactionPollingInterval = _this.transactionPollingInterval; method.handleRevert = _this.handleRevert; }); From b8c8d95433832b2db1f35d98824ab62693e4c6b1 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Thu, 6 Jan 2022 18:10:02 -1000 Subject: [PATCH 3/6] Init transactionPollingInterval tests --- test/contract.js | 15 +++++++++++++++ test/eth.transactionPollingInterval.js | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/eth.transactionPollingInterval.js diff --git a/test/contract.js b/test/contract.js index 3c7352d4383..9a7fa16894d 100644 --- a/test/contract.js +++ b/test/contract.js @@ -356,6 +356,21 @@ var runTests = function(contractFactory) { assert.equal(contract.options.transactionPollingTimeout, 0); }); + it('should define the transactionPollingInterval object property if passed over the options', function() { + var provider = new FakeIpcProvider(); + var contract = contractFactory(abi, address, {transactionPollingInterval: 0}, provider); + + assert.equal(contract.transactionPollingInterval, 0); + assert.equal(contract.options.transactionPollingInterval, 0); + }); + it('should update the transactionPollingInterval property in the options object', function() { + var provider = new FakeIpcProvider(); + var contract = contractFactory(abi, address, {transactionPollingInterval: 1}, provider); + + contract.transactionPollingInterval = 0; + + assert.equal(contract.options.transactionPollingInterval, 0); + }); it('should define the transactionConfirmationBlocks object property if passed over the options', function() { var provider = new FakeIpcProvider(); var contract = contractFactory(abi, address, {transactionConfirmationBlocks: 0}, provider); diff --git a/test/eth.transactionPollingInterval.js b/test/eth.transactionPollingInterval.js new file mode 100644 index 00000000000..06505e598f8 --- /dev/null +++ b/test/eth.transactionPollingInterval.js @@ -0,0 +1,25 @@ +var chai = require('chai'); +var assert = chai.assert; +var Eth = require('../packages/web3-eth'); + +var eth = new Eth(); + +var setValue = 123; + +describe('web3.eth', function () { + describe('transactionPollingInterval', function () { + it('should check if transactionPollingInterval is set to proper value', function () { + assert.equal(eth.transactionPollingInterval, 1000); + assert.equal(eth.Contract.transactionPollingInterval, 1000); + assert.equal(eth.getCode.method.transactionPollingInterval, 1000); + }); + it('should set transactionPollingInterval for all sub packages is set to proper value, if Eth package is changed', function () { + eth.transactionPollingInterval = setValue; + + assert.equal(eth.transactionPollingInterval, setValue); + assert.equal(eth.Contract.transactionPollingInterval, setValue); + assert.equal(eth.getCode.method.transactionPollingInterval, setValue); + }); + }); +}); + From a704f719dd32af72c72087114611d0fa1807b5e2 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Fri, 7 Jan 2022 06:54:14 -1000 Subject: [PATCH 4/6] Update docs/web3-eth.rst Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com> --- docs/web3-eth.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 6f059724b36..e2d2933c87f 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -493,7 +493,7 @@ The ``transactionPollingInterval`` is used over HTTP connections. This option de Returns ------- -``number``: The current value of transactionPollingInterval (default: 1000) +``number``: The current value of transactionPollingInterval (default: 1000ms) ------------------------------------------------------------------------------ From 1e0070ce6ed76ad8e61f031559ecb2b6a5fdf8f7 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Fri, 7 Jan 2022 06:54:39 -1000 Subject: [PATCH 5/6] Update docs/web3-eth-contract.rst Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com> --- docs/web3-eth-contract.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index a915873a8b8..97b6acfdc2d 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -397,7 +397,7 @@ The ``transactionPollingInterval`` is used over HTTP connections. This option de Returns ------- -``number``: The current value of transactionPollingInterval (default: 1000) +``number``: The current value of transactionPollingInterval (default: 1000ms) ------------------------------------------------------------------------------ From 1915a999329f61bc66c87c9e95cce9464b866c88 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Fri, 14 Jan 2022 08:52:16 -1000 Subject: [PATCH 6/6] Move 4672 change to 1.7.1 --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14443835ed5..dad93a2af20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -484,8 +484,7 @@ Released with 1.0.0-beta.37 code base. ### Added - `maxPriorityFeePerGas` and `maxFeePerGas` added to `Transaction` and `TransactionConfig` interfaces (#4232) (#4585) -- `transactionPollingInterval` added to web3, contract and method constructor options. defaults to 1 second. (#4584) -- + ### Fixed - Fix readthedoc's build for web3js documentation (#4425) - Fix response sorting for batch requests (#4250) @@ -496,6 +495,9 @@ Released with 1.0.0-beta.37 code base. ## [1.7.1] +### Added +- `transactionPollingInterval` added to web3, contract and method constructor options. defaults to 1 second. (#4584) + ### Fixed - Fix a typo in the documentation for `methods.myMethod.send` (#4599) - Added effectiveGasPrice to TransactionReceipt (#4692)