few methods moved to utils

This commit is contained in:
Marek Kotewicz 2015-01-31 15:22:05 +01:00
parent a8a2e3231c
commit b20e972bec
9 changed files with 154 additions and 154 deletions

144
dist/ethereum.js vendored
View File

@ -28,22 +28,6 @@ var types = require('./types');
var c = require('./const'); var c = require('./const');
var f = require('./formatters'); var f = require('./formatters');
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/// Filters all events form input abi
/// @returns abi array with filtered objects of type 'event'
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
/// 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 /// @returns true if it is, otherwise false
var arrayType = function (type) { var arrayType = function (type) {
@ -155,27 +139,14 @@ var fromAbiOutput = function (method, output) {
return result; return result;
}; };
/// @returns display name for method eg. multiply(uint256) -> multiply
var methodDisplayName = function (method) {
var length = method.indexOf('(');
return length !== -1 ? method.substr(0, length) : method;
};
/// @returns overloaded part of method's name
var methodTypeName = function (method) {
/// TODO: make it invulnerable
var length = method.indexOf('(');
return length !== -1 ? method.substr(length + 1, method.length - 1 - (length + 1)) : "";
};
/// @param json abi for contract /// @param json abi for contract
/// @returns input parser object for given json abi /// @returns input parser object for given json abi
/// TODO: refactor creating the parser, do not double logic from contract /// TODO: refactor creating the parser, do not double logic from contract
var inputParser = function (json) { var inputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { json.forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = utils.extractDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = utils.extractTypeName(method.name);
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
@ -198,8 +169,8 @@ var outputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { json.forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = utils.extractDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = utils.extractTypeName(method.name);
var impl = function (output) { var impl = function (output) {
return fromAbiOutput(method, output); return fromAbiOutput(method, output);
@ -215,20 +186,16 @@ var outputParser = function (json) {
return parser; return parser;
}; };
/// @param method name for which we want to get method signature /// @param function/event name for which we want to get signature
/// @returns (promise) contract method signature for method with given name /// @returns signature of function/event with given name
var methodSignature = function (name) { var signatureFromAscii = function (name) {
return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_METHOD_SIGNATURE_LENGTH * 2); return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_SIGNATURE_LENGTH * 2);
}; };
module.exports = { module.exports = {
inputParser: inputParser, inputParser: inputParser,
outputParser: outputParser, outputParser: outputParser,
methodSignature: methodSignature, signatureFromAscii: signatureFromAscii
methodDisplayName: methodDisplayName,
methodTypeName: methodTypeName,
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };
@ -262,7 +229,7 @@ if ("build" !== 'build') {/*
module.exports = { module.exports = {
ETH_PADDING: 32, ETH_PADDING: 32,
ETH_METHOD_SIGNATURE_LENGTH: 4, ETH_SIGNATURE_LENGTH: 4,
ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN } ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN }
}; };
@ -292,8 +259,18 @@ module.exports = {
var web3 = require('./web3'); var web3 = require('./web3');
var abi = require('./abi'); var abi = require('./abi');
var utils = require('./utils');
var eventImpl = require('./event'); var eventImpl = require('./event');
var exportNatspecGlobals = function (vars) {
// it's used byt natspec.js
// TODO: figure out better way to solve this
web3._currentContractAbi = vars.abi;
web3._currentContractAddress = vars.address;
web3._currentContractMethodName = vars.method;
web3._currentContractMethodParams = vars.params;
};
var addFunctionRelatedPropertiesToContract = function (contract) { var addFunctionRelatedPropertiesToContract = function (contract) {
contract.call = function (options) { contract.call = function (options) {
@ -323,14 +300,14 @@ var addFunctionsToContract = function (contract, desc, address) {
var outputParser = abi.outputParser(desc); var outputParser = abi.outputParser(desc);
// create contract functions // create contract functions
abi.filterFunctions(desc).forEach(function (method) { utils.filterFunctions(desc).forEach(function (method) {
var displayName = abi.methodDisplayName(method.name); var displayName = utils.extractDisplayName(method.name);
var typeName = abi.methodTypeName(method.name); var typeName = utils.extractTypeName(method.name);
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(method.name); var signature = abi.signatureFromAscii(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params); var parsed = inputParser[displayName][typeName].apply(null, params);
var options = contract._options || {}; var options = contract._options || {};
@ -345,12 +322,13 @@ var addFunctionsToContract = function (contract, desc, address) {
contract._isTransact = null; contract._isTransact = null;
if (isTransact) { if (isTransact) {
// it's used byt natspec.js
// TODO: figure out better way to solve this exportNatspecGlobals({
web3._currentContractAbi = desc; abi: desc,
web3._currentContractAddress = address; address: address,
web3._currentContractMethodName = method.name; method: method.name,
web3._currentContractMethodParams = params; params: params
});
// transactions do not have any output, cause we do not know, when they will be processed // transactions do not have any output, cause we do not know, when they will be processed
web3.eth.transact(options); web3.eth.transact(options);
@ -382,8 +360,8 @@ var addEventRelatedPropertiesToContract = function (contract, desc, address) {
Object.defineProperty(contract, 'topic', { Object.defineProperty(contract, 'topic', {
get: function() { get: function() {
return abi.filterEvents(desc).map(function (e) { return utils.filterEvents(desc).map(function (e) {
return abi.methodSignature(e.name); return abi.signatureFromAscii(e.name);
}); });
} }
}); });
@ -392,11 +370,11 @@ var addEventRelatedPropertiesToContract = function (contract, desc, address) {
var addEventsToContract = function (contract, desc, address) { var addEventsToContract = function (contract, desc, address) {
// create contract events // create contract events
abi.filterEvents(desc).forEach(function (e) { utils.filterEvents(desc).forEach(function (e) {
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(e.name); var signature = abi.signatureFromAscii(e.name);
var event = eventImpl(address, signature, e); var event = eventImpl(address, signature, e);
var o = event.apply(null, params); var o = event.apply(null, params);
return web3.eth.watch(o); return web3.eth.watch(o);
@ -405,18 +383,8 @@ var addEventsToContract = function (contract, desc, address) {
// this property should be used by eth.filter to check if object is an event // this property should be used by eth.filter to check if object is an event
impl._isEvent = true; impl._isEvent = true;
// TODO: we can remove address && topic properties, they are not used anymore since we introduced _isEvent var displayName = utils.extractDisplayName(e.name);
impl.address = address; var typeName = utils.extractTypeName(e.name);
Object.defineProperty(impl, 'topic', {
get: function() {
return [abi.methodSignature(e.name)];
}
});
// TODO: rename these methods, cause they are used not only for methods
var displayName = abi.methodDisplayName(e.name);
var typeName = abi.methodTypeName(e.name);
if (contract[displayName] === undefined) { if (contract[displayName] === undefined) {
contract[displayName] = impl; contract[displayName] = impl;
@ -476,7 +444,7 @@ var contract = function (address, desc) {
module.exports = contract; module.exports = contract;
},{"./abi":1,"./event":4,"./web3":12}],4:[function(require,module,exports){ },{"./abi":1,"./event":4,"./utils":11,"./web3":12}],4:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
@ -508,7 +476,7 @@ var inputWithName = function (inputs, name) {
}); });
if (index === -1) { if (index === -1) {
console.error('indexed paray with name ' + name + ' not found'); console.error('indexed param with name ' + name + ' not found');
return undefined; return undefined;
} }
return inputs[index]; return inputs[index];
@ -1178,11 +1146,43 @@ var fromAscii = function(str, pad) {
return "0x" + hex; return "0x" + hex;
}; };
/// @returns display name for function/event eg. multiply(uint256) -> multiply
var extractDisplayName = function (name) {
var length = name.indexOf('(');
return length !== -1 ? name.substr(0, length) : name;
};
/// @returns overloaded part of function/event name
var extractTypeName = function (name) {
/// TODO: make it invulnerable
var length = name.indexOf('(');
return length !== -1 ? name.substr(length + 1, name.length - 1 - (length + 1)) : "";
};
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/// Filters all events form input abi
/// @returns abi array with filtered objects of type 'event'
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
module.exports = { module.exports = {
findIndex: findIndex, findIndex: findIndex,
toAscii: toAscii, toAscii: toAscii,
fromAscii: fromAscii fromAscii: fromAscii,
extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName,
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };

12
dist/ethereum.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -27,22 +27,6 @@ var types = require('./types');
var c = require('./const'); var c = require('./const');
var f = require('./formatters'); var f = require('./formatters');
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/// Filters all events form input abi
/// @returns abi array with filtered objects of type 'event'
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
/// 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 /// @returns true if it is, otherwise false
var arrayType = function (type) { var arrayType = function (type) {
@ -154,27 +138,14 @@ var fromAbiOutput = function (method, output) {
return result; return result;
}; };
/// @returns display name for method eg. multiply(uint256) -> multiply
var methodDisplayName = function (method) {
var length = method.indexOf('(');
return length !== -1 ? method.substr(0, length) : method;
};
/// @returns overloaded part of method's name
var methodTypeName = function (method) {
/// TODO: make it invulnerable
var length = method.indexOf('(');
return length !== -1 ? method.substr(length + 1, method.length - 1 - (length + 1)) : "";
};
/// @param json abi for contract /// @param json abi for contract
/// @returns input parser object for given json abi /// @returns input parser object for given json abi
/// TODO: refactor creating the parser, do not double logic from contract /// TODO: refactor creating the parser, do not double logic from contract
var inputParser = function (json) { var inputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { json.forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = utils.extractDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = utils.extractTypeName(method.name);
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
@ -197,8 +168,8 @@ var outputParser = function (json) {
var parser = {}; var parser = {};
json.forEach(function (method) { json.forEach(function (method) {
var displayName = methodDisplayName(method.name); var displayName = utils.extractDisplayName(method.name);
var typeName = methodTypeName(method.name); var typeName = utils.extractTypeName(method.name);
var impl = function (output) { var impl = function (output) {
return fromAbiOutput(method, output); return fromAbiOutput(method, output);
@ -214,19 +185,15 @@ var outputParser = function (json) {
return parser; return parser;
}; };
/// @param method name for which we want to get method signature /// @param function/event name for which we want to get signature
/// @returns (promise) contract method signature for method with given name /// @returns signature of function/event with given name
var methodSignature = function (name) { var signatureFromAscii = function (name) {
return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_METHOD_SIGNATURE_LENGTH * 2); return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_SIGNATURE_LENGTH * 2);
}; };
module.exports = { module.exports = {
inputParser: inputParser, inputParser: inputParser,
outputParser: outputParser, outputParser: outputParser,
methodSignature: methodSignature, signatureFromAscii: signatureFromAscii
methodDisplayName: methodDisplayName,
methodTypeName: methodTypeName,
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };

View File

@ -27,7 +27,7 @@ if (process.env.NODE_ENV !== 'build') {
module.exports = { module.exports = {
ETH_PADDING: 32, ETH_PADDING: 32,
ETH_METHOD_SIGNATURE_LENGTH: 4, ETH_SIGNATURE_LENGTH: 4,
ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN } ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN }
}; };

View File

@ -22,8 +22,18 @@
var web3 = require('./web3'); var web3 = require('./web3');
var abi = require('./abi'); var abi = require('./abi');
var utils = require('./utils');
var eventImpl = require('./event'); var eventImpl = require('./event');
var exportNatspecGlobals = function (vars) {
// it's used byt natspec.js
// TODO: figure out better way to solve this
web3._currentContractAbi = vars.abi;
web3._currentContractAddress = vars.address;
web3._currentContractMethodName = vars.method;
web3._currentContractMethodParams = vars.params;
};
var addFunctionRelatedPropertiesToContract = function (contract) { var addFunctionRelatedPropertiesToContract = function (contract) {
contract.call = function (options) { contract.call = function (options) {
@ -53,14 +63,14 @@ var addFunctionsToContract = function (contract, desc, address) {
var outputParser = abi.outputParser(desc); var outputParser = abi.outputParser(desc);
// create contract functions // create contract functions
abi.filterFunctions(desc).forEach(function (method) { utils.filterFunctions(desc).forEach(function (method) {
var displayName = abi.methodDisplayName(method.name); var displayName = utils.extractDisplayName(method.name);
var typeName = abi.methodTypeName(method.name); var typeName = utils.extractTypeName(method.name);
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(method.name); var signature = abi.signatureFromAscii(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params); var parsed = inputParser[displayName][typeName].apply(null, params);
var options = contract._options || {}; var options = contract._options || {};
@ -75,12 +85,13 @@ var addFunctionsToContract = function (contract, desc, address) {
contract._isTransact = null; contract._isTransact = null;
if (isTransact) { if (isTransact) {
// it's used byt natspec.js
// TODO: figure out better way to solve this exportNatspecGlobals({
web3._currentContractAbi = desc; abi: desc,
web3._currentContractAddress = address; address: address,
web3._currentContractMethodName = method.name; method: method.name,
web3._currentContractMethodParams = params; params: params
});
// transactions do not have any output, cause we do not know, when they will be processed // transactions do not have any output, cause we do not know, when they will be processed
web3.eth.transact(options); web3.eth.transact(options);
@ -112,8 +123,8 @@ var addEventRelatedPropertiesToContract = function (contract, desc, address) {
Object.defineProperty(contract, 'topic', { Object.defineProperty(contract, 'topic', {
get: function() { get: function() {
return abi.filterEvents(desc).map(function (e) { return utils.filterEvents(desc).map(function (e) {
return abi.methodSignature(e.name); return abi.signatureFromAscii(e.name);
}); });
} }
}); });
@ -122,11 +133,11 @@ var addEventRelatedPropertiesToContract = function (contract, desc, address) {
var addEventsToContract = function (contract, desc, address) { var addEventsToContract = function (contract, desc, address) {
// create contract events // create contract events
abi.filterEvents(desc).forEach(function (e) { utils.filterEvents(desc).forEach(function (e) {
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(e.name); var signature = abi.signatureFromAscii(e.name);
var event = eventImpl(address, signature, e); var event = eventImpl(address, signature, e);
var o = event.apply(null, params); var o = event.apply(null, params);
return web3.eth.watch(o); return web3.eth.watch(o);
@ -135,18 +146,8 @@ var addEventsToContract = function (contract, desc, address) {
// this property should be used by eth.filter to check if object is an event // this property should be used by eth.filter to check if object is an event
impl._isEvent = true; impl._isEvent = true;
// TODO: we can remove address && topic properties, they are not used anymore since we introduced _isEvent var displayName = utils.extractDisplayName(e.name);
impl.address = address; var typeName = utils.extractTypeName(e.name);
Object.defineProperty(impl, 'topic', {
get: function() {
return [abi.methodSignature(e.name)];
}
});
// TODO: rename these methods, cause they are used not only for methods
var displayName = abi.methodDisplayName(e.name);
var typeName = abi.methodTypeName(e.name);
if (contract[displayName] === undefined) { if (contract[displayName] === undefined) {
contract[displayName] = impl; contract[displayName] = impl;

View File

@ -29,7 +29,7 @@ var inputWithName = function (inputs, name) {
}); });
if (index === -1) { if (index === -1) {
console.error('indexed paray with name ' + name + ' not found'); console.error('indexed param with name ' + name + ' not found');
return undefined; return undefined;
} }
return inputs[index]; return inputs[index];

View File

@ -72,10 +72,42 @@ var fromAscii = function(str, pad) {
return "0x" + hex; return "0x" + hex;
}; };
/// @returns display name for function/event eg. multiply(uint256) -> multiply
var extractDisplayName = function (name) {
var length = name.indexOf('(');
return length !== -1 ? name.substr(0, length) : name;
};
/// @returns overloaded part of function/event name
var extractTypeName = function (name) {
/// TODO: make it invulnerable
var length = name.indexOf('(');
return length !== -1 ? name.substr(length + 1, name.length - 1 - (length + 1)) : "";
};
/// Filters all function from input abi
/// @returns abi array with filtered objects of type 'function'
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/// Filters all events form input abi
/// @returns abi array with filtered objects of type 'event'
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
module.exports = { module.exports = {
findIndex: findIndex, findIndex: findIndex,
toAscii: toAscii, toAscii: toAscii,
fromAscii: fromAscii fromAscii: fromAscii,
extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName,
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };

View File

@ -1,7 +1,7 @@
var assert = require('assert'); var assert = require('assert');
var abi = require('../lib/abi.js'); var utils = require('../lib/utils.js');
describe('abi', function() { describe('utils', function() {
it('should filter functions and events from input array properly', function () { it('should filter functions and events from input array properly', function () {
// given // given
@ -36,8 +36,8 @@ describe('abi', function() {
}]; }];
// when // when
var events = abi.filterEvents(description); var events = utils.filterEvents(description);
var functions = abi.filterFunctions(description); var functions = utils.filterFunctions(description);
// then // then
assert.equal(events.length, 1); assert.equal(events.length, 1);