diff options
author | Gav Wood <i@gavwood.com> | 2014-12-22 08:13:49 +0800 |
---|---|---|
committer | Gav Wood <i@gavwood.com> | 2014-12-22 08:13:49 +0800 |
commit | 123098ce79562be0d4ce87e15bcebe6a970f1ff6 (patch) | |
tree | fba808e0bf433cc209c7554f5aed744813a71574 /lib/main.js | |
parent | e9db2d1b1877bdf49cb76de46849a1426ded288c (diff) | |
download | go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.tar go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.tar.gz go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.tar.bz2 go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.tar.lz go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.tar.xz go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.tar.zst go-tangerine-123098ce79562be0d4ce87e15bcebe6a970f1ff6.zip |
Lots of fixes.
Diffstat (limited to 'lib/main.js')
-rw-r--r-- | lib/main.js | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/lib/main.js b/lib/main.js index 697cbdbc3..59c60cfa8 100644 --- a/lib/main.js +++ b/lib/main.js @@ -19,6 +19,7 @@ * Jeffrey Wilcke <jeff@ethdev.com> * Marek Kotewicz <marek@ethdev.com> * Marian Oancea <marian@ethdev.com> + * Gav Wood <g@ethdev.com> * @date 2014 */ @@ -61,17 +62,23 @@ function flattenPromise (obj) { return Promise.resolve(obj); } +var web3Methods = function () { + return [ + { name: 'sha3', call: 'web3_sha3' } + ]; +}; + var ethMethods = function () { var blockCall = function (args) { return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber"; }; var transactionCall = function (args) { - return typeof args[0] === "string" ? 'eth_transactionByHash' : 'eth_transactionByNumber'; + return typeof args[0] === "string" ? 'eth_transactionByHash' : 'eth_transactionByNumber'; }; var uncleCall = function (args) { - return typeof args[0] === "string" ? 'eth_uncleByHash' : 'eth_uncleByNumber'; + return typeof args[0] === "string" ? 'eth_uncleByHash' : 'eth_uncleByNumber'; }; var methods = [ @@ -205,19 +212,20 @@ 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); +}; + + var web3 = { _callbacks: {}, _events: {}, providers: {}, - toHex: function(str) { - var hex = ""; - for(var i = 0; i < str.length; i++) { - var n = str.charCodeAt(i).toString(16); - hex += n.length < 2 ? '0' + n : n; - } - - return hex; - }, toAscii: function(hex) { // Find termination @@ -237,10 +245,6 @@ var web3 = { return str; }, - toDecimal: function (val) { - return parseInt(val, 16); - }, - fromAscii: function(str, pad) { pad = pad === undefined ? 32 : pad; var hex = this.toHex(str); @@ -249,6 +253,33 @@ var web3 = { return "0x" + hex; }, + toDecimal: function (val) { + return hexToDec(val.substring(2)); + }, + + fromDecimal: function (val) { + return "0x" + decToHex(val); + }, + + 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' ]; + while (val > 3000 && unit < units.length - 1) + { + val /= 1000; + unit++; + } + var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2); + while (true) { + var o = s; + s = s.replace(/(\d)(\d\d\d[\.\,])/, function($0, $1, $2) { return $1 + ',' + $2; }); + if (o == s) + break; + } + return s + ' ' + units[unit]; + }, + eth: { prototype: Object(), // jshint ignore:line watch: function (params) { @@ -294,6 +325,7 @@ var web3 = { } }; +setupMethods(web3, web3Methods()); setupMethods(web3.eth, ethMethods()); setupProperties(web3.eth, ethProperties()); setupMethods(web3.db, dbMethods()); @@ -460,4 +492,3 @@ function messageHandler(data) { } module.exports = web3; - |