mirror of https://github.com/status-im/web3.js.git
code comments
This commit is contained in:
parent
b8505d1761
commit
60ae274f34
|
@ -27,29 +27,43 @@ var types = require('./types');
|
||||||
var c = require('./config');
|
var c = require('./config');
|
||||||
var f = require('./formatters');
|
var f = require('./formatters');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays error about incorrect type
|
||||||
|
* @param {String} type
|
||||||
|
*/
|
||||||
var displayTypeError = function (type) {
|
var displayTypeError = function (type) {
|
||||||
console.error('parser does not support type: ' + type);
|
console.error('parser does not support type: ' + type);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This method should be called if we want to check if givent type is an array 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
|
* @param {String} type name
|
||||||
var arrayType = function (type) {
|
* @returns {Boolean} true if it is, otherwise false
|
||||||
|
*/
|
||||||
|
var isArrayType = function (type) {
|
||||||
return type.slice(-2) === '[]';
|
return type.slice(-2) === '[]';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method should be called to return dynamic type length in hex
|
||||||
|
* @param {String} type
|
||||||
|
* @param {String|Array} dynamic type
|
||||||
|
* @return {String} length of dynamic type in hex or empty string if type is not dynamic
|
||||||
|
*/
|
||||||
var dynamicTypeBytes = function (type, value) {
|
var dynamicTypeBytes = function (type, value) {
|
||||||
// TODO: decide what to do with array of strings
|
// TODO: decide what to do with array of strings
|
||||||
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
||||||
return f.formatInputInt(value.length);
|
return f.formatInputInt(value.length);
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
|
||||||
var inputTypes = types.inputTypes();
|
var inputTypes = types.inputTypes();
|
||||||
|
|
||||||
/// Formats input params to bytes
|
/**
|
||||||
/// @param abi contract method inputs
|
* Formats input params to bytes
|
||||||
/// @param array of params that will be formatted to bytes
|
* @param {Array} abi inputs of method
|
||||||
/// @returns bytes representation of input params
|
* @param {Array} params that will be formatted to bytes
|
||||||
|
* @returns bytes representation of input params
|
||||||
|
*/
|
||||||
var formatInput = function (inputs, params) {
|
var formatInput = function (inputs, params) {
|
||||||
var bytes = "";
|
var bytes = "";
|
||||||
var toAppendConstant = "";
|
var toAppendConstant = "";
|
||||||
|
@ -72,7 +86,7 @@ var formatInput = function (inputs, params) {
|
||||||
|
|
||||||
var formatter = inputTypes[j - 1].format;
|
var formatter = inputTypes[j - 1].format;
|
||||||
|
|
||||||
if (arrayType(inputs[i].type))
|
if (isArrayType(inputs[i].type))
|
||||||
toAppendArrayContent += params[i].reduce(function (acc, curr) {
|
toAppendArrayContent += params[i].reduce(function (acc, curr) {
|
||||||
return acc + formatter(curr);
|
return acc + formatter(curr);
|
||||||
}, "");
|
}, "");
|
||||||
|
@ -87,8 +101,14 @@ var formatInput = function (inputs, params) {
|
||||||
return bytes;
|
return bytes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method should be called to predict the length of dynamic type
|
||||||
|
*
|
||||||
|
* @param {String} type
|
||||||
|
* @returns {Number} length of dynamic type, 0 or multiplication of ETH_PADDING (32)
|
||||||
|
*/
|
||||||
var dynamicBytesLength = function (type) {
|
var dynamicBytesLength = function (type) {
|
||||||
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
||||||
return c.ETH_PADDING * 2;
|
return c.ETH_PADDING * 2;
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
@ -124,7 +144,7 @@ var formatOutput = function (outs, output) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var formatter = outputTypes[j - 1].format;
|
var formatter = outputTypes[j - 1].format;
|
||||||
if (arrayType(outs[i].type)) {
|
if (isArrayType(outs[i].type)) {
|
||||||
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
|
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
|
||||||
dynamicPart = dynamicPart.slice(padding);
|
dynamicPart = dynamicPart.slice(padding);
|
||||||
var array = [];
|
var array = [];
|
||||||
|
|
File diff suppressed because one or more lines are too long
42
lib/abi.js
42
lib/abi.js
|
@ -26,29 +26,43 @@ var types = require('./types');
|
||||||
var c = require('./config');
|
var c = require('./config');
|
||||||
var f = require('./formatters');
|
var f = require('./formatters');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays error about incorrect type
|
||||||
|
* @param {String} type
|
||||||
|
*/
|
||||||
var displayTypeError = function (type) {
|
var displayTypeError = function (type) {
|
||||||
console.error('parser does not support type: ' + type);
|
console.error('parser does not support type: ' + type);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This method should be called if we want to check if givent type is an array 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
|
* @param {String} type name
|
||||||
var arrayType = function (type) {
|
* @returns {Boolean} true if it is, otherwise false
|
||||||
|
*/
|
||||||
|
var isArrayType = function (type) {
|
||||||
return type.slice(-2) === '[]';
|
return type.slice(-2) === '[]';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method should be called to return dynamic type length in hex
|
||||||
|
* @param {String} type
|
||||||
|
* @param {String|Array} dynamic type
|
||||||
|
* @return {String} length of dynamic type in hex or empty string if type is not dynamic
|
||||||
|
*/
|
||||||
var dynamicTypeBytes = function (type, value) {
|
var dynamicTypeBytes = function (type, value) {
|
||||||
// TODO: decide what to do with array of strings
|
// TODO: decide what to do with array of strings
|
||||||
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
||||||
return f.formatInputInt(value.length);
|
return f.formatInputInt(value.length);
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
|
||||||
var inputTypes = types.inputTypes();
|
var inputTypes = types.inputTypes();
|
||||||
|
|
||||||
/// Formats input params to bytes
|
/**
|
||||||
/// @param abi contract method inputs
|
* Formats input params to bytes
|
||||||
/// @param array of params that will be formatted to bytes
|
* @param {Array} abi inputs of method
|
||||||
/// @returns bytes representation of input params
|
* @param {Array} params that will be formatted to bytes
|
||||||
|
* @returns bytes representation of input params
|
||||||
|
*/
|
||||||
var formatInput = function (inputs, params) {
|
var formatInput = function (inputs, params) {
|
||||||
var bytes = "";
|
var bytes = "";
|
||||||
var toAppendConstant = "";
|
var toAppendConstant = "";
|
||||||
|
@ -71,7 +85,7 @@ var formatInput = function (inputs, params) {
|
||||||
|
|
||||||
var formatter = inputTypes[j - 1].format;
|
var formatter = inputTypes[j - 1].format;
|
||||||
|
|
||||||
if (arrayType(inputs[i].type))
|
if (isArrayType(inputs[i].type))
|
||||||
toAppendArrayContent += params[i].reduce(function (acc, curr) {
|
toAppendArrayContent += params[i].reduce(function (acc, curr) {
|
||||||
return acc + formatter(curr);
|
return acc + formatter(curr);
|
||||||
}, "");
|
}, "");
|
||||||
|
@ -86,8 +100,14 @@ var formatInput = function (inputs, params) {
|
||||||
return bytes;
|
return bytes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method should be called to predict the length of dynamic type
|
||||||
|
*
|
||||||
|
* @param {String} type
|
||||||
|
* @returns {Number} length of dynamic type, 0 or multiplication of ETH_PADDING (32)
|
||||||
|
*/
|
||||||
var dynamicBytesLength = function (type) {
|
var dynamicBytesLength = function (type) {
|
||||||
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
if (isArrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
|
||||||
return c.ETH_PADDING * 2;
|
return c.ETH_PADDING * 2;
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
@ -123,7 +143,7 @@ var formatOutput = function (outs, output) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var formatter = outputTypes[j - 1].format;
|
var formatter = outputTypes[j - 1].format;
|
||||||
if (arrayType(outs[i].type)) {
|
if (isArrayType(outs[i].type)) {
|
||||||
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
|
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
|
||||||
dynamicPart = dynamicPart.slice(padding);
|
dynamicPart = dynamicPart.slice(padding);
|
||||||
var array = [];
|
var array = [];
|
||||||
|
|
Loading…
Reference in New Issue