diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-31 22:22:05 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-31 22:22:05 +0800 |
commit | b20e972bec52781de806fb050e72d44b729c6541 (patch) | |
tree | 0d8114da1536ae5ec6c2c606a30e1ae4c2cb311f /lib | |
parent | a8a2e3231c2ced50989dc5d23659f7482a667f69 (diff) | |
download | dexon-b20e972bec52781de806fb050e72d44b729c6541.tar dexon-b20e972bec52781de806fb050e72d44b729c6541.tar.gz dexon-b20e972bec52781de806fb050e72d44b729c6541.tar.bz2 dexon-b20e972bec52781de806fb050e72d44b729c6541.tar.lz dexon-b20e972bec52781de806fb050e72d44b729c6541.tar.xz dexon-b20e972bec52781de806fb050e72d44b729c6541.tar.zst dexon-b20e972bec52781de806fb050e72d44b729c6541.zip |
few methods moved to utils
Diffstat (limited to 'lib')
-rw-r--r-- | lib/abi.js | 51 | ||||
-rw-r--r-- | lib/const.js | 2 | ||||
-rw-r--r-- | lib/contract.js | 53 | ||||
-rw-r--r-- | lib/event.js | 2 | ||||
-rw-r--r-- | lib/utils.js | 34 |
5 files changed, 71 insertions, 71 deletions
diff --git a/lib/abi.js b/lib/abi.js index 01d2debb7..8121c1a05 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -27,22 +27,6 @@ var types = require('./types'); var c = require('./const'); 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 /// @returns true if it is, otherwise false var arrayType = function (type) { @@ -154,27 +138,14 @@ var fromAbiOutput = function (method, output) { 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 /// @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 = {}; json.forEach(function (method) { - var displayName = methodDisplayName(method.name); - var typeName = methodTypeName(method.name); + var displayName = utils.extractDisplayName(method.name); + var typeName = utils.extractTypeName(method.name); var impl = function () { var params = Array.prototype.slice.call(arguments); @@ -197,8 +168,8 @@ var outputParser = function (json) { var parser = {}; json.forEach(function (method) { - var displayName = methodDisplayName(method.name); - var typeName = methodTypeName(method.name); + var displayName = utils.extractDisplayName(method.name); + var typeName = utils.extractTypeName(method.name); var impl = function (output) { return fromAbiOutput(method, output); @@ -214,19 +185,15 @@ var outputParser = function (json) { return parser; }; -/// @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 + c.ETH_METHOD_SIGNATURE_LENGTH * 2); +/// @param function/event name for which we want to get signature +/// @returns signature of function/event with given name +var signatureFromAscii = function (name) { + return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_SIGNATURE_LENGTH * 2); }; module.exports = { inputParser: inputParser, outputParser: outputParser, - methodSignature: methodSignature, - methodDisplayName: methodDisplayName, - methodTypeName: methodTypeName, - filterFunctions: filterFunctions, - filterEvents: filterEvents + signatureFromAscii: signatureFromAscii }; diff --git a/lib/const.js b/lib/const.js index 69ed5a09b..22f6dc690 100644 --- a/lib/const.js +++ b/lib/const.js @@ -27,7 +27,7 @@ if (process.env.NODE_ENV !== 'build') { module.exports = { ETH_PADDING: 32, - ETH_METHOD_SIGNATURE_LENGTH: 4, + ETH_SIGNATURE_LENGTH: 4, ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN } }; diff --git a/lib/contract.js b/lib/contract.js index 4c92825f1..94a6d7dac 100644 --- a/lib/contract.js +++ b/lib/contract.js @@ -22,8 +22,18 @@ var web3 = require('./web3'); var abi = require('./abi'); +var utils = require('./utils'); 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) { contract.call = function (options) { @@ -53,14 +63,14 @@ var addFunctionsToContract = function (contract, desc, address) { var outputParser = abi.outputParser(desc); // create contract functions - abi.filterFunctions(desc).forEach(function (method) { + utils.filterFunctions(desc).forEach(function (method) { - var displayName = abi.methodDisplayName(method.name); - var typeName = abi.methodTypeName(method.name); + var displayName = utils.extractDisplayName(method.name); + var typeName = utils.extractTypeName(method.name); var impl = function () { 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 options = contract._options || {}; @@ -75,12 +85,13 @@ var addFunctionsToContract = function (contract, desc, address) { contract._isTransact = null; if (isTransact) { - // it's used byt natspec.js - // TODO: figure out better way to solve this - web3._currentContractAbi = desc; - web3._currentContractAddress = address; - web3._currentContractMethodName = method.name; - web3._currentContractMethodParams = params; + + exportNatspecGlobals({ + abi: desc, + address: address, + method: method.name, + params: params + }); // transactions do not have any output, cause we do not know, when they will be processed web3.eth.transact(options); @@ -112,8 +123,8 @@ var addEventRelatedPropertiesToContract = function (contract, desc, address) { Object.defineProperty(contract, 'topic', { get: function() { - return abi.filterEvents(desc).map(function (e) { - return abi.methodSignature(e.name); + return utils.filterEvents(desc).map(function (e) { + return abi.signatureFromAscii(e.name); }); } }); @@ -122,11 +133,11 @@ var addEventRelatedPropertiesToContract = function (contract, desc, address) { var addEventsToContract = function (contract, desc, address) { // create contract events - abi.filterEvents(desc).forEach(function (e) { + utils.filterEvents(desc).forEach(function (e) { var impl = function () { 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 o = event.apply(null, params); 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 impl._isEvent = true; - // TODO: we can remove address && topic properties, they are not used anymore since we introduced _isEvent - impl.address = address; - - 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); + var displayName = utils.extractDisplayName(e.name); + var typeName = utils.extractTypeName(e.name); if (contract[displayName] === undefined) { contract[displayName] = impl; diff --git a/lib/event.js b/lib/event.js index efb85b1fc..43ea96d12 100644 --- a/lib/event.js +++ b/lib/event.js @@ -29,7 +29,7 @@ var inputWithName = function (inputs, name) { }); if (index === -1) { - console.error('indexed paray with name ' + name + ' not found'); + console.error('indexed param with name ' + name + ' not found'); return undefined; } return inputs[index]; diff --git a/lib/utils.js b/lib/utils.js index f9fcd3389..5cd6ec8d6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -72,10 +72,42 @@ var fromAscii = function(str, pad) { 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 = { findIndex: findIndex, toAscii: toAscii, - fromAscii: fromAscii + fromAscii: fromAscii, + extractDisplayName: extractDisplayName, + extractTypeName: extractTypeName, + filterFunctions: filterFunctions, + filterEvents: filterEvents }; |