aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/const.js23
-rw-r--r--lib/utils.js31
-rw-r--r--lib/web3.js52
3 files changed, 54 insertions, 52 deletions
diff --git a/lib/const.js b/lib/const.js
index 22f6dc690..8a17b794d 100644
--- a/lib/const.js
+++ b/lib/const.js
@@ -25,9 +25,32 @@ if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js'); // jshint ignore:line
}
+var ETH_UNITS = [
+ 'wei',
+ 'Kwei',
+ 'Mwei',
+ 'Gwei',
+ 'szabo',
+ 'finney',
+ 'ether',
+ 'grand',
+ 'Mether',
+ 'Gether',
+ 'Tether',
+ 'Pether',
+ 'Eether',
+ 'Zether',
+ 'Yether',
+ 'Nether',
+ 'Dether',
+ 'Vether',
+ 'Uether'
+];
+
module.exports = {
ETH_PADDING: 32,
ETH_SIGNATURE_LENGTH: 4,
+ ETH_UNITS: ETH_UNITS,
ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN }
};
diff --git a/lib/utils.js b/lib/utils.js
index 5cd6ec8d6..e1265803c 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -20,6 +20,8 @@
* @date 2015
*/
+var c = require('./const');
+
/// Finds first index of array element matching pattern
/// @param array
/// @param callback pattern
@@ -101,6 +103,32 @@ var filterEvents = function (json) {
});
};
+/// used to transform value/string to eth string
+/// TODO: use BigNumber.js to parse int
+/// TODO: add tests for it!
+var 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 = c.ETH_UNITS;
+ while (val > 3000 && unit < units.length - 1)
+ {
+ val /= 1000;
+ unit++;
+ }
+ var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
+ var replaceFunction = function($0, $1, $2) {
+ return $1 + ',' + $2;
+ };
+
+ while (true) {
+ var o = s;
+ s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
+ if (o === s)
+ break;
+ }
+ return s + ' ' + units[unit];
+};
+
module.exports = {
findIndex: findIndex,
toAscii: toAscii,
@@ -108,6 +136,7 @@ module.exports = {
extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName,
filterFunctions: filterFunctions,
- filterEvents: filterEvents
+ filterEvents: filterEvents,
+ toEth: toEth
};
diff --git a/lib/web3.js b/lib/web3.js
index c3126afc4..e868e9412 100644
--- a/lib/web3.js
+++ b/lib/web3.js
@@ -29,28 +29,6 @@ if (process.env.NODE_ENV !== 'build') {
var utils = require('./utils');
-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 [
@@ -213,29 +191,7 @@ var web3 = {
},
/// 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 = ETH_UNITS;
- while (val > 3000 && unit < units.length - 1)
- {
- val /= 1000;
- unit++;
- }
- var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
- var replaceFunction = function($0, $1, $2) {
- return $1 + ',' + $2;
- };
-
- while (true) {
- var o = s;
- s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
- if (o === s)
- break;
- }
- return s + ' ' + units[unit];
- },
+ toEth: utils.toEth,
/// eth object prototype
eth: {
@@ -271,11 +227,6 @@ var web3 = {
return new web3.filter(filter, shhWatch);
}
},
-
- /// @returns true if provider is installed
- haveProvider: function() {
- return !!web3.provider.provider;
- }
};
/// setups all api methods
@@ -298,7 +249,6 @@ var shhWatch = {
setupMethods(shhWatch, shhWatchMethods());
web3.setProvider = function(provider) {
- //provider.onmessage = messageHandler; // there will be no async calls, to remove
web3.provider.set(provider);
};