From 26a877517f65c9de650e13f940f995114d9f65ad Mon Sep 17 00:00:00 2001 From: perissology Date: Thu, 21 Jun 2018 13:24:33 -0700 Subject: [PATCH] Eth.setProvider now sets the provider on Contract instances When Eth.setProvider is called, any Contract instances created by the Eth instance will have their providers updated --- packages/web3-eth/src/index.js | 12 ++++++++++++ test/contract.js | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index 47762ed612e..f0cd4968164 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -143,8 +143,20 @@ var Eth = function Eth() { // not create this proxy type, changing the provider in one instance of // web3-eth would subsequently change the provider for _all_ contract // instances! + var self = this; var Contract = function Contract() { BaseContract.apply(this, arguments); + + // when Eth.setProvider is called, call packageInit + // on all contract instances instantiated via this Eth + // instances. This will update the currentProvider for + // the contract instances + var _this = this; + var setProvider = self.setProvider; + self.setProvider = function() { + setProvider.apply(self, arguments); + core.packageInit(_this, [self.currentProvider]); + }; }; Contract.setProvider = function() { diff --git a/test/contract.js b/test/contract.js index 4f6c9c37764..d0d7e211b5b 100644 --- a/test/contract.js +++ b/test/contract.js @@ -2912,6 +2912,20 @@ var runTests = function(contractFactory) { describe('typical usage', function() { runTests(getEthContractInstance); + it('should update contract instance provider when assigned a provider to eth instance that contract instance came from', function () { + var provider1 = new FakeIpcProvider(); + var provider2 = new FakeHttpProvider(); + + var eth = new Eth(provider1); + var contract = new eth.Contract(abi, address); + assert.deepEqual(contract.currentProvider, provider1); + assert.deepEqual(eth.currentProvider, provider1); + + eth.setProvider(provider2); + assert.deepEqual(contract.currentProvider, provider2); + assert.deepEqual(eth.currentProvider, provider2); + }); + it('should deploy a contract, sign transaction, and return contract instance', function (done) { var provider = new FakeIpcProvider(); var eth = new Eth(provider);