mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 11:38:12 +00:00
commit
7949f6a3b1
37
dist/web3-light.js
vendored
37
dist/web3-light.js
vendored
@ -215,7 +215,7 @@ SolidityType.prototype.formatInput = function (param, arrayType) {
|
||||
}).reduce(function (acc, current) {
|
||||
acc.appendArrayElement(current);
|
||||
return acc;
|
||||
}, new SolidityParam('', f.formatInputInt(param.length).value));
|
||||
}, new SolidityParam(f.formatInputInt(param.length).value));
|
||||
}
|
||||
return this._inputFormatter(param);
|
||||
};
|
||||
@ -232,7 +232,7 @@ SolidityType.prototype.formatOutput = function (param, arrayType) {
|
||||
if (arrayType) {
|
||||
// let's assume, that we solidity will never return long arrays :P
|
||||
var result = [];
|
||||
var length = new BigNumber(param.prefix, 16);
|
||||
var length = new BigNumber(param.value, 16);
|
||||
for (var i = 0; i < length * 64; i += 64) {
|
||||
result.push(this._outputFormatter(new SolidityParam(param.suffix.slice(i, i + 64))));
|
||||
}
|
||||
@ -263,7 +263,7 @@ SolidityType.prototype.shiftParam = function (type, param) {
|
||||
if (this._mode === 'bytes') {
|
||||
return param.shiftBytes();
|
||||
} else if (isArrayType(type)) {
|
||||
var length = new BigNumber(param.prefix.slice(0, 64), 16);
|
||||
var length = new BigNumber(param.value.slice(0, 64), 16);
|
||||
return param.shiftArray(length);
|
||||
}
|
||||
return param.shiftValue();
|
||||
@ -305,17 +305,9 @@ SolidityCoder.prototype._requireType = function (type) {
|
||||
* @return {SolidityParam} SolidityParam for this group of params
|
||||
*/
|
||||
SolidityCoder.prototype._bytesToParam = function (types, bytes) {
|
||||
var self = this;
|
||||
var prefixTypes = types.reduce(function (acc, type) {
|
||||
return self._requireType(type).isVariadicType(type) ? acc + 1 : acc;
|
||||
}, 0);
|
||||
var valueTypes = types.length - prefixTypes;
|
||||
|
||||
var prefix = bytes.slice(0, prefixTypes * 64);
|
||||
bytes = bytes.slice(prefixTypes * 64);
|
||||
var value = bytes.slice(0, valueTypes * 64);
|
||||
var suffix = bytes.slice(valueTypes * 64);
|
||||
return new SolidityParam(value, prefix, suffix);
|
||||
var value = bytes.slice(0, types.length * 64);
|
||||
var suffix = bytes.slice(types.length * 64);
|
||||
return new SolidityParam(value, suffix);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -530,7 +522,7 @@ var formatInputBytes = function (value) {
|
||||
*/
|
||||
var formatInputDynamicBytes = function (value) {
|
||||
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
|
||||
return new SolidityParam('', formatInputInt(value.length).value, result);
|
||||
return new SolidityParam(formatInputInt(value.length).value, result);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -711,8 +703,7 @@ module.exports = {
|
||||
* SolidityParam object prototype.
|
||||
* Should be used when encoding, decoding solidity bytes
|
||||
*/
|
||||
var SolidityParam = function (value, prefix, suffix) {
|
||||
this.prefix = prefix || '';
|
||||
var SolidityParam = function (value, suffix) {
|
||||
this.value = value || '';
|
||||
this.suffix = suffix || '';
|
||||
};
|
||||
@ -724,7 +715,6 @@ var SolidityParam = function (value, prefix, suffix) {
|
||||
* @param {SolidityParam} param that it appended after this
|
||||
*/
|
||||
SolidityParam.prototype.append = function (param) {
|
||||
this.prefix += param.prefix;
|
||||
this.value += param.value;
|
||||
this.suffix += param.suffix;
|
||||
};
|
||||
@ -737,8 +727,7 @@ SolidityParam.prototype.append = function (param) {
|
||||
*/
|
||||
SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
this.suffix += param.value;
|
||||
this.prefix += param.prefix;
|
||||
// TODO: suffix not supported = it's required for nested arrays;
|
||||
//this.suffix += param.suffix; // we do not support nested dynamic types
|
||||
};
|
||||
|
||||
/**
|
||||
@ -748,7 +737,7 @@ SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
* @return {String} encoded param(s)
|
||||
*/
|
||||
SolidityParam.prototype.encode = function () {
|
||||
return this.prefix + this.value + this.suffix;
|
||||
return this.value + this.suffix;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -781,11 +770,11 @@ SolidityParam.prototype.shiftBytes = function () {
|
||||
* @return {SolidityParam} first array param
|
||||
*/
|
||||
SolidityParam.prototype.shiftArray = function (length) {
|
||||
var prefix = this.prefix.slice(0, 64);
|
||||
this.prefix = this.value.slice(64);
|
||||
var value = this.value.slice(0, 64);
|
||||
this.value = this.value.slice(64);
|
||||
var suffix = this.suffix.slice(0, 64 * length);
|
||||
this.suffix = this.suffix.slice(64 * length);
|
||||
return new SolidityParam('', prefix, suffix);
|
||||
return new SolidityParam(value, suffix);
|
||||
};
|
||||
|
||||
module.exports = SolidityParam;
|
||||
|
8
dist/web3-light.js.map
vendored
8
dist/web3-light.js.map
vendored
File diff suppressed because one or more lines are too long
4
dist/web3-light.min.js
vendored
4
dist/web3-light.min.js
vendored
File diff suppressed because one or more lines are too long
37
dist/web3.js
vendored
37
dist/web3.js
vendored
@ -215,7 +215,7 @@ SolidityType.prototype.formatInput = function (param, arrayType) {
|
||||
}).reduce(function (acc, current) {
|
||||
acc.appendArrayElement(current);
|
||||
return acc;
|
||||
}, new SolidityParam('', f.formatInputInt(param.length).value));
|
||||
}, new SolidityParam(f.formatInputInt(param.length).value));
|
||||
}
|
||||
return this._inputFormatter(param);
|
||||
};
|
||||
@ -232,7 +232,7 @@ SolidityType.prototype.formatOutput = function (param, arrayType) {
|
||||
if (arrayType) {
|
||||
// let's assume, that we solidity will never return long arrays :P
|
||||
var result = [];
|
||||
var length = new BigNumber(param.prefix, 16);
|
||||
var length = new BigNumber(param.value, 16);
|
||||
for (var i = 0; i < length * 64; i += 64) {
|
||||
result.push(this._outputFormatter(new SolidityParam(param.suffix.slice(i, i + 64))));
|
||||
}
|
||||
@ -263,7 +263,7 @@ SolidityType.prototype.shiftParam = function (type, param) {
|
||||
if (this._mode === 'bytes') {
|
||||
return param.shiftBytes();
|
||||
} else if (isArrayType(type)) {
|
||||
var length = new BigNumber(param.prefix.slice(0, 64), 16);
|
||||
var length = new BigNumber(param.value.slice(0, 64), 16);
|
||||
return param.shiftArray(length);
|
||||
}
|
||||
return param.shiftValue();
|
||||
@ -305,17 +305,9 @@ SolidityCoder.prototype._requireType = function (type) {
|
||||
* @return {SolidityParam} SolidityParam for this group of params
|
||||
*/
|
||||
SolidityCoder.prototype._bytesToParam = function (types, bytes) {
|
||||
var self = this;
|
||||
var prefixTypes = types.reduce(function (acc, type) {
|
||||
return self._requireType(type).isVariadicType(type) ? acc + 1 : acc;
|
||||
}, 0);
|
||||
var valueTypes = types.length - prefixTypes;
|
||||
|
||||
var prefix = bytes.slice(0, prefixTypes * 64);
|
||||
bytes = bytes.slice(prefixTypes * 64);
|
||||
var value = bytes.slice(0, valueTypes * 64);
|
||||
var suffix = bytes.slice(valueTypes * 64);
|
||||
return new SolidityParam(value, prefix, suffix);
|
||||
var value = bytes.slice(0, types.length * 64);
|
||||
var suffix = bytes.slice(types.length * 64);
|
||||
return new SolidityParam(value, suffix);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -530,7 +522,7 @@ var formatInputBytes = function (value) {
|
||||
*/
|
||||
var formatInputDynamicBytes = function (value) {
|
||||
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
|
||||
return new SolidityParam('', formatInputInt(value.length).value, result);
|
||||
return new SolidityParam(formatInputInt(value.length).value, result);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -711,8 +703,7 @@ module.exports = {
|
||||
* SolidityParam object prototype.
|
||||
* Should be used when encoding, decoding solidity bytes
|
||||
*/
|
||||
var SolidityParam = function (value, prefix, suffix) {
|
||||
this.prefix = prefix || '';
|
||||
var SolidityParam = function (value, suffix) {
|
||||
this.value = value || '';
|
||||
this.suffix = suffix || '';
|
||||
};
|
||||
@ -724,7 +715,6 @@ var SolidityParam = function (value, prefix, suffix) {
|
||||
* @param {SolidityParam} param that it appended after this
|
||||
*/
|
||||
SolidityParam.prototype.append = function (param) {
|
||||
this.prefix += param.prefix;
|
||||
this.value += param.value;
|
||||
this.suffix += param.suffix;
|
||||
};
|
||||
@ -737,8 +727,7 @@ SolidityParam.prototype.append = function (param) {
|
||||
*/
|
||||
SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
this.suffix += param.value;
|
||||
this.prefix += param.prefix;
|
||||
// TODO: suffix not supported = it's required for nested arrays;
|
||||
//this.suffix += param.suffix; // we do not support nested dynamic types
|
||||
};
|
||||
|
||||
/**
|
||||
@ -748,7 +737,7 @@ SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
* @return {String} encoded param(s)
|
||||
*/
|
||||
SolidityParam.prototype.encode = function () {
|
||||
return this.prefix + this.value + this.suffix;
|
||||
return this.value + this.suffix;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -781,11 +770,11 @@ SolidityParam.prototype.shiftBytes = function () {
|
||||
* @return {SolidityParam} first array param
|
||||
*/
|
||||
SolidityParam.prototype.shiftArray = function (length) {
|
||||
var prefix = this.prefix.slice(0, 64);
|
||||
this.prefix = this.value.slice(64);
|
||||
var value = this.value.slice(0, 64);
|
||||
this.value = this.value.slice(64);
|
||||
var suffix = this.suffix.slice(0, 64 * length);
|
||||
this.suffix = this.suffix.slice(64 * length);
|
||||
return new SolidityParam('', prefix, suffix);
|
||||
return new SolidityParam(value, suffix);
|
||||
};
|
||||
|
||||
module.exports = SolidityParam;
|
||||
|
8
dist/web3.js.map
vendored
8
dist/web3.js.map
vendored
File diff suppressed because one or more lines are too long
4
dist/web3.min.js
vendored
4
dist/web3.min.js
vendored
File diff suppressed because one or more lines are too long
@ -79,7 +79,7 @@ SolidityType.prototype.formatInput = function (param, arrayType) {
|
||||
}).reduce(function (acc, current) {
|
||||
acc.appendArrayElement(current);
|
||||
return acc;
|
||||
}, new SolidityParam('', f.formatInputInt(param.length).value));
|
||||
}, new SolidityParam(f.formatInputInt(param.length).value));
|
||||
}
|
||||
return this._inputFormatter(param);
|
||||
};
|
||||
@ -96,7 +96,7 @@ SolidityType.prototype.formatOutput = function (param, arrayType) {
|
||||
if (arrayType) {
|
||||
// let's assume, that we solidity will never return long arrays :P
|
||||
var result = [];
|
||||
var length = new BigNumber(param.prefix, 16);
|
||||
var length = new BigNumber(param.value, 16);
|
||||
for (var i = 0; i < length * 64; i += 64) {
|
||||
result.push(this._outputFormatter(new SolidityParam(param.suffix.slice(i, i + 64))));
|
||||
}
|
||||
@ -127,7 +127,7 @@ SolidityType.prototype.shiftParam = function (type, param) {
|
||||
if (this._mode === 'bytes') {
|
||||
return param.shiftBytes();
|
||||
} else if (isArrayType(type)) {
|
||||
var length = new BigNumber(param.prefix.slice(0, 64), 16);
|
||||
var length = new BigNumber(param.value.slice(0, 64), 16);
|
||||
return param.shiftArray(length);
|
||||
}
|
||||
return param.shiftValue();
|
||||
@ -169,17 +169,9 @@ SolidityCoder.prototype._requireType = function (type) {
|
||||
* @return {SolidityParam} SolidityParam for this group of params
|
||||
*/
|
||||
SolidityCoder.prototype._bytesToParam = function (types, bytes) {
|
||||
var self = this;
|
||||
var prefixTypes = types.reduce(function (acc, type) {
|
||||
return self._requireType(type).isVariadicType(type) ? acc + 1 : acc;
|
||||
}, 0);
|
||||
var valueTypes = types.length - prefixTypes;
|
||||
|
||||
var prefix = bytes.slice(0, prefixTypes * 64);
|
||||
bytes = bytes.slice(prefixTypes * 64);
|
||||
var value = bytes.slice(0, valueTypes * 64);
|
||||
var suffix = bytes.slice(valueTypes * 64);
|
||||
return new SolidityParam(value, prefix, suffix);
|
||||
var value = bytes.slice(0, types.length * 64);
|
||||
var suffix = bytes.slice(types.length * 64);
|
||||
return new SolidityParam(value, suffix);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -63,7 +63,7 @@ var formatInputBytes = function (value) {
|
||||
*/
|
||||
var formatInputDynamicBytes = function (value) {
|
||||
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
|
||||
return new SolidityParam('', formatInputInt(value.length).value, result);
|
||||
return new SolidityParam(formatInputInt(value.length).value, result);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -24,8 +24,7 @@
|
||||
* SolidityParam object prototype.
|
||||
* Should be used when encoding, decoding solidity bytes
|
||||
*/
|
||||
var SolidityParam = function (value, prefix, suffix) {
|
||||
this.prefix = prefix || '';
|
||||
var SolidityParam = function (value, suffix) {
|
||||
this.value = value || '';
|
||||
this.suffix = suffix || '';
|
||||
};
|
||||
@ -37,7 +36,6 @@ var SolidityParam = function (value, prefix, suffix) {
|
||||
* @param {SolidityParam} param that it appended after this
|
||||
*/
|
||||
SolidityParam.prototype.append = function (param) {
|
||||
this.prefix += param.prefix;
|
||||
this.value += param.value;
|
||||
this.suffix += param.suffix;
|
||||
};
|
||||
@ -50,8 +48,7 @@ SolidityParam.prototype.append = function (param) {
|
||||
*/
|
||||
SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
this.suffix += param.value;
|
||||
this.prefix += param.prefix;
|
||||
// TODO: suffix not supported = it's required for nested arrays;
|
||||
//this.suffix += param.suffix; // we do not support nested dynamic types
|
||||
};
|
||||
|
||||
/**
|
||||
@ -61,7 +58,7 @@ SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
* @return {String} encoded param(s)
|
||||
*/
|
||||
SolidityParam.prototype.encode = function () {
|
||||
return this.prefix + this.value + this.suffix;
|
||||
return this.value + this.suffix;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -94,11 +91,11 @@ SolidityParam.prototype.shiftBytes = function () {
|
||||
* @return {SolidityParam} first array param
|
||||
*/
|
||||
SolidityParam.prototype.shiftArray = function (length) {
|
||||
var prefix = this.prefix.slice(0, 64);
|
||||
this.prefix = this.value.slice(64);
|
||||
var value = this.value.slice(0, 64);
|
||||
this.value = this.value.slice(64);
|
||||
var suffix = this.suffix.slice(0, 64 * length);
|
||||
this.suffix = this.suffix.slice(64 * length);
|
||||
return new SolidityParam('', prefix, suffix);
|
||||
return new SolidityParam(value, suffix);
|
||||
};
|
||||
|
||||
module.exports = SolidityParam;
|
||||
|
@ -1,515 +0,0 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var BigNumber = require('bignumber.js');
|
||||
var abi = require('../lib/solidity/abi');
|
||||
var clone = function (object) { return JSON.parse(JSON.stringify(object)); };
|
||||
|
||||
var description = [{
|
||||
"name": "test",
|
||||
"type": "function",
|
||||
"inputs": [{
|
||||
"name": "a",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "d",
|
||||
"type": "uint256"
|
||||
}
|
||||
]
|
||||
}];
|
||||
|
||||
describe('lib/solidity/abi', function () {
|
||||
describe('inputParser', function () {
|
||||
it('should parse input uint', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "uint" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
|
||||
assert.equal(
|
||||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input uint128', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "uint128" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
|
||||
assert.equal(
|
||||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input uint256', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "uint256" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
|
||||
assert.equal(
|
||||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input int', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
|
||||
assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||
assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
|
||||
assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0");
|
||||
assert.equal(
|
||||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
});
|
||||
|
||||
it('should parse input int128', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int128" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
|
||||
assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||
assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
|
||||
assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0");
|
||||
assert.equal(
|
||||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input int256', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int256" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
|
||||
assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||
assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
|
||||
assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0");
|
||||
assert.equal(
|
||||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)),
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
);
|
||||
assert.equal(parser.test(0.1), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test(3.9), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
assert.equal(parser.test('0.1'), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
assert.equal(parser.test('3.9'), "0000000000000000000000000000000000000000000000000000000000000003");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input bool', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: 'bool' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(true), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.test(false), "0000000000000000000000000000000000000000000000000000000000000000");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input address', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "address" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d)
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0x407d73d8a49eeb85d32cf465507dd71d507100c1"), "000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input fixed bytes type', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "bytes" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test('hello'),
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
assert.equal(
|
||||
parser.test('world'),
|
||||
"0000000000000000000000000000000000000000000000000000000000000005776f726c64000000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse input int followed by a fixed bytes type', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int" },
|
||||
{ type: "bytes" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test(9, 'hello'),
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000009" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse input fixed bytes type followed by an int', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "bytes" },
|
||||
{ type: "int" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test('hello', 9),
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000009" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
});
|
||||
|
||||
it('should use proper method name', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].name = 'helloworld(int)';
|
||||
d[0].inputs = [
|
||||
{ type: "int" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.helloworld(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(parser.helloworld['int'](1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
|
||||
});
|
||||
|
||||
it('should parse multiple methods', function () {
|
||||
|
||||
// given
|
||||
var d = [{
|
||||
name: "test",
|
||||
type: "function",
|
||||
inputs: [{ type: "int" }],
|
||||
outputs: [{ type: "int" }]
|
||||
},{
|
||||
name: "test2",
|
||||
type: "function",
|
||||
inputs: [{ type: "bytes" }],
|
||||
outputs: [{ type: "bytes" }]
|
||||
}];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
//then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||
assert.equal(
|
||||
parser.test2('hello'),
|
||||
"000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
it('should parse input array of ints', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int[]" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test([5, 6]),
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000006"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse an array followed by an int', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int[]" },
|
||||
{ type: "int" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test([5, 6], 3),
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000006"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse an int followed by an array', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int" },
|
||||
{ type: "int[]" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test(3, [5, 6]),
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000006"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse mixture of arrays and ints', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: "int" },
|
||||
{ type: "int[]" },
|
||||
{ type: "int" },
|
||||
{ type: "int[]" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test(3, [5, 6, 1, 2], 7, [8, 9]),
|
||||
"0000000000000000000000000000000000000000000000000000000000000004" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000007" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000008" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000009"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse input real', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: 'real' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000100000000000000000000000000000000");
|
||||
assert.equal(parser.test(2.125), "0000000000000000000000000000000220000000000000000000000000000000");
|
||||
assert.equal(parser.test(8.5), "0000000000000000000000000000000880000000000000000000000000000000");
|
||||
assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffff00000000000000000000000000000000");
|
||||
|
||||
});
|
||||
|
||||
it('should parse input ureal', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].inputs = [
|
||||
{ type: 'ureal' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(1), "0000000000000000000000000000000100000000000000000000000000000000");
|
||||
assert.equal(parser.test(2.125), "0000000000000000000000000000000220000000000000000000000000000000");
|
||||
assert.equal(parser.test(8.5), "0000000000000000000000000000000880000000000000000000000000000000");
|
||||
|
||||
});
|
||||
|
||||
it('should throw an incorrect type error', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].inputs = [
|
||||
{ type: 'uin' }
|
||||
]
|
||||
|
||||
// when
|
||||
var parser = abi.inputParser(d);
|
||||
|
||||
// then
|
||||
assert.throws(function () {parser.test('0x')}, Error);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
});
|
@ -1,419 +0,0 @@
|
||||
var assert = require('assert');
|
||||
var BigNumber = require('bignumber.js');
|
||||
var abi = require('../lib/solidity/abi.js');
|
||||
var clone = function (object) { return JSON.parse(JSON.stringify(object)); };
|
||||
|
||||
var description = [{
|
||||
"name": "test",
|
||||
"type": "function",
|
||||
"inputs": [{
|
||||
"name": "a",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "d",
|
||||
"type": "uint256"
|
||||
}
|
||||
]
|
||||
}];
|
||||
|
||||
describe('lib/solidity/abi', function() {
|
||||
describe('outputParser', function() {
|
||||
it('should parse output fixed bytes type', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: "bytes" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test(
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000")[0],
|
||||
'hello'
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"776f726c64000000000000000000000000000000000000000000000000000000")[0],
|
||||
'world'
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
it('should parse output uint', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'uint' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
|
||||
assert.equal(
|
||||
parser.test("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10),
|
||||
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10)
|
||||
);
|
||||
assert.equal(
|
||||
parser.test("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10),
|
||||
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10)
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse output uint256', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'uint256' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
|
||||
assert.equal(
|
||||
parser.test("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10),
|
||||
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10)
|
||||
);
|
||||
assert.equal(
|
||||
parser.test("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10),
|
||||
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10)
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse output uint128', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'uint128' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
|
||||
assert.equal(
|
||||
parser.test("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10),
|
||||
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10)
|
||||
);
|
||||
assert.equal(
|
||||
parser.test("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10),
|
||||
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10)
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse output int', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'int' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
|
||||
assert.equal(parser.test("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1);
|
||||
assert.equal(parser.test("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16);
|
||||
});
|
||||
|
||||
it('should parse output int256', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'int256' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
|
||||
assert.equal(parser.test("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1);
|
||||
assert.equal(parser.test("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16);
|
||||
});
|
||||
|
||||
it('should parse output int128', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'int128' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test("000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
|
||||
assert.equal(parser.test("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1);
|
||||
assert.equal(parser.test("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16);
|
||||
});
|
||||
|
||||
it('should parse output address', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'address' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test("000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0],
|
||||
"0x407d73d8a49eeb85d32cf465507dd71d507100c1"
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse output bool', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: 'bool' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], true);
|
||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000000")[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("0000000000000000000000000000000100000000000000000000000000000000")[0], 1);
|
||||
assert.equal(parser.test("0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125);
|
||||
assert.equal(parser.test("0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5);
|
||||
assert.equal(parser.test("ffffffffffffffffffffffffffffffff00000000000000000000000000000000")[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("0000000000000000000000000000000100000000000000000000000000000000")[0], 1);
|
||||
assert.equal(parser.test("0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125);
|
||||
assert.equal(parser.test("0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5);
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('should parse multiple output fixed bytes type', function() {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
|
||||
d[0].outputs = [
|
||||
{ type: "bytes" },
|
||||
{ type: "bytes" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(
|
||||
parser.test(
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000" +
|
||||
"776f726c64000000000000000000000000000000000000000000000000000000")[0],
|
||||
'hello'
|
||||
);
|
||||
assert.equal(
|
||||
parser.test(
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000" +
|
||||
"776f726c64000000000000000000000000000000000000000000000000000000")[1],
|
||||
'world'
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
it('should use proper method name', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].name = 'helloworld(int)';
|
||||
d[0].outputs = [
|
||||
{ type: "int" }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.helloworld("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.helloworld['int']("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('should parse multiple methods', function () {
|
||||
|
||||
// given
|
||||
var d = [{
|
||||
name: "test",
|
||||
type: "function",
|
||||
inputs: [{ type: "int" }],
|
||||
outputs: [{ type: "int" }]
|
||||
},{
|
||||
name: "test2",
|
||||
type: "function",
|
||||
inputs: [{ type: "bytes" }],
|
||||
outputs: [{ type: "bytes" }]
|
||||
}];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
//then
|
||||
assert.equal(parser.test("00000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||
assert.equal(parser.test2(
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000")[0],
|
||||
"hello"
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
it('should parse output array', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].outputs = [
|
||||
{ type: 'int[]' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test(
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000006")[0][0],
|
||||
5
|
||||
);
|
||||
assert.equal(parser.test(
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||
"0000000000000000000000000000000000000000000000000000000000000006")[0][1],
|
||||
6
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
it('should parse 0x0 value', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].outputs = [
|
||||
{ type: 'int' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0x0")[0], 0);
|
||||
|
||||
});
|
||||
|
||||
it('should parse 0x0 value', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].outputs = [
|
||||
{ type: 'uint' }
|
||||
];
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.equal(parser.test("0x0")[0], 0);
|
||||
|
||||
});
|
||||
|
||||
it('should throw an incorrect type error', function () {
|
||||
|
||||
// given
|
||||
var d = clone(description);
|
||||
d[0].outputs = [
|
||||
{ type: 'uin' }
|
||||
]
|
||||
|
||||
// when
|
||||
var parser = abi.outputParser(d);
|
||||
|
||||
// then
|
||||
assert.throws(function () {parser.test('0x')}, Error);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
@ -32,6 +32,17 @@ describe('lib/solidity/coder', function () {
|
||||
'0000000000000000000000000000000000000000000000000000000000000001' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003'});
|
||||
test({ type: 'bool', expected: true, value: '0000000000000000000000000000000000000000000000000000000000000001'});
|
||||
test({ type: 'bool', expected: false, value: '0000000000000000000000000000000000000000000000000000000000000000'});
|
||||
test({ type: 'real', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'});
|
||||
test({ type: 'real', expected: new bn(2.125), value: '0000000000000000000000000000000220000000000000000000000000000000'});
|
||||
test({ type: 'real', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
|
||||
test({ type: 'real', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
|
||||
test({ type: 'ureal', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'});
|
||||
test({ type: 'ureal', expected: new bn(2.125), value: '0000000000000000000000000000000220000000000000000000000000000000'});
|
||||
test({ type: 'ureal', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
|
||||
test({ type: 'address', expected: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
|
||||
value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
|
||||
});
|
||||
});
|
||||
|
||||
@ -53,12 +64,12 @@ describe('lib/solidity/coder', function () {
|
||||
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
|
||||
test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], expected: [new bn(1), 'gavofyork', new bn(2), new bn(3), new bn(4),
|
||||
[new bn(5), new bn(6), new bn(7)]],
|
||||
values: '0000000000000000000000000000000000000000000000000000000000000009' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000001' +
|
||||
values: '0000000000000000000000000000000000000000000000000000000000000001' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000009' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000004' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003' +
|
||||
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000005' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000006' +
|
||||
|
@ -15,6 +15,8 @@ describe('lib/solidity/coder', function () {
|
||||
test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
|
||||
test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||
test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
|
||||
test({ type: 'int', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
|
||||
test({ type: 'int', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'});
|
||||
test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
|
||||
test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
|
||||
test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
|
||||
@ -29,6 +31,17 @@ describe('lib/solidity/coder', function () {
|
||||
'0000000000000000000000000000000000000000000000000000000000000001' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003'});
|
||||
test({ type: 'bool', value: true, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
|
||||
test({ type: 'bool', value: false, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
|
||||
test({ type: 'address', value: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
|
||||
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
|
||||
test({ type: 'real', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
|
||||
test({ type: 'real', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
|
||||
test({ type: 'real', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
|
||||
test({ type: 'real', value: -1, expected: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
|
||||
test({ type: 'ureal', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
|
||||
test({ type: 'ureal', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
|
||||
test({ type: 'ureal', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
|
||||
});
|
||||
});
|
||||
|
||||
@ -70,16 +83,16 @@ describe('lib/solidity/coder', function () {
|
||||
'0000000000000000000000000000000000000000000000000000000000000005' +
|
||||
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
|
||||
test({ types: ['int', 'bytes'], values: [5, 'gavofyork'],
|
||||
expected: '0000000000000000000000000000000000000000000000000000000000000009' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000005' +
|
||||
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000009' +
|
||||
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
|
||||
test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
|
||||
expected: '0000000000000000000000000000000000000000000000000000000000000009' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000001' +
|
||||
expected: '0000000000000000000000000000000000000000000000000000000000000001' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000009' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000002' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000004' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000003' +
|
||||
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000005' +
|
||||
'0000000000000000000000000000000000000000000000000000000000000006' +
|
||||
|
Loading…
x
Reference in New Issue
Block a user