diff options
Diffstat (limited to 'lib/event.js')
-rw-r--r-- | lib/event.js | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/lib/event.js b/lib/event.js index ae2195381..812ef9115 100644 --- a/lib/event.js +++ b/lib/event.js @@ -20,13 +20,47 @@ * @date 2014 */ -var implementationOfEvent = function (address, signature) { +var abi = require('./abi'); +var utils = require('./utils'); + +var inputWithName = function (inputs, name) { + var index = utils.findIndex(inputs, function (input) { + return input.name === name; + }); + + if (index === -1) { + console.error('indexed param with name ' + name + ' not found'); + return undefined; + } + return inputs[index]; +}; + +var indexedParamsToTopics = function (event, indexed) { + // sort keys? + return Object.keys(indexed).map(function (key) { + var inputs = [inputWithName(event.inputs, key)]; + + var value = indexed[key]; + if (value instanceof Array) { + return value.map(function (v) { + return abi.formatInput(inputs, [v]); + }); + } + return abi.formatInput(inputs, [value]); + }); +}; + +var implementationOfEvent = function (address, signature, event) { - return function (options) { + // valid options are 'earliest', 'latest', 'offset' and 'max', as defined for 'eth.watch' + return function (indexed, options) { var o = options || {}; - o.address = o.address || address; - o.topics = o.topics || []; - o.topics.push(signature); + o.address = address; + o.topic = []; + o.topic.push(signature); + if (indexed) { + o.topic = o.topic.concat(indexedParamsToTopics(event, indexed)); + } return o; }; }; |