Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 6cd3885

Browse files
benjamincburnsfrozeman
authored andcommittedDec 21, 2017
Fixes #1188 (#1191)
* Fixes #1188 * Exercise standalone Contracts, plus extra set provider tests * forgot to fix up new test during merge * fix broken test * Review changes: get rid of util.inherits, rename ContractWrapper to Contract
1 parent dd9d540 commit 6cd3885

File tree

4 files changed

+305
-226
lines changed

4 files changed

+305
-226
lines changed
 

‎packages/web3-eth-contract/src/index.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ var Contract = function Contract(jsonInterface, address, options) {
5555
var _this = this,
5656
args = Array.prototype.slice.call(arguments);
5757

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

5962
// sets _requestmanager
60-
core.packageInit(this, [Contract.currentProvider]);
63+
core.packageInit(this, [this.constructor.currentProvider]);
6164

6265
this.clearSubscriptions = this._requestManager.clearSubscriptions;
6366

6467

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

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

175175
// get default account from the Class
176-
var defaultAccount = Contract.defaultAccount;
177-
var defaultBlock = Contract.defaultBlock || 'latest';
176+
var defaultAccount = this.constructor.defaultAccount;
177+
var defaultBlock = this.constructor.defaultBlock || 'latest';
178178

179179
Object.defineProperty(this, 'defaultAccount', {
180180
get: function () {
@@ -216,9 +216,9 @@ var Contract = function Contract(jsonInterface, address, options) {
216216

217217
Contract.setProvider = function(provider, accounts) {
218218
// Contract.currentProvider = provider;
219-
core.packageInit(Contract, [provider]);
219+
core.packageInit(this, [provider]);
220220

221-
Contract._ethAccounts = accounts;
221+
this._ethAccounts = accounts;
222222
};
223223

224224

@@ -498,7 +498,8 @@ Contract.prototype.deploy = function(options, callback){
498498
return this._createTxObject.apply({
499499
method: constructor,
500500
parent: this,
501-
deployData: options.data
501+
deployData: options.data,
502+
_ethAccounts: this.constructor._ethAccounts
502503
}, options.arguments);
503504

504505
};
@@ -695,6 +696,7 @@ Contract.prototype._createTxObject = function _createTxObject(){
695696
txObject.arguments = args || [];
696697
txObject._method = this.method;
697698
txObject._parent = this.parent;
699+
txObject._ethAccounts = this.constructor._ethAccounts || this._ethAccounts;
698700

699701
if(this.deployData) {
700702
txObject._deployData = this.deployData;
@@ -756,8 +758,8 @@ Contract.prototype._processExecuteArguments = function _processExecuteArguments(
756758
Contract.prototype._executeMethod = function _executeMethod(){
757759
var _this = this,
758760
args = this._parent._processExecuteArguments.call(this, Array.prototype.slice.call(arguments), defer),
759-
defer = promiEvent((args.type !== 'send'));
760-
761+
defer = promiEvent((args.type !== 'send')),
762+
ethAccounts = _this.constructor._ethAccounts || _this._ethAccounts;
761763

762764
// simple return request for batch requests
763765
if(args.generateRequest) {
@@ -788,7 +790,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
788790
inputFormatter: [formatters.inputCallFormatter],
789791
outputFormatter: utils.hexToNumber,
790792
requestManager: _this._parent._requestManager,
791-
accounts: Contract._ethAccounts, // is eth.accounts (necessary for wallet signing)
793+
accounts: ethAccounts, // is eth.accounts (necessary for wallet signing)
792794
defaultAccount: _this._parent.defaultAccount,
793795
defaultBlock: _this._parent.defaultBlock
794796
})).createFunction();
@@ -809,7 +811,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
809811
return _this._parent._decodeMethodReturn(_this._method.outputs, result);
810812
},
811813
requestManager: _this._parent._requestManager,
812-
accounts: Contract._ethAccounts, // is eth.accounts (necessary for wallet signing)
814+
accounts: ethAccounts, // is eth.accounts (necessary for wallet signing)
813815
defaultAccount: _this._parent.defaultAccount,
814816
defaultBlock: _this._parent.defaultBlock
815817
})).createFunction();
@@ -879,7 +881,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
879881
params: 1,
880882
inputFormatter: [formatters.inputTransactionFormatter],
881883
requestManager: _this._parent._requestManager,
882-
accounts: Contract._ethAccounts, // is eth.accounts (necessary for wallet signing)
884+
accounts: _this.constructor._ethAccounts || _this._ethAccounts, // is eth.accounts (necessary for wallet signing)
883885
defaultAccount: _this._parent.defaultAccount,
884886
defaultBlock: _this._parent.defaultBlock,
885887
extraFormatters: extraFormatters

‎packages/web3-eth/src/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var utils = require('web3-utils');
3131
var Net = require('web3-net');
3232

3333
var Personal = require('web3-eth-personal');
34-
var Contract = require('web3-eth-contract');
34+
var BaseContract = require('web3-eth-contract');
3535
var Iban = require('web3-eth-iban');
3636
var Accounts = require('web3-eth-accounts');
3737
var abi = require('web3-eth-abi');
@@ -138,6 +138,24 @@ var Eth = function Eth() {
138138
this.personal = new Personal(this.currentProvider);
139139
this.personal.defaultAccount = this.defaultAccount;
140140

141+
// create a proxy Contract type for this instance, as a Contract's provider
142+
// is stored as a class member rather than an instance variable. If we do
143+
// not create this proxy type, changing the provider in one instance of
144+
// web3-eth would subsequently change the provider for _all_ contract
145+
// instances!
146+
var Contract = function Contract() {
147+
BaseContract.apply(this, arguments);
148+
};
149+
150+
Contract.setProvider = function() {
151+
BaseContract.setProvider.apply(this, arguments);
152+
};
153+
154+
// make our proxy Contract inherit from web3-eth-contract so that it has all
155+
// the right functionality and so that instanceof and friends work properly
156+
Contract.prototype = Object.create(BaseContract.prototype);
157+
Contract.prototype.constructor = Contract;
158+
141159
// add contract
142160
this.Contract = Contract;
143161
this.Contract.defaultAccount = this.defaultAccount;

0 commit comments

Comments
 (0)
This repository has been archived.