Skip to content

Commit

Permalink
Fixes #1188 (#1191)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
benjamincburns authored and frozeman committed Dec 21, 2017
1 parent dd9d540 commit 6cd3885
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 226 deletions.
30 changes: 16 additions & 14 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 @@ -173,8 +173,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 @@ -216,9 +216,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 @@ -498,7 +498,8 @@ Contract.prototype.deploy = function(options, callback){
return this._createTxObject.apply({
method: constructor,
parent: this,
deployData: options.data
deployData: options.data,
_ethAccounts: this.constructor._ethAccounts
}, options.arguments);

};
Expand Down Expand Up @@ -695,6 +696,7 @@ Contract.prototype._createTxObject = function _createTxObject(){
txObject.arguments = args || [];
txObject._method = this.method;
txObject._parent = this.parent;
txObject._ethAccounts = this.constructor._ethAccounts || this._ethAccounts;

if(this.deployData) {
txObject._deployData = this.deployData;
Expand Down Expand Up @@ -756,8 +758,8 @@ Contract.prototype._processExecuteArguments = function _processExecuteArguments(
Contract.prototype._executeMethod = function _executeMethod(){
var _this = this,
args = this._parent._processExecuteArguments.call(this, Array.prototype.slice.call(arguments), defer),
defer = promiEvent((args.type !== 'send'));

defer = promiEvent((args.type !== 'send')),
ethAccounts = _this.constructor._ethAccounts || _this._ethAccounts;

// simple return request for batch requests
if(args.generateRequest) {
Expand Down Expand Up @@ -788,7 +790,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: ethAccounts, // is eth.accounts (necessary for wallet signing)
defaultAccount: _this._parent.defaultAccount,
defaultBlock: _this._parent.defaultBlock
})).createFunction();
Expand All @@ -809,7 +811,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: ethAccounts, // is eth.accounts (necessary for wallet signing)
defaultAccount: _this._parent.defaultAccount,
defaultBlock: _this._parent.defaultBlock
})).createFunction();
Expand Down Expand Up @@ -879,7 +881,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 || _this._ethAccounts, // is eth.accounts (necessary for wallet signing)
defaultAccount: _this._parent.defaultAccount,
defaultBlock: _this._parent.defaultBlock,
extraFormatters: extraFormatters
Expand Down
20 changes: 19 additions & 1 deletion packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var utils = require('web3-utils');
var Net = require('web3-net');

var Personal = require('web3-eth-personal');
var Contract = require('web3-eth-contract');
var BaseContract = require('web3-eth-contract');
var Iban = require('web3-eth-iban');
var Accounts = require('web3-eth-accounts');
var abi = require('web3-eth-abi');
Expand Down Expand Up @@ -138,6 +138,24 @@ var Eth = function Eth() {
this.personal = new Personal(this.currentProvider);
this.personal.defaultAccount = this.defaultAccount;

// create a proxy Contract type for this instance, as a Contract's provider
// is stored as a class member rather than an instance variable. If we do
// not create this proxy type, changing the provider in one instance of
// web3-eth would subsequently change the provider for _all_ contract
// instances!
var Contract = function Contract() {
BaseContract.apply(this, arguments);
};

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

// make our proxy Contract inherit from web3-eth-contract so that it has all
// the right functionality and so that instanceof and friends work properly
Contract.prototype = Object.create(BaseContract.prototype);
Contract.prototype.constructor = Contract;

// add contract
this.Contract = Contract;
this.Contract.defaultAccount = this.defaultAccount;
Expand Down
Loading

0 comments on commit 6cd3885

Please sign in to comment.