Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 29 additions & 80 deletions packages/web3-core-helpers/package-lock.json

Large diffs are not rendered by default.

90 changes: 49 additions & 41 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @date 2017
*/

"use strict";
'use strict';


var _ = require('underscore');
Expand All @@ -29,24 +29,26 @@ var Jsonrpc = require('./jsonrpc.js');
var BatchManager = require('./batch.js');
var givenProvider = require('./givenProvider.js');



/**
/**
* It's responsible for passing messages to providers
* It's also responsible for polling the ethereum node for incoming messages
* Default poll timeout is 1 second
* Singleton
*
* @param {string|Object}provider
* @param {Net.Socket} net
*
* @constructor
*/
var RequestManager = function RequestManager(provider) {
var RequestManager = function RequestManager(provider, net) {
this.provider = null;
this.providers = RequestManager.providers;

this.setProvider(provider);
this.setProvider(provider, net);
this.subscriptions = {};
};



RequestManager.givenProvider = givenProvider;

RequestManager.providers = {
Expand All @@ -56,50 +58,57 @@ RequestManager.providers = {
};



/**
* Should be used to set provider of request manager
*
* @method setProvider
* @param {Object} p
*
* @param {Object} provider
* @param {net.Socket} net
*
* @returns void
*/
RequestManager.prototype.setProvider = function (p, net) {
RequestManager.prototype.setProvider = function(provider, net) {
var _this = this;

// autodetect provider
if(p && typeof p === 'string' && this.providers) {
if (provider && typeof provider === 'string' && this.providers) {

// HTTP
if(/^http(s)?:\/\//i.test(p)) {
p = new this.providers.HttpProvider(p);
if (/^http(s)?:\/\//i.test(provider)) {
provider = new this.providers.HttpProvider(provider);

// WS
} else if(/^ws(s)?:\/\//i.test(p)) {
p = new this.providers.WebsocketProvider(p);
} else if (/^ws(s)?:\/\//i.test(provider)) {
provider = new this.providers.WebsocketProvider(provider);

// IPC
} else if(p && typeof net === 'object' && typeof net.connect === 'function') {
p = new this.providers.IpcProvider(p, net);
} else if (provider && typeof net === 'object' && typeof net.connect === 'function') {
provider = new this.providers.IpcProvider(provider, net);

} else if(p) {
throw new Error('Can\'t autodetect provider for "'+ p +'"');
} else if (provider) {
throw new Error('Can\'t autodetect provider for "' + provider + '"');
}
}

// reset the old one before changing, if still connected
if(this.provider && this.provider.connected)
if (this.provider && this.provider.connected)
this.clearSubscriptions();


this.provider = p || null;
this.provider = provider || null;

// listen to incoming notifications
if(this.provider && this.provider.on) {
this.provider.on('data', function requestManagerNotification(result, deprecatedResult){
if (this.provider && this.provider.on) {
this.provider.on('data', function(result, deprecatedResult) {
result = result || deprecatedResult; // this is for possible old providers, which may had the error first handler

// check for result.method, to prevent old providers errors to pass as result
if(result.method && _this.subscriptions[result.params.subscription] && _this.subscriptions[result.params.subscription].callback) {
if (
result.method &&
_this.subscriptions[result.params.subscription] &&
_this.subscriptions[result.params.subscription].callback
) {
_this.subscriptions[result.params.subscription].callback(null, result.params.result);
}
});
Expand All @@ -113,24 +122,24 @@ RequestManager.prototype.setProvider = function (p, net) {
}
};


/**
* Should be used to asynchronously send request
*
* @method sendAsync
* @param {Object} data
* @param {Function} callback
*/
RequestManager.prototype.send = function (data, callback) {
callback = callback || function(){};
RequestManager.prototype.send = function(data, callback) {
callback = callback || function() {
};

if (!this.provider) {
return callback(errors.InvalidProvider());
}

var payload = Jsonrpc.toPayload(data.method, data.params);
this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](payload, function (err, result) {
if(result && result.id && payload.id !== result.id) return callback(new Error('Wrong response id "'+ result.id +'" (expected: "'+ payload.id +'") in '+ JSON.stringify(payload)));
this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](payload, function(err, result) {
if (result && result.id && payload.id !== result.id) return callback(new Error('Wrong response id "' + result.id + '" (expected: "' + payload.id + '") in ' + JSON.stringify(payload)));

if (err) {
return callback(err);
Expand All @@ -155,13 +164,13 @@ RequestManager.prototype.send = function (data, callback) {
* @param {Array} batch data
* @param {Function} callback
*/
RequestManager.prototype.sendBatch = function (data, callback) {
RequestManager.prototype.sendBatch = function(data, callback) {
if (!this.provider) {
return callback(errors.InvalidProvider());
}

var payload = Jsonrpc.toBatchPayload(data);
this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](payload, function (err, results) {
this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](payload, function(err, results) {
if (err) {
return callback(err);
}
Expand All @@ -184,16 +193,15 @@ RequestManager.prototype.sendBatch = function (data, callback) {
* @param {String} type the subscription namespace (eth, personal, etc)
* @param {Function} callback the callback to call for incoming notifications
*/
RequestManager.prototype.addSubscription = function (id, name, type, callback) {
if(this.provider.on) {
RequestManager.prototype.addSubscription = function(id, name, type, callback) {
if (this.provider.on) {
this.subscriptions[id] = {
callback: callback,
type: type,
name: name
};

} else {
throw new Error('The provider doesn\'t support subscriptions: '+ this.provider.constructor.name);
throw new Error('The provider doesn\'t support subscriptions: ' + this.provider.constructor.name);
}
};

Expand All @@ -204,10 +212,10 @@ RequestManager.prototype.addSubscription = function (id, name, type, callback) {
* @param {String} id the subscription id
* @param {Function} callback fired once the subscription is removed
*/
RequestManager.prototype.removeSubscription = function (id, callback) {
RequestManager.prototype.removeSubscription = function(id, callback) {
var _this = this;

if(this.subscriptions[id]) {
if (this.subscriptions[id]) {

this.send({
method: this.subscriptions[id].type + '_unsubscribe',
Expand All @@ -224,19 +232,19 @@ RequestManager.prototype.removeSubscription = function (id, callback) {
*
* @method reset
*/
RequestManager.prototype.clearSubscriptions = function (keepIsSyncing) {
RequestManager.prototype.clearSubscriptions = function(keepIsSyncing) {
var _this = this;


// uninstall all subscriptions
Object.keys(this.subscriptions).forEach(function(id){
if(!keepIsSyncing || _this.subscriptions[id].name !== 'syncing')
Object.keys(this.subscriptions).forEach(function(id) {
if (!keepIsSyncing || _this.subscriptions[id].name !== 'syncing')
_this.removeSubscription(id);
});


// reset notification callbacks etc.
if(this.provider.reset)
if (this.provider.reset)
this.provider.reset();
};

Expand Down
29 changes: 16 additions & 13 deletions packages/web3-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
* @date 2017
*/

"use strict";
'use strict';


var requestManager = require('web3-core-requestmanager');
var extend = require('./extend.js');

module.exports = {
packageInit: function (pkg, args) {
packageInit: function(pkg, args) {
args = Array.prototype.slice.call(args);

if (!pkg) {
throw new Error('You need to instantiate using the "new" keyword.');
}


// make property of pkg._provider, which can properly set providers
Object.defineProperty(pkg, 'currentProvider', {
get: function () {
get: function() {
return pkg._provider;
},
set: function (value) {
set: function(value) {
return pkg.setProvider(value);
},
enumerable: true,
Expand All @@ -49,36 +48,40 @@ module.exports = {

// inherit from web3 umbrella package
if (args[0] && args[0]._requestManager) {
pkg._requestManager = new requestManager.Manager(args[0].currentProvider);

// set requestmanager on package
pkg._requestManager = args[0]._requestManager;
// set requestmanager on package
} else {
pkg._requestManager = new requestManager.Manager();
pkg._requestManager.setProvider(args[0], args[1]);
pkg._requestManager = new requestManager.Manager(args[0], args[1]);
}

// add givenProvider
pkg.givenProvider = requestManager.Manager.givenProvider;
pkg.providers = requestManager.Manager.providers;

pkg._provider = pkg._requestManager.provider;
pkg._provider = pkg._requestManager.provider;

// add SETPROVIDER function (don't overwrite if already existing)
if (!pkg.setProvider) {
pkg.setProvider = function (provider, net) {
pkg.setProvider = function(provider, net) {

pkg._requestManager.setProvider(provider, net);
pkg._provider = pkg._requestManager.provider;
return true;
};
}

pkg.setRequestManager = function(manager) {
pkg._requestManager = manager;
pkg._provider = manager.provider;
};

// attach batch request creation
pkg.BatchRequest = requestManager.BatchManager.bind(null, pkg._requestManager);

// attach extend function
pkg.extend = extend(pkg);
},
addProviders: function (pkg) {
addProviders: function(pkg) {
pkg.givenProvider = requestManager.Manager.givenProvider;
pkg.providers = requestManager.Manager.providers;
}
Expand Down
26 changes: 18 additions & 8 deletions packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,18 @@ var Contract = function Contract(jsonInterface, address, options) {
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!');
throw new Error('Please use the "new" keyword to instantiate a web3.eth.Contract() object!');
}

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

this.clearSubscriptions = this._requestManager.clearSubscriptions;



if(!jsonInterface || !(Array.isArray(jsonInterface))) {
throw new Error('You must provide the json interface of the contract when instantiating a contract object.');
}



// create the options object
this.options = {};

Expand Down Expand Up @@ -225,6 +221,17 @@ var Contract = function Contract(jsonInterface, address, options) {

};

/**
* Sets the new provider, creates a new requestManager, registers the "data" listener on the provider and sets the
* accounts module for the Contract class.
*
* @method setProvider
*
* @param {string|provider} provider
* @param {Accounts} accounts
*
* @returns void
*/
Contract.setProvider = function(provider, accounts) {
// Contract.currentProvider = provider;
core.packageInit(this, [provider]);
Expand Down Expand Up @@ -606,18 +613,20 @@ Contract.prototype.once = function(event, options, callback) {
* Adds event listeners and creates a subscription.
*
* @method _on
*
* @param {String} event
* @param {Object} options
* @param {Function} callback
*
* @return {Object} the event subscription
*/
Contract.prototype._on = function(){
var subOptions = this._generateEventOptions.apply(this, arguments);


// prevent the event "newListener" and "removeListener" from being overwritten
this._checkListener('newListener', subOptions.event.name, subOptions.callback);
this._checkListener('removeListener', subOptions.event.name, subOptions.callback);
this._checkListener('newListener', subOptions.event.name);
this._checkListener('removeListener', subOptions.event.name);

// TODO check if listener already exists? and reuse subscription if options are the same.

Expand All @@ -643,6 +652,7 @@ Contract.prototype._on = function(){
type: 'eth',
requestManager: this._requestManager
});

subscription.subscribe('logs', subOptions.params, subOptions.callback || function () {});

return subscription;
Expand Down
1 change: 0 additions & 1 deletion packages/web3-eth-ens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"web3-core-helpers": "1.2.2",
"web3-core-promievent": "1.2.2",
"web3-eth-abi": "1.2.2",
"web3-eth-contract": "1.2.2",
"web3-utils": "1.2.2"
},
"devDependencies": {
Expand Down
Loading