Skip to content

Commit

Permalink
Fixes #1188
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns committed Nov 22, 2017
1 parent c4c92c3 commit 3663cb1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
22 changes: 11 additions & 11 deletions packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ var Contract = function Contract(jsonInterface, address, options) {
var _this = this,
args = Array.prototype.slice.call(arguments);

if(!(this instanceof Contract)) {
throw new Error('Please use the "new" keyword to instantiate a web3.eth.contract() object!');
}

// sets _requestmanager
core.packageInit(this, [Contract.currentProvider]);
core.packageInit(this, [this.constructor.currentProvider]);

this.clearSubscriptions = this._requestManager.clearSubscriptions;


if(!(this instanceof Contract)) {
throw new Error('Please use the "new" keyword to instantiate a web3.eth.contract() object!');
}

if(!jsonInterface || !(Array.isArray(jsonInterface))) {
throw new Error('You must provide the json interface of the contract when instantiating a contract object.');
Expand Down Expand Up @@ -165,8 +165,8 @@ var Contract = function Contract(jsonInterface, address, options) {
});

// get default account from the Class
var defaultAccount = Contract.defaultAccount;
var defaultBlock = Contract.defaultBlock || 'latest';
var defaultAccount = this.constructor.defaultAccount;
var defaultBlock = this.constructor.defaultBlock || 'latest';

Object.defineProperty(this, 'defaultAccount', {
get: function () {
Expand Down Expand Up @@ -208,9 +208,9 @@ var Contract = function Contract(jsonInterface, address, options) {

Contract.setProvider = function(provider, accounts) {
// Contract.currentProvider = provider;
core.packageInit(Contract, [provider]);
core.packageInit(this, [provider]);

Contract._ethAccounts = accounts;
this._ethAccounts = accounts;
};


Expand Down Expand Up @@ -777,7 +777,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
inputFormatter: [formatters.inputCallFormatter],
outputFormatter: utils.hexToNumber,
requestManager: _this._parent._requestManager,
accounts: Contract._ethAccounts, // is eth.accounts (necessary for wallet signing)
accounts: _this.constructor._ethAccounts, // is eth.accounts (necessary for wallet signing)
defaultAccount: _this._parent.defaultAccount,
defaultBlock: _this._parent.defaultBlock
})).createFunction();
Expand All @@ -798,7 +798,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
return _this._parent._decodeMethodReturn(_this._method.outputs, result);
},
requestManager: _this._parent._requestManager,
accounts: Contract._ethAccounts, // is eth.accounts (necessary for wallet signing)
accounts: _this.constructor._ethAccounts, // is eth.accounts (necessary for wallet signing)
defaultAccount: _this._parent.defaultAccount,
defaultBlock: _this._parent.defaultBlock
})).createFunction();
Expand Down Expand Up @@ -868,7 +868,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
params: 1,
inputFormatter: [formatters.inputTransactionFormatter],
requestManager: _this._parent._requestManager,
accounts: Contract._ethAccounts, // is eth.accounts (necessary for wallet signing)
accounts: _this.constructor._ethAccounts, // is eth.accounts (necessary for wallet signing)
defaultAccount: _this._parent.defaultAccount,
defaultBlock: _this._parent.defaultBlock,
extraFormatters: extraFormatters
Expand Down
14 changes: 13 additions & 1 deletion packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var Iban = require('web3-eth-iban');
var Accounts = require('web3-eth-accounts');
var abi = require('web3-eth-abi');

var util = require('util');

var getNetworkType = require('./getNetworkType.js');
var formatter = helpers.formatters;

Expand Down Expand Up @@ -138,8 +140,18 @@ var Eth = function Eth() {
this.personal = new Personal(this.currentProvider);
this.personal.defaultAccount = this.defaultAccount;

var ContractWrapper = function ContractWrapper() {
Contract.apply(this, arguments);
};

ContractWrapper.setProvider = function() {
Contract.setProvider.apply(this, arguments);
};

util.inherits(ContractWrapper, Contract);

// add contract
this.Contract = Contract;
this.Contract = ContractWrapper;
this.Contract.defaultAccount = this.defaultAccount;
this.Contract.defaultBlock = this.defaultBlock;
this.Contract.setProvider(this.currentProvider, this.accounts);
Expand Down
34 changes: 34 additions & 0 deletions test/setProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ describe('lib/web3/setProvider', function () {
provider1.bzz = 'http://localhost:8500';
provider2.bzz = 'http://swarm-gateways.net';

var provider3 = new FakeHttpProvider();
provider3.bzz = 'http://localhost2:8500';

var lib = new Web3(provider1);
var lib2 = new Web3(provider3);

assert.equal(lib.eth.currentProvider.constructor.name, provider1.constructor.name);
assert.equal(lib.eth.net.currentProvider.constructor.name, provider1.constructor.name);
Expand All @@ -71,6 +75,21 @@ describe('lib/web3/setProvider', function () {
assert.equal(lib.eth.accounts._requestManager.provider.constructor.name, provider1.constructor.name);
assert.equal(lib.shh._requestManager.provider.constructor.name, provider1.constructor.name);

assert.equal(lib2.eth.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.net.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.personal.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.Contract.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.accounts.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.shh.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.bzz.currentProvider, provider3.bzz);

assert.equal(lib2.eth._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.net._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.personal._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.Contract._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.accounts._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.shh._requestManager.provider.constructor.name, provider3.constructor.name);


lib.setProvider(provider2);

Expand All @@ -89,6 +108,21 @@ describe('lib/web3/setProvider', function () {
assert.equal(lib.eth.accounts._requestManager.provider.constructor.name, provider2.constructor.name);
assert.equal(lib.shh._requestManager.provider.constructor.name, provider2.constructor.name);

assert.equal(lib2.eth.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.net.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.personal.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.Contract.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.accounts.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.shh.currentProvider.constructor.name, provider3.constructor.name);
assert.equal(lib2.bzz.currentProvider, provider3.bzz);

assert.equal(lib2.eth._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.net._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.personal._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.Contract._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.eth.accounts._requestManager.provider.constructor.name, provider3.constructor.name);
assert.equal(lib2.shh._requestManager.provider.constructor.name, provider3.constructor.name);


});

Expand Down

0 comments on commit 3663cb1

Please sign in to comment.