diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-02-03 22:08:19 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-02-03 22:08:19 +0800 |
commit | 03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965 (patch) | |
tree | dbfe40179e9e24006d37be201bd0b7e67f75f157 /lib | |
parent | 9d9c23e315a124c9fa5fa929d00c18791cfdb4e6 (diff) | |
download | go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.tar go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.tar.gz go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.tar.bz2 go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.tar.lz go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.tar.xz go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.tar.zst go-tangerine-03faec9d41d631f9a4cc6bbe1e91e7cd1cf7b965.zip |
event outputParser && tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/contract.js | 2 | ||||
-rw-r--r-- | lib/event.js | 56 |
2 files changed, 54 insertions, 4 deletions
diff --git a/lib/contract.js b/lib/contract.js index 748844fec..82e59af54 100644 --- a/lib/contract.js +++ b/lib/contract.js @@ -138,7 +138,7 @@ var addEventsToContract = function (contract, desc, address) { var impl = function () { var params = Array.prototype.slice.call(arguments); var signature = abi.eventSignatureFromAscii(e.name); - var event = eventImpl(address, signature, e); + var event = eventImpl.inputParser(address, signature, e); var o = event.apply(null, params); return web3.eth.watch(o); }; diff --git a/lib/event.js b/lib/event.js index 812ef9115..a50b2ae28 100644 --- a/lib/event.js +++ b/lib/event.js @@ -23,6 +23,16 @@ var abi = require('./abi'); var utils = require('./utils'); +/// filter inputs array && returns only indexed (or not) inputs +/// @param inputs array +/// @param bool if result should be an array of indexed params on not +/// @returns array of (not?) indexed params +var filterInputs = function (inputs, indexed) { + return inputs.filter(function (current) { + return current.indexed === indexed; + }); +}; + var inputWithName = function (inputs, name) { var index = utils.findIndex(inputs, function (input) { return input.name === name; @@ -38,7 +48,7 @@ var inputWithName = function (inputs, name) { var indexedParamsToTopics = function (event, indexed) { // sort keys? return Object.keys(indexed).map(function (key) { - var inputs = [inputWithName(event.inputs, key)]; + var inputs = [inputWithName(filterInputs(event.inputs, true), key)]; var value = indexed[key]; if (value instanceof Array) { @@ -50,7 +60,7 @@ var indexedParamsToTopics = function (event, indexed) { }); }; -var implementationOfEvent = function (address, signature, event) { +var inputParser = function (address, signature, event) { // valid options are 'earliest', 'latest', 'offset' and 'max', as defined for 'eth.watch' return function (indexed, options) { @@ -65,5 +75,45 @@ var implementationOfEvent = function (address, signature, event) { }; }; -module.exports = implementationOfEvent; +var getArgumentsObject = function (inputs, indexed, notIndexed) { + var indexedCopy = indexed.slice(); + var notIndexedCopy = notIndexed.slice(); + return inputs.reduce(function (acc, current) { + var value; + if (current.indexed) + value = indexed.splice(0, 1)[0]; + else + value = notIndexed.splice(0, 1)[0]; + + acc[current.name] = value; + return acc; + }, {}); +}; + + +var outputParser = function (event) { + + return function (output) { + var result = { + event: utils.extractDisplayName(event.name), + number: output.number + }; + + var indexedOutputs = filterInputs(event.inputs, true); + var indexedData = "0x" + output.topic.slice(1, output.topic.length).map(function (topic) { return topic.slice(2); }).join(""); + var indexedRes = abi.formatOutput(indexedOutputs, indexedData); + + var notIndexedOutputs = filterInputs(event.inputs, false); + var notIndexedRes = abi.formatOutput(notIndexedOutputs, output.data); + + result.args = getArgumentsObject(event.inputs, indexedRes, notIndexedRes); + + return result; + }; +}; + +module.exports = { + inputParser: inputParser, + outputParser: outputParser +}; |