diff --git a/lib/web3.js b/lib/web3.js index 8b6d073..afb65f1 100644 --- a/lib/web3.js +++ b/lib/web3.js @@ -37,7 +37,7 @@ var utils = require('./utils/utils'); var sha3 = require('./utils/sha3'); var extend = require('./web3/extend'); var Batch = require('./web3/batch'); -var Contract = require('./web3/contract'); +var Property = require('./web3/property'); function Web3 (provider) { this._requestManager = new RequestManager(provider); @@ -51,6 +51,9 @@ function Web3 (provider) { version: version.version }; this._extend = extend(this); + this._extend({ + properties: properties() + }); } Web3.prototype.setProvider = function (provider) { @@ -62,8 +65,6 @@ Web3.prototype.reset = function () { this.settings = new Settings(); }; - - Web3.prototype.toHex = utils.toHex; Web3.prototype.toAscii = utils.toAscii; Web3.prototype.toUtf8 = utils.toUtf8; @@ -86,38 +87,37 @@ Web3.prototype.fromICAP = function (icap) { return iban.address(); }; -//var web3Properties = [ - //new Property({ - //name: 'version.client', - //getter: 'web3_clientVersion' - //}), - //new Property({ - //name: 'version.network', - //getter: 'net_version', - //inputFormatter: utils.toDecimal - //}), - //new Property({ - //name: 'version.ethereum', - //getter: 'eth_protocolVersion', - //inputFormatter: utils.toDecimal - //}), - //new Property({ - //name: 'version.whisper', - //getter: 'shh_version', - //inputFormatter: utils.toDecimal - //}) -//]; - -//setupProperties(Web3.prototype, web3Properties); - -//Web3.prototype.isConnected = function(){ - //return (this.currentProvider && this.currentProvider.isConnected()); -//}; - -Web3.prototype.createBatch = function () { - return new Batch(); +var properties = function () { + return [ + new Property({ + name: 'version.client', + getter: 'web3_clientVersion' + }), + new Property({ + name: 'version.network', + getter: 'net_version', + inputFormatter: utils.toDecimal + }), + new Property({ + name: 'version.ethereum', + getter: 'eth_protocolVersion', + inputFormatter: utils.toDecimal + }), + new Property({ + name: 'version.whisper', + getter: 'shh_version', + inputFormatter: utils.toDecimal + }) + ]; }; +Web3.prototype.isConnected = function(){ + return (this.currentProvider && this.currentProvider.isConnected()); +}; + +Web3.prototype.createBatch = function () { + return new Batch(this); +}; module.exports = Web3; diff --git a/lib/web3/batch.js b/lib/web3/batch.js index 6b6b6f5..f31828c 100644 --- a/lib/web3/batch.js +++ b/lib/web3/batch.js @@ -20,11 +20,11 @@ * @date 2015 */ -var RequestManager = require('./requestmanager'); var Jsonrpc = require('./jsonrpc'); var errors = require('./errors'); -var Batch = function () { +var Batch = function (web3) { + this.requestManager = web3._requestManager; this.requests = []; }; @@ -45,7 +45,7 @@ Batch.prototype.add = function (request) { */ Batch.prototype.execute = function () { var requests = this.requests; - RequestManager.getInstance().sendBatch(requests, function (err, results) { + this.requestManager.sendBatch(requests, function (err, results) { results = results || []; requests.map(function (request, index) { return results[index] || {}; diff --git a/lib/web3/method.js b/lib/web3/method.js index a216421..8d72d0d 100644 --- a/lib/web3/method.js +++ b/lib/web3/method.js @@ -135,7 +135,7 @@ Method.prototype.attachToObject = function (obj) { Method.prototype.buildCall = function() { var method = this; - return function send() { + var send = function () { var payload = method.toPayload(Array.prototype.slice.call(arguments)); if (payload.callback) { return method.requestManager.sendAsync(payload, function (err, result) { @@ -144,6 +144,22 @@ Method.prototype.buildCall = function() { } return method.formatOutput(method.requestManager.send(payload)); }; + send.request = this.request.bind(this); + return send; +}; + +/** + * Should be called to create pure JSONRPC request which can be used in batch request + * + * @method request + * @param {...} params + * @return {Object} jsonrpc request + */ +Method.prototype.request = function () { + var payload = this.toPayload(Array.prototype.slice.call(arguments)); + payload.format = this.formatOutput.bind(this); + return payload; }; module.exports = Method; + diff --git a/lib/web3/property.js b/lib/web3/property.js index f7b3349..0e6e482 100644 --- a/lib/web3/property.js +++ b/lib/web3/property.js @@ -111,13 +111,33 @@ Property.prototype.buildGet = function () { Property.prototype.buildAsyncGet = function () { var property = this; - return function get(callback) { + var get = function (callback) { property.requestManager.sendAsync({ method: property.getter }, function (err, result) { callback(err, property.formatOutput(result)); }); }; + get.request = this.request.bind(this); + return get; +}; + +/** + * Should be called to create pure JSONRPC request which can be used in batch request + * + * @method request + * @param {...} params + * @return {Object} jsonrpc request + */ +Property.prototype.request = function () { + var payload = { + method: this.getter, + params: [], + callback: this.extractCallback(Array.prototype.slice.call(arguments)) + }; + payload.format = this.formatOutput.bind(this); + return payload; }; module.exports = Property; + diff --git a/lib/web3/requestmanager.js b/lib/web3/requestmanager.js index 4c10d0a..d27236d 100644 --- a/lib/web3/requestmanager.js +++ b/lib/web3/requestmanager.js @@ -36,26 +36,12 @@ var errors = require('./errors'); * Singleton */ var RequestManager = function (provider) { - // singleton pattern - //if (arguments.callee._singletonInstance) { - //return arguments.callee._singletonInstance; - //} - //arguments.callee._singletonInstance = this; - this.provider = provider; this.polls = {}; this.timeout = null; this.isPolling = false; }; -/** - * @return {RequestManager} singleton - */ -RequestManager.getInstance = function () { - var instance = new RequestManager(); - return instance; -}; - /** * Should be used to synchronously send request * diff --git a/test/batch.js b/test/batch.js index 2558787..5d2e3c4 100644 --- a/test/batch.js +++ b/test/batch.js @@ -5,7 +5,6 @@ var web3 = new Web3(); var FakeHttpProvider = require('./helpers/FakeHttpProvider'); var bn = require('bignumber.js'); -/* describe('lib/web3/batch', function () { describe('execute', function () { it('should execute batch request', function (done) { @@ -201,4 +200,3 @@ describe('lib/web3/batch', function () { }); }); -*/ diff --git a/test/requestmanager.js b/test/requestmanager.js index 5c951de..02a877e 100644 --- a/test/requestmanager.js +++ b/test/requestmanager.js @@ -10,8 +10,7 @@ describe('lib/web3/requestmanager', function () { describe('send', function () { it('should return expected result synchronously', function () { var provider = new FakeHttpProvider(); - var manager = RequestManager.getInstance(); - manager.setProvider(provider); + var manager = new RequestManager(provider); var expected = 'hello_world'; provider.injectResult(expected); @@ -25,8 +24,7 @@ describe('lib/web3/requestmanager', function () { it('should return expected result asynchronously', function (done) { var provider = new FakeHttpProvider(); - var manager = RequestManager.getInstance(); - manager.setProvider(provider); + var manager = new RequestManager(provider); var expected = 'hello_world'; provider.injectResult(expected);