parsing real, ureal values on output
This commit is contained in:
parent
af54832d24
commit
6a58db66f7
|
@ -194,13 +194,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);
|
||||
|
@ -212,6 +218,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;
|
||||
|
@ -247,8 +263,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 }
|
||||
];
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
24
lib/abi.js
24
lib/abi.js
|
@ -193,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);
|
||||
|
@ -211,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;
|
||||
|
@ -246,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 }
|
||||
];
|
||||
|
|
|
@ -419,7 +419,6 @@ describe('abi', function() {
|
|||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('outputParser', function() {
|
||||
|
@ -673,12 +672,52 @@ describe('abi', function() {
|
|||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000001")[0], true);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000000")[0], false);
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], true);
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000000")[0], false);
|
||||
|
||||
|
||||
});
|
||||
|
||||
it('should parse output real', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'real' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000100000000000000000000000000000000")[0], 1);
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125);
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5);
|
||||
assert.equal(parser.test("0xffffffffffffffffffffffffffffffff00000000000000000000000000000000")[0], -1);
|
||||
|
||||
});
|
||||
|
||||
it('should parse output ureal', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'ureal' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000100000000000000000000000000000000")[0], 1);
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125);
|
||||
assert.equal(parser.test("0x0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5);
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('should parse multiple output strings', function() {
|
||||
|
||||
// given
|
||||
|
|
Loading…
Reference in New Issue