From 0172939490f47f869b39b00d3f5f228b226ab170 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 16 Jan 2015 15:49:36 +0100 Subject: fixed #28 and other small node.js issues --- lib/abi.js | 4 ++-- lib/contract.js | 6 +----- lib/filter.js | 5 +---- lib/providermanager.js | 4 +--- lib/web3.js | 13 ++++--------- 5 files changed, 9 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/abi.js b/lib/abi.js index 8af10c382..fbd6effb6 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -23,10 +23,10 @@ // TODO: is these line is supposed to be here? if (process.env.NODE_ENV !== 'build') { - var web3 = require('./web3'); // jshint ignore:line + var BigNumber = require('bignumber.js'); // jshint ignore:line } -var BigNumber = require('bignumber.js'); +var web3 = require('./web3'); // jshint ignore:line // TODO: make these be actually accurate instead of falling back onto JS's doubles. var hexToDec = function (hex) { diff --git a/lib/contract.js b/lib/contract.js index 52ce08705..744fc88a4 100644 --- a/lib/contract.js +++ b/lib/contract.js @@ -20,11 +20,7 @@ * @date 2014 */ -// TODO: is these line is supposed to be here? -if (process.env.NODE_ENV !== 'build') { - var web3 = require('./web3'); // jshint ignore:line -} - +var web3 = require('./web3'); // jshint ignore:line var abi = require('./abi'); /// method signature length in bytes diff --git a/lib/filter.js b/lib/filter.js index 47f5a070c..4a82babb9 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -23,10 +23,7 @@ * @date 2014 */ -// TODO: is these line is supposed to be here? -if (process.env.NODE_ENV !== 'build') { - var web3 = require('./web3'); // jshint ignore:line -} +var web3 = require('./web3'); // jshint ignore:line /// should be used when we want to watch something /// it's using inner polling mechanism and is notified about changes diff --git a/lib/providermanager.js b/lib/providermanager.js index 00266154d..2697ebbd7 100644 --- a/lib/providermanager.js +++ b/lib/providermanager.js @@ -24,9 +24,7 @@ */ // TODO: is these line is supposed to be here? -if (process.env.NODE_ENV !== 'build') { - var web3 = require('./web3'); // jshint ignore:line -} +var web3 = require('./web3'); // jshint ignore:line /** * Provider manager object prototype diff --git a/lib/web3.js b/lib/web3.js index 25c2901a8..f071eea49 100644 --- a/lib/web3.js +++ b/lib/web3.js @@ -23,9 +23,6 @@ * @date 2014 */ -var Filter = require('./filter'); -var ProviderManager = require('./providermanager'); - /// 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 @@ -319,7 +316,7 @@ var web3 = { /// eth object prototype eth: { watch: function (params) { - return new Filter(params, ethWatch); + return new web3.filter(params, ethWatch); } }, @@ -329,7 +326,7 @@ var web3 = { /// shh object prototype shh: { watch: function (params) { - return new Filter(params, shhWatch); + return new web3.filter(params, shhWatch); } }, @@ -387,8 +384,6 @@ var shhWatch = { setupMethods(shhWatch, shhWatchMethods()); -web3.provider = new ProviderManager(); - web3.setProvider = function(provider) { provider.onmessage = messageHandler; web3.provider.set(provider); @@ -411,5 +406,5 @@ function messageHandler(data) { } } -if (typeof(module) !== "undefined") - module.exports = web3; +module.exports = web3; + -- cgit v1.2.3 From 774e9d24a14fd258bd832b10dab445ebaa792d29 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 16 Jan 2015 16:26:58 +0100 Subject: abi.js rounds down floating point input --- lib/abi.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/abi.js b/lib/abi.js index fbd6effb6..11141e28b 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -28,6 +28,8 @@ if (process.env.NODE_ENV !== 'build') { var web3 = require('./web3'); // jshint ignore:line +BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN }); + // TODO: make these be actually accurate instead of falling back onto JS's doubles. var hexToDec = function (hex) { return parseInt(hex, 16).toString(); @@ -87,25 +89,23 @@ var setupInputTypes = function () { /// Formats input value to byte representation of int /// If value is negative, return it's two's complement + /// If the value is floating point, it rounds it down /// @returns right-aligned byte representation of int var formatInt = function (value) { var padding = 32 * 2; - if (value instanceof BigNumber) { + if (value instanceof BigNumber || typeof value === 'number') { + if (typeof value === 'number') + value = new BigNumber(value); + value = value.round(); + if (value.lessThan(0)) - value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16); - else - value = value.toString(16); - } - else if (typeof value === 'number') { - if (value < 0) - value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16); - else - value = new BigNumber(value).toString(16); + value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1); + value = value.toString(16); } else if (value.indexOf('0x') === 0) value = value.substr(2); else if (typeof value === 'string') - value = new BigNumber(value).toString(16); + value = formatInt(new BigNumber(value)); else value = (+value).toString(16); return padLeft(value, padding); -- cgit v1.2.3 From a1c0bb68ddd7a032843d16ed083a951588281278 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 16 Jan 2015 16:40:26 +0100 Subject: fixed checking first bit for parsing int output --- lib/abi.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/abi.js b/lib/abi.js index 11141e28b..f94e97faf 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -89,7 +89,7 @@ var setupInputTypes = function () { /// Formats input value to byte representation of int /// If value is negative, return it's two's complement - /// If the value is floating point, it rounds it down + /// If the value is floating point, round it down /// @returns right-aligned byte representation of int var formatInt = function (value) { var padding = 32 * 2; @@ -177,12 +177,15 @@ var setupOutputTypes = function () { var formatInt = function (value) { // check if it's negative number // it it is, return two's complement - if (value.substr(0, 1).toLowerCase() === 'f') { + var firstBit = new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1); + if (firstBit === '1') { return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1); } return new BigNumber(value, 16); }; + /// Formats big right-aligned input bytes to uint + /// @returns right-aligned input bytes formatted to uint var formatUInt = function (value) { return new BigNumber(value, 16); }; -- cgit v1.2.3 From e94da808cb2a9f0493b42e5e572f6aed78de5ee3 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 16 Jan 2015 16:46:14 +0100 Subject: default padding set to 32, separated to one variable --- lib/abi.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/abi.js b/lib/abi.js index f94e97faf..5a01f43fd 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -30,6 +30,8 @@ var web3 = require('./web3'); // jshint ignore:line BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN }); +var ETH_PADDING = 32; + // TODO: make these be actually accurate instead of falling back onto JS's doubles. var hexToDec = function (hex) { return parseInt(hex, 16).toString(); @@ -92,7 +94,7 @@ var setupInputTypes = function () { /// If the value is floating point, round it down /// @returns right-aligned byte representation of int var formatInt = function (value) { - var padding = 32 * 2; + var padding = ETH_PADDING * 2; if (value instanceof BigNumber || typeof value === 'number') { if (typeof value === 'number') value = new BigNumber(value); @@ -114,7 +116,7 @@ var setupInputTypes = function () { /// Formats input value to byte representation of string /// @returns left-algined byte representation of string var formatString = function (value) { - return web3.fromAscii(value, 32).substr(2); + return web3.fromAscii(value, ETH_PADDING).substr(2); }; /// Formats input value to byte representation of bool @@ -151,7 +153,7 @@ var toAbiInput = function (json, methodName, params) { } var method = json[index]; - var padding = 32 * 2; + var padding = ETH_PADDING * 2; for (var i = 0; i < method.inputs.length; i++) { var typeMatch = false; @@ -240,7 +242,7 @@ var fromAbiOutput = function (json, methodName, output) { var result = []; var method = json[index]; - var padding = 32 * 2; + var padding = ETH_PADDING * 2; for (var i = 0; i < method.outputs.length; i++) { var typeMatch = false; for (var j = 0; j < outputTypes.length && !typeMatch; j++) { -- cgit v1.2.3