aboutsummaryrefslogtreecommitdiffstats
path: root/lib/abi.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/abi.js')
-rw-r--r--lib/abi.js41
1 files changed, 23 insertions, 18 deletions
diff --git a/lib/abi.js b/lib/abi.js
index 8af10c382..5a01f43fd 100644
--- a/lib/abi.js
+++ b/lib/abi.js
@@ -23,10 +23,14 @@
// 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
+
+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) {
@@ -87,25 +91,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, round it down
/// @returns right-aligned byte representation of int
var formatInt = function (value) {
- var padding = 32 * 2;
- if (value instanceof BigNumber) {
+ var padding = ETH_PADDING * 2;
+ 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);
@@ -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;
@@ -177,12 +179,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);
};
@@ -237,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++) {