aboutsummaryrefslogtreecommitdiffstats
path: root/lib/abi.js
diff options
context:
space:
mode:
authorMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-19 20:53:44 +0800
committerMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-19 20:53:44 +0800
commit83505e61f35eae3b491d8223444459b2faec41f0 (patch)
tree6d4dda23d22c6645b4cbb0bd503911e4c4bbd267 /lib/abi.js
parent2ce109eb53845ae4b3b8f025ab438f3436494a62 (diff)
parent6a58db66f7f42a49667bcc751418256441752279 (diff)
downloaddexon-83505e61f35eae3b491d8223444459b2faec41f0.tar
dexon-83505e61f35eae3b491d8223444459b2faec41f0.tar.gz
dexon-83505e61f35eae3b491d8223444459b2faec41f0.tar.bz2
dexon-83505e61f35eae3b491d8223444459b2faec41f0.tar.lz
dexon-83505e61f35eae3b491d8223444459b2faec41f0.tar.xz
dexon-83505e61f35eae3b491d8223444459b2faec41f0.tar.zst
dexon-83505e61f35eae3b491d8223444459b2faec41f0.zip
Merge commit '2b4d38b9bf059014596e1ab00c99dc2ad4ab3761' into ethereumjs
Diffstat (limited to 'lib/abi.js')
-rw-r--r--lib/abi.js35
1 files changed, 29 insertions, 6 deletions
diff --git a/lib/abi.js b/lib/abi.js
index 319b06066..72001eaa0 100644
--- a/lib/abi.js
+++ b/lib/abi.js
@@ -116,6 +116,13 @@ var formatInputBool = function (value) {
return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0');
};
+/// Formats input value to byte representation of real
+/// Values are multiplied by 2^m and encoded as integers
+/// @returns byte representation of real
+var formatInputReal = function (value) {
+ return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128)));
+};
+
var dynamicTypeBytes = function (type, value) {
// TODO: decide what to do with array of strings
if (arrayType(type) || prefixedType('string')(type))
@@ -132,8 +139,8 @@ var setupInputTypes = function () {
{ type: prefixedType('int'), format: formatInputInt },
{ type: prefixedType('hash'), format: formatInputInt },
{ type: prefixedType('string'), format: formatInputString },
- { type: prefixedType('real'), format: formatInputInt },
- { type: prefixedType('ureal'), format: formatInputInt },
+ { type: prefixedType('real'), format: formatInputReal },
+ { type: prefixedType('ureal'), format: formatInputReal },
{ type: namedType('address'), format: formatInputInt },
{ type: namedType('bool'), format: formatInputBool }
];
@@ -186,13 +193,19 @@ var toAbiInput = function (json, methodName, params) {
return bytes;
};
+/// Check if input value is negative
+/// @param value is hex format
+/// @returns true if it is negative, otherwise false
+var signedIsNegative = function (value) {
+ return (new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1)) === '1';
+};
+
/// Formats input right-aligned input bytes to int
/// @returns right-aligned input bytes formatted to int
var formatOutputInt = function (value) {
// check if it's negative number
// it it is, return two's complement
- var firstBit = new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1);
- if (firstBit === '1') {
+ if (signedIsNegative(value)) {
return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1);
}
return new BigNumber(value, 16);
@@ -204,6 +217,16 @@ var formatOutputUInt = function (value) {
return new BigNumber(value, 16);
};
+/// @returns input bytes formatted to real
+var formatOutputReal = function (value) {
+ return formatOutputInt(value).dividedBy(new BigNumber(2).pow(128));
+};
+
+/// @returns input bytes formatted to ureal
+var formatOutputUReal = function (value) {
+ return formatOutputUInt(value).dividedBy(new BigNumber(2).pow(128));
+};
+
/// @returns right-aligned input bytes formatted to hex
var formatOutputHash = function (value) {
return "0x" + value;
@@ -239,8 +262,8 @@ var setupOutputTypes = function () {
{ type: prefixedType('int'), format: formatOutputInt },
{ type: prefixedType('hash'), format: formatOutputHash },
{ type: prefixedType('string'), format: formatOutputString },
- { type: prefixedType('real'), format: formatOutputInt },
- { type: prefixedType('ureal'), format: formatOutputInt },
+ { type: prefixedType('real'), format: formatOutputReal },
+ { type: prefixedType('ureal'), format: formatOutputUReal },
{ type: namedType('address'), format: formatOutputAddress },
{ type: namedType('bool'), format: formatOutputBool }
];