constants separated to const.js file

This commit is contained in:
Marek Kotewicz
2015-01-31 14:05:48 +01:00
parent 4bdf52fc1e
commit a8a2e3231c
6 changed files with 104 additions and 58 deletions
+5 -15
View File
@@ -21,22 +21,12 @@
* @date 2014
*/
if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js'); // jshint ignore:line
}
var web3 = require('./web3');
var utils = require('./utils');
var types = require('./types');
var c = require('./const');
var f = require('./formatters');
BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
var ETH_PADDING = 32;
/// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
@@ -74,7 +64,7 @@ var inputTypes = types.inputTypes();
/// @returns bytes representation of input params
var toAbiInput = function (method, params) {
var bytes = "";
var padding = ETH_PADDING * 2;
var padding = c.ETH_PADDING * 2;
/// first we iterate in search for dynamic
method.inputs.forEach(function (input, index) {
@@ -107,7 +97,7 @@ var toAbiInput = function (method, params) {
var dynamicBytesLength = function (type) {
if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length.
return ETH_PADDING * 2;
return c.ETH_PADDING * 2;
return 0;
};
@@ -121,7 +111,7 @@ var fromAbiOutput = function (method, output) {
output = output.slice(2);
var result = [];
var padding = ETH_PADDING * 2;
var padding = c.ETH_PADDING * 2;
var dynamicPartLength = method.outputs.reduce(function (acc, curr) {
return acc + dynamicBytesLength(curr.type);
@@ -227,7 +217,7 @@ var outputParser = function (json) {
/// @param method name for which we want to get method signature
/// @returns (promise) contract method signature for method with given name
var methodSignature = function (name) {
return web3.sha3(web3.fromAscii(name)).slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2);
return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_METHOD_SIGNATURE_LENGTH * 2);
};
module.exports = {
+33
View File
@@ -0,0 +1,33 @@
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file const.js
* @authors:
* Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
/// required to define ETH_BIGNUMBER_ROUNDING_MODE
if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js'); // jshint ignore:line
}
module.exports = {
ETH_PADDING: 32,
ETH_METHOD_SIGNATURE_LENGTH: 4,
ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN }
};
+4 -6
View File
@@ -25,10 +25,7 @@ if (process.env.NODE_ENV !== 'build') {
}
var utils = require('./utils');
BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
var ETH_PADDING = 32;
var c = require('./const');
/// @param string string to be padded
/// @param number of characters that result string should have
@@ -43,10 +40,11 @@ var padLeft = function (string, chars, sign) {
/// If the value is floating point, round it down
/// @returns right-aligned byte representation of int
var formatInputInt = function (value) {
var padding = ETH_PADDING * 2;
var padding = c.ETH_PADDING * 2;
if (value instanceof BigNumber || typeof value === 'number') {
if (typeof value === 'number')
value = new BigNumber(value);
BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
value = value.round();
if (value.lessThan(0))
@@ -65,7 +63,7 @@ var formatInputInt = function (value) {
/// Formats input value to byte representation of string
/// @returns left-algined byte representation of string
var formatInputString = function (value) {
return utils.fromAscii(value, ETH_PADDING).substr(2);
return utils.fromAscii(value, c.ETH_PADDING).substr(2);
};
/// Formats input value to byte representation of bool