diff options
Diffstat (limited to 'lib/event.js')
-rw-r--r-- | lib/event.js | 72 |
1 files changed, 69 insertions, 3 deletions
diff --git a/lib/event.js b/lib/event.js index 812ef9115..0c41e0a39 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,61 @@ 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, + args: {} + }; + + if (!output.topic) { + return result; + } + + 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; + }; +}; + +var getMatchingEvent = function (events, payload) { + for (var i = 0; i < events.length; i++) { + var signature = abi.eventSignatureFromAscii(events[i].name); + if (signature === payload.topic[0]) { + return events[i]; + } + } + return undefined; +}; + + +module.exports = { + inputParser: inputParser, + outputParser: outputParser, + getMatchingEvent: getMatchingEvent +}; |