Skip to content

Commit

Permalink
add: custom transaction polling interval (#4584) (#4672)
Browse files Browse the repository at this point in the history
* 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 <nazarhussain@gmail.com>

* Add transactionPollingInterval to web3-eth

* Init transactionPollingInterval tests

* Update docs/web3-eth.rst

Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com>

* Update docs/web3-eth-contract.rst

Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com>

* Move 4672 change to 1.7.1

Co-authored-by: sirpy <hadarr@gmail.com>
Co-authored-by: Nazar Hussain <nazarhussain@gmail.com>
Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com>
  • Loading branch information
4 people committed Jan 14, 2022
1 parent 0b890b7 commit b32555c
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,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)
Expand Down
21 changes: 21 additions & 0 deletions docs/web3-eth-contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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: 1000ms)

------------------------------------------------------------------------------

.. _eth-contract-module-handlerevert:

handleRevert
Expand Down
20 changes: 20 additions & 0 deletions docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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: 1000ms)

------------------------------------------------------------------------------

.. _web3-module-handlerevert:
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});

Expand Down
15 changes: 15 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions test/eth.transactionPollingInterval.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit b32555c

Please sign in to comment.