Skip to content

Commit

Permalink
Eth.setProvider now sets the provider on Contract instances
Browse files Browse the repository at this point in the history
When Eth.setProvider is called, any Contract instances created by the
Eth instance will have their providers updated
  • Loading branch information
ewingrj committed Jun 21, 2018
1 parent c0e727e commit 26a8775
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 26a8775

Please sign in to comment.