aboutsummaryrefslogtreecommitdiffstats
path: root/lib/web3.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/web3.js')
-rw-r--r--lib/web3.js174
1 files changed, 40 insertions, 134 deletions
diff --git a/lib/web3.js b/lib/web3.js
index f071eea49..96002a232 100644
--- a/lib/web3.js
+++ b/lib/web3.js
@@ -23,48 +23,32 @@
* @date 2014
*/
-/// Recursively resolves all promises in given object and replaces the resolved values with promises
-/// @param any object/array/promise/anything else..
-/// @returns (resolves) object with replaced promises with their result
-function flattenPromise (obj) {
- if (obj instanceof Promise) {
- return Promise.resolve(obj);
- }
-
- if (obj instanceof Array) {
- return new Promise(function (resolve) {
- var promises = obj.map(function (o) {
- return flattenPromise(o);
- });
-
- return Promise.all(promises).then(function (res) {
- for (var i = 0; i < obj.length; i++) {
- obj[i] = res[i];
- }
- resolve(obj);
- });
- });
- }
-
- if (obj instanceof Object) {
- return new Promise(function (resolve) {
- var keys = Object.keys(obj);
- var promises = keys.map(function (key) {
- return flattenPromise(obj[key]);
- });
-
- return Promise.all(promises).then(function (res) {
- for (var i = 0; i < keys.length; i++) {
- obj[keys[i]] = res[i];
- }
- resolve(obj);
- });
- });
- }
-
- return Promise.resolve(obj);
+if (process.env.NODE_ENV !== 'build') {
+ var BigNumber = require('bignumber.js');
}
+var ETH_UNITS = [
+ 'wei',
+ 'Kwei',
+ 'Mwei',
+ 'Gwei',
+ 'szabo',
+ 'finney',
+ 'ether',
+ 'grand',
+ 'Mether',
+ 'Gether',
+ 'Tether',
+ 'Pether',
+ 'Eether',
+ 'Zether',
+ 'Yether',
+ 'Nether',
+ 'Dether',
+ 'Vether',
+ 'Uether'
+];
+
/// @returns an array of objects describing web3 api methods
var web3Methods = function () {
return [
@@ -169,21 +153,11 @@ var shhWatchMethods = function () {
var setupMethods = function (obj, methods) {
methods.forEach(function (method) {
obj[method.name] = function () {
- return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) {
- var call = typeof method.call === "function" ? method.call(args) : method.call;
- return {call: call, args: args};
- }).then(function (request) {
- return new Promise(function (resolve, reject) {
- web3.provider.send(request, function (err, result) {
- if (!err) {
- resolve(result);
- return;
- }
- reject(err);
- });
- });
- }).catch(function(err) {
- console.error(err);
+ var args = Array.prototype.slice.call(arguments);
+ var call = typeof method.call === 'function' ? method.call(args) : method.call;
+ return web3.provider.send({
+ call: call,
+ args: args
});
};
});
@@ -195,30 +169,16 @@ var setupProperties = function (obj, properties) {
properties.forEach(function (property) {
var proto = {};
proto.get = function () {
- return new Promise(function(resolve, reject) {
- web3.provider.send({call: property.getter}, function(err, result) {
- if (!err) {
- resolve(result);
- return;
- }
- reject(err);
- });
+ return web3.provider.send({
+ call: property.getter
});
};
+
if (property.setter) {
proto.set = function (val) {
- return flattenPromise([val]).then(function (args) {
- return new Promise(function (resolve) {
- web3.provider.send({call: property.setter, args: args}, function (err, result) {
- if (!err) {
- resolve(result);
- return;
- }
- reject(err);
- });
- });
- }).catch(function (err) {
- console.error(err);
+ return web3.provider.send({
+ call: property.setter,
+ args: [val]
});
};
}
@@ -226,15 +186,6 @@ var setupProperties = function (obj, properties) {
});
};
-// TODO: import from a dependency, don't duplicate.
-var hexToDec = function (hex) {
- return parseInt(hex, 16).toString();
-};
-
-var decToHex = function (dec) {
- return parseInt(dec).toString(16);
-};
-
/// setups web3 object, and it's in-browser executed methods
var web3 = {
_callbacks: {},
@@ -281,19 +232,20 @@ var web3 = {
/// @returns decimal representaton of hex value prefixed by 0x
toDecimal: function (val) {
- return hexToDec(val.substring(2));
+ return (new BigNumber(val.substring(2), 16).toString(10));
},
/// @returns hex representation (prefixed by 0x) of decimal value
fromDecimal: function (val) {
- return "0x" + decToHex(val);
+ return "0x" + (new BigNumber(val).toString(16));
},
/// used to transform value/string to eth string
+ /// TODO: use BigNumber.js to parse int
toEth: function(str) {
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
var unit = 0;
- var units = [ 'wei', 'Kwei', 'Mwei', 'Gwei', 'szabo', 'finney', 'ether', 'grand', 'Mether', 'Gether', 'Tether', 'Pether', 'Eether', 'Zether', 'Yether', 'Nether', 'Dether', 'Vether', 'Uether' ];
+ var units = ETH_UNITS;
while (val > 3000 && unit < units.length - 1)
{
val /= 1000;
@@ -330,35 +282,6 @@ var web3 = {
}
},
- /// used by filter to register callback with given id
- on: function(event, id, cb) {
- if(web3._events[event] === undefined) {
- web3._events[event] = {};
- }
-
- web3._events[event][id] = cb;
- return this;
- },
-
- /// used by filter to unregister callback with given id
- off: function(event, id) {
- if(web3._events[event] !== undefined) {
- delete web3._events[event][id];
- }
-
- return this;
- },
-
- /// used to trigger callback registered by filter
- trigger: function(event, id, data) {
- var callbacks = web3._events[event];
- if (!callbacks || !callbacks[id]) {
- return;
- }
- var cb = callbacks[id];
- cb(data);
- },
-
/// @returns true if provider is installed
haveProvider: function() {
return !!web3.provider.provider;
@@ -385,26 +308,9 @@ var shhWatch = {
setupMethods(shhWatch, shhWatchMethods());
web3.setProvider = function(provider) {
- provider.onmessage = messageHandler;
+ //provider.onmessage = messageHandler; // there will be no async calls, to remove
web3.provider.set(provider);
- web3.provider.sendQueued();
};
-/// callled when there is new incoming message
-function messageHandler(data) {
- if(data._event !== undefined) {
- web3.trigger(data._event, data._id, data.data);
- return;
- }
-
- if(data._id) {
- var cb = web3._callbacks[data._id];
- if (cb) {
- cb.call(this, data.error, data.data);
- delete web3._callbacks[data._id];
- }
- }
-}
-
module.exports = web3;