Skip to content

Commit

Permalink
contract options handling improved, contract test extended, and relat…
Browse files Browse the repository at this point in the history
…ed documentation updated
  • Loading branch information
nivida committed Dec 4, 2019
1 parent eba77c2 commit 0623f2c
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/web3-eth-contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ Properties
- ``from`` - ``String``: The address transactions should be made from.
- ``gasPrice`` - ``String``: The gas price in wei to use for transactions.
- ``gas`` - ``Number``: The maximum gas provided for a transaction (gas limit).
- ``handleRevert`` - ``Boolean``: It will otherwise use the default value provided from the Eth module. See :ref:`handleRevert <eth-contract-module-handlerevert>`.
- ``transactionBlockTimeout`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`transactionBlockTimeout <eth-contract-transactionblocktimeout>`.
- ``transactionConfirmationBlocks`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`transactionConfirmationBlocks <eth-contract-module-transactionconfirmationblocks>`.
- ``transactionPollingTimeout`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`transactionPollingTimeout <eth-contract-module-transactionpollingtimeout>`.
- ``chain`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`defaultChain <eth-contract-defaultchain>`.
- ``hardfork`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`defaultHardfork <eth-contract-defaulthardfork>`.
- ``common`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`defaultCommon <eth-contract-defaultcommon>`.


-------
Expand Down
86 changes: 79 additions & 7 deletions packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,86 @@ var Contract = function Contract(jsonInterface, address, options) {
// get default account from the Class
var defaultAccount = this.constructor.defaultAccount;
var defaultBlock = this.constructor.defaultBlock || 'latest';
this.transactionBlockTimeout = this.options.transactionBlockTimeout || this.constructor.transactionBlockTimeout;
this.transactionConfirmationBlocks = this.options.transactionConfirmationBlocks || this.constructor.transactionConfirmationBlocks;
this.transactionPollingTimeout = this.options.transactionPollingTimeout || this.constructor.transactionPollingTimeout;
this.defaultChain = this.options.defaultChain || this.constructor.defaultChain;
this.defaultHardfork = this.options.defaultHardfork || this.constructor.defaultHardfork;
this.defaultCommon = this.options.defaultCommon || this.constructor.defaultCommon;
this.handleRevert = this.options.handleRevert || this.constructor.handleRevert;

Object.defineProperty(this, 'handleRevert', {
get: function () {
if (_this.options.handleRevert === false || _this.options.handleRevert === true) {
return _this.options.handleRevert;
}

return this.constructor.handleRevert;
},
set: function (val) {
_this.options.handleRevert = val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultCommon', {
get: function () {
return _this.options.common || this.constructor.defaultCommon;
},
set: function (val) {
_this.options.common = val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultHardfork', {
get: function () {
return _this.options.hardfork || this.constructor.defaultHardfork;
},
set: function (val) {
_this.options.hardfork = val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultChain', {
get: function () {
return _this.options.chain || this.constructor.defaultChain;
},
set: function (val) {
_this.options.chain = val;
},
enumerable: true
});
Object.defineProperty(this, 'transactionPollingTimeout', {
get: function () {
if (_this.options.transactionPollingTimeout === 0) {
return _this.options.transactionPollingTimeout;
}

return _this.options.transactionPollingTimeout || this.constructor.transactionPollingTimeout;
},
set: function (val) {
_this.options.transactionPollingTimeout = val;
},
enumerable: true
});
Object.defineProperty(this, 'transactionConfirmationBlocks', {
get: function () {
if (_this.options.transactionConfirmationBlocks === 0) {
return _this.options.transactionConfirmationBlocks;
}

return _this.options.transactionConfirmationBlocks || this.constructor.transactionConfirmationBlocks;
},
set: function (val) {
_this.options.transactionConfirmationBlocks = val;
},
enumerable: true
});
Object.defineProperty(this, 'transactionBlockTimeout', {
get: function () {
if (_this.options.transactionBlockTimeout === 0) {
return _this.options.transactionBlockTimeout;
}

return _this.options.transactionBlockTimeout || this.constructor.transactionBlockTimeout;
},
set: function (val) {
_this.options.transactionBlockTimeout = val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultAccount', {
get: function () {
return defaultAccount;
Expand Down
105 changes: 105 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,111 @@ var runTests = function(contractFactory) {

assert.throws(test);
});
it('should define the handleRevert object property if passed over the options', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {handleRevert: true}, provider);

assert.equal(contract.handleRevert, true);
assert.equal(contract.options.handleRevert, true);
});
it('should update the handleRevert property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {handleRevert: false}, provider);

contract.handleRevert = true;

assert.equal(contract.options.handleRevert, true);
});
it('should define the defaultCommon object property if passed over the options', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {common: true}, provider);

assert.equal(contract.defaultCommon, true);
assert.equal(contract.options.common, true);
});
it('should update the defaultCommon property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {common: false}, provider);

contract.defaultCommon = true;

assert.equal(contract.options.common, true);
});
it('should define the defaultHardfork object property if passed over the options', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {hardfork: 'istanbul'}, provider);

assert.equal(contract.defaultHardfork, 'istanbul');
assert.equal(contract.options.hardfork, 'istanbul');
});
it('should update the defaultHardfork property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {hardfork: false}, provider);

contract.defaultHardfork = true;

assert.equal(contract.options.hardfork, true);
});
it('should define the defaultChain object property if passed over the options', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {chain: 'mainnet'}, provider);

assert.equal(contract.defaultChain, 'mainnet');
assert.equal(contract.options.chain, 'mainnet');
});
it('should update the defaultChain property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {chain: false}, provider);

contract.defaultChain = true;

assert.equal(contract.options.chain, true);
});
it('should define the transactionPollingTimeout object property if passed over the options', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {transactionPollingTimeout: 0}, provider);

assert.equal(contract.transactionPollingTimeout, 0);
assert.equal(contract.options.transactionPollingTimeout, 0);
});
it('should update the transactionPollingTimeout property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {transactionPollingTimeout: 1}, provider);

contract.transactionPollingTimeout = 0;

assert.equal(contract.options.transactionPollingTimeout, 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);

assert.equal(contract.transactionConfirmationBlocks, 0);
assert.equal(contract.options.transactionConfirmationBlocks, 0);
});
it('should update the transactionConfirmationBlocks property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {transactionConfirmationBlocks: 1}, provider);

contract.transactionConfirmationBlocks = 0;

assert.equal(contract.options.transactionConfirmationBlocks, 0);
});
it('should define the transactionBlockTimeout object property if passed over the options', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {transactionBlockTimeout: 0}, provider);

assert.equal(contract.transactionBlockTimeout, 0);
assert.equal(contract.options.transactionBlockTimeout, 0);
});
it('should update the transactionBlockTimeout property in the options object', function() {
var provider = new FakeIpcProvider();
var contract = contractFactory(abi, address, {transactionBlockTimeout: 1}, provider);

contract.transactionBlockTimeout = 0;

assert.equal(contract.options.transactionBlockTimeout, 0);
});
it('.clone() should properly clone the contract instance', function () {
var provider = new FakeIpcProvider();

Expand Down

0 comments on commit 0623f2c

Please sign in to comment.