diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index d1d7fb059ac..5856653c181 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -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 `. +- ``transactionBlockTimeout`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`transactionBlockTimeout `. +- ``transactionConfirmationBlocks`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`transactionConfirmationBlocks `. +- ``transactionPollingTimeout`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`transactionPollingTimeout `. +- ``chain`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`defaultChain `. +- ``hardfork`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`defaultHardfork `. +- ``common`` - ``Number``: It will otherwise use the default value provided from the Eth module. See :ref:`defaultCommon `. ------- diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index 805cbf98363..711cf9317b8 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -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; diff --git a/test/contract.js b/test/contract.js index 18d0a9c6c77..b727ee937b8 100644 --- a/test/contract.js +++ b/test/contract.js @@ -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();