abi.js cleanup && new types.js, utils.js
This commit is contained in:
85
lib/abi.js
85
lib/abi.js
@ -26,6 +26,8 @@ if (process.env.NODE_ENV !== 'build') {
|
||||
}
|
||||
|
||||
var web3 = require('./web3');
|
||||
var utils = require('./utils');
|
||||
var types = require('./types');
|
||||
var f = require('./formatters');
|
||||
|
||||
BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
|
||||
@ -35,22 +37,9 @@ var ETH_PADDING = 32;
|
||||
/// method signature length in bytes
|
||||
var ETH_METHOD_SIGNATURE_LENGTH = 4;
|
||||
|
||||
/// Finds first index of array element matching pattern
|
||||
/// @param array
|
||||
/// @param callback pattern
|
||||
/// @returns index of element
|
||||
var findIndex = function (array, callback) {
|
||||
var end = false;
|
||||
var i = 0;
|
||||
for (; i < array.length && !end; i++) {
|
||||
end = callback(array[i]);
|
||||
}
|
||||
return end ? i - 1 : -1;
|
||||
};
|
||||
|
||||
/// @returns a function that is used as a pattern for 'findIndex'
|
||||
var findMethodIndex = function (json, methodName) {
|
||||
return findIndex(json, function (method) {
|
||||
return utils.findIndex(json, function (method) {
|
||||
return method.name === methodName;
|
||||
});
|
||||
};
|
||||
@ -81,31 +70,6 @@ var filterEvents = function (json) {
|
||||
});
|
||||
};
|
||||
|
||||
/// @param string string to be padded
|
||||
/// @param number of characters that result string should have
|
||||
/// @param sign, by default 0
|
||||
/// @returns right aligned string
|
||||
/// TODO: remove, it was moved to formatters.js
|
||||
var padLeft = function (string, chars, sign) {
|
||||
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
|
||||
};
|
||||
|
||||
/// @param expected type prefix (string)
|
||||
/// @returns function which checks if type has matching prefix. if yes, returns true, otherwise false
|
||||
var prefixedType = function (prefix) {
|
||||
return function (type) {
|
||||
return type.indexOf(prefix) === 0;
|
||||
};
|
||||
};
|
||||
|
||||
/// @param expected type name (string)
|
||||
/// @returns function which checks if type is matching expected one. if yes, returns true, otherwise false
|
||||
var namedType = function (name) {
|
||||
return function (type) {
|
||||
return name === type;
|
||||
};
|
||||
};
|
||||
|
||||
/// This method should be called if we want to check if givent type is an array type
|
||||
/// @returns true if it is, otherwise false
|
||||
var arrayType = function (type) {
|
||||
@ -119,23 +83,7 @@ var dynamicTypeBytes = function (type, value) {
|
||||
return "";
|
||||
};
|
||||
|
||||
/// Setups input formatters for solidity types
|
||||
/// @returns an array of input formatters
|
||||
var setupInputTypes = function () {
|
||||
|
||||
return [
|
||||
{ type: prefixedType('uint'), format: f.formatInputInt },
|
||||
{ type: prefixedType('int'), format: f.formatInputInt },
|
||||
{ type: prefixedType('hash'), format: f.formatInputInt },
|
||||
{ type: prefixedType('string'), format: f.formatInputString },
|
||||
{ type: prefixedType('real'), format: f.formatInputReal },
|
||||
{ type: prefixedType('ureal'), format: f.formatInputReal },
|
||||
{ type: namedType('address'), format: f.formatInputInt },
|
||||
{ type: namedType('bool'), format: f.formatInputBool }
|
||||
];
|
||||
};
|
||||
|
||||
var inputTypes = setupInputTypes();
|
||||
var inputTypes = types.inputTypes();
|
||||
|
||||
/// Formats input params to bytes
|
||||
/// @param contract json abi
|
||||
@ -183,23 +131,7 @@ var dynamicBytesLength = function (type) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
/// Setups output formaters for solidity types
|
||||
/// @returns an array of output formatters
|
||||
var setupOutputTypes = function () {
|
||||
|
||||
return [
|
||||
{ type: prefixedType('uint'), format: f.formatOutputUInt },
|
||||
{ type: prefixedType('int'), format: f.formatOutputInt },
|
||||
{ type: prefixedType('hash'), format: f.formatOutputHash },
|
||||
{ type: prefixedType('string'), format: f.formatOutputString },
|
||||
{ type: prefixedType('real'), format: f.formatOutputReal },
|
||||
{ type: prefixedType('ureal'), format: f.formatOutputUReal },
|
||||
{ type: namedType('address'), format: f.formatOutputAddress },
|
||||
{ type: namedType('bool'), format: f.formatOutputBool }
|
||||
];
|
||||
};
|
||||
|
||||
var outputTypes = setupOutputTypes();
|
||||
var outputTypes = types.outputTypes();
|
||||
|
||||
/// Formats output bytes back to param list
|
||||
/// @param contract json abi
|
||||
@ -241,7 +173,7 @@ var fromAbiOutput = function (json, methodName, output) {
|
||||
}
|
||||
result.push(array);
|
||||
}
|
||||
else if (prefixedType('string')(method.outputs[i].type)) {
|
||||
else if (types.prefixedType('string')(method.outputs[i].type)) {
|
||||
dynamicPart = dynamicPart.slice(padding);
|
||||
result.push(formatter(output.slice(0, padding)));
|
||||
output = output.slice(padding);
|
||||
@ -269,9 +201,10 @@ var methodTypeName = function (method) {
|
||||
|
||||
/// @param json abi for contract
|
||||
/// @returns input parser object for given json abi
|
||||
/// TODO: refactor creating the parser, do not double logic from contract
|
||||
var inputParser = function (json) {
|
||||
var parser = {};
|
||||
filterFunctions(json).forEach(function (method) {
|
||||
json.forEach(function (method) {
|
||||
var displayName = methodDisplayName(method.name);
|
||||
var typeName = methodTypeName(method.name);
|
||||
|
||||
@ -294,7 +227,7 @@ var inputParser = function (json) {
|
||||
/// @returns output parser for given json abi
|
||||
var outputParser = function (json) {
|
||||
var parser = {};
|
||||
filterFunctions(json).forEach(function (method) {
|
||||
json.forEach(function (method) {
|
||||
|
||||
var displayName = methodDisplayName(method.name);
|
||||
var typeName = methodTypeName(method.name);
|
||||
|
Reference in New Issue
Block a user