diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-22 04:35:15 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-22 04:35:15 +0800 |
commit | c2d9c1a6f12822a08300bbafb66227d8d33ae73b (patch) | |
tree | c7ceacc36e7ff80893394dc968bf4111d9adaf4e /lib/web3.js | |
parent | 81a58132709b683a61cbde3dab93bbeb11d8debb (diff) | |
download | go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.tar go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.tar.gz go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.tar.bz2 go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.tar.lz go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.tar.xz go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.tar.zst go-tangerine-c2d9c1a6f12822a08300bbafb66227d8d33ae73b.zip |
toDecimal/fromDecimal is using bignumber.js now
Diffstat (limited to 'lib/web3.js')
-rw-r--r-- | lib/web3.js | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/lib/web3.js b/lib/web3.js index cfe06763d..96002a232 100644 --- a/lib/web3.js +++ b/lib/web3.js @@ -23,6 +23,32 @@ * @date 2014 */ +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 [ @@ -160,16 +186,6 @@ var setupProperties = function (obj, properties) { }); }; -// TODO: import from a dependency, don't duplicate. -// TODO: use bignumber for that! -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: {}, @@ -216,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; |