forked from cerc-io/plugeth
parsing events output
This commit is contained in:
parent
1860b3dff9
commit
a5909d82eb
36
dist/ethereum.js
vendored
36
dist/ethereum.js
vendored
@ -391,6 +391,11 @@ var addFunctionsToContract = function (contract, desc, address) {
|
||||
|
||||
var addEventRelatedPropertiesToContract = function (contract, desc, address) {
|
||||
contract.address = address;
|
||||
contract._onWatchEventResult = function (data) {
|
||||
var matchingEvent = event.getMatchingEvent(utils.filterEvents(desc));
|
||||
var parser = eventImpl.outputParser(matchingEvent);
|
||||
return parser(data);
|
||||
};
|
||||
|
||||
Object.defineProperty(contract, 'topic', {
|
||||
get: function() {
|
||||
@ -411,6 +416,10 @@ var addEventsToContract = function (contract, desc, address) {
|
||||
var signature = abi.eventSignatureFromAscii(e.name);
|
||||
var event = eventImpl.inputParser(address, signature, e);
|
||||
var o = event.apply(null, params);
|
||||
o._onWatchEventResult = function (data) {
|
||||
var parser = eventImpl.outputParser(e);
|
||||
return parser(data);
|
||||
};
|
||||
return web3.eth.watch(o);
|
||||
};
|
||||
|
||||
@ -571,14 +580,18 @@ var getArgumentsObject = function (inputs, indexed, notIndexed) {
|
||||
}, {});
|
||||
};
|
||||
|
||||
|
||||
var outputParser = function (event) {
|
||||
|
||||
return function (output) {
|
||||
var result = {
|
||||
event: utils.extractDisplayName(event.name),
|
||||
number: output.number
|
||||
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("");
|
||||
@ -593,9 +606,21 @@ var outputParser = function (event) {
|
||||
};
|
||||
};
|
||||
|
||||
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
|
||||
outputParser: outputParser,
|
||||
getMatchingEvent: getMatchingEvent
|
||||
};
|
||||
|
||||
|
||||
@ -638,6 +663,8 @@ var Filter = function(options, impl) {
|
||||
if (options.topics) {
|
||||
console.warn('"topics" is deprecated, use "topic" instead');
|
||||
}
|
||||
|
||||
this._onWatchResult = options._onWatchEventResult;
|
||||
|
||||
// evaluate lazy properties
|
||||
options = {
|
||||
@ -673,7 +700,8 @@ Filter.prototype.changed = function(callback) {
|
||||
Filter.prototype.trigger = function(messages) {
|
||||
for (var i = 0; i < this.callbacks.length; i++) {
|
||||
for (var j = 0; j < messages.length; j++) {
|
||||
this.callbacks[i].call(this, messages[j]);
|
||||
var message = this._onWatchResult ? this._onWatchResult(messages[j]) : messages[j];
|
||||
this.callbacks[i].call(this, message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
8
dist/ethereum.js.map
vendored
8
dist/ethereum.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/ethereum.min.js
vendored
2
dist/ethereum.min.js
vendored
File diff suppressed because one or more lines are too long
@ -120,6 +120,11 @@ var addFunctionsToContract = function (contract, desc, address) {
|
||||
|
||||
var addEventRelatedPropertiesToContract = function (contract, desc, address) {
|
||||
contract.address = address;
|
||||
contract._onWatchEventResult = function (data) {
|
||||
var matchingEvent = event.getMatchingEvent(utils.filterEvents(desc));
|
||||
var parser = eventImpl.outputParser(matchingEvent);
|
||||
return parser(data);
|
||||
};
|
||||
|
||||
Object.defineProperty(contract, 'topic', {
|
||||
get: function() {
|
||||
@ -140,6 +145,10 @@ var addEventsToContract = function (contract, desc, address) {
|
||||
var signature = abi.eventSignatureFromAscii(e.name);
|
||||
var event = eventImpl.inputParser(address, signature, e);
|
||||
var o = event.apply(null, params);
|
||||
o._onWatchEventResult = function (data) {
|
||||
var parser = eventImpl.outputParser(e);
|
||||
return parser(data);
|
||||
};
|
||||
return web3.eth.watch(o);
|
||||
};
|
||||
|
||||
|
26
lib/event.js
26
lib/event.js
@ -90,14 +90,18 @@ var getArgumentsObject = function (inputs, indexed, notIndexed) {
|
||||
}, {});
|
||||
};
|
||||
|
||||
|
||||
var outputParser = function (event) {
|
||||
|
||||
return function (output) {
|
||||
var result = {
|
||||
event: utils.extractDisplayName(event.name),
|
||||
number: output.number
|
||||
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("");
|
||||
@ -112,8 +116,20 @@ var outputParser = function (event) {
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputParser: inputParser,
|
||||
outputParser: outputParser
|
||||
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
|
||||
};
|
||||
|
||||
|
@ -36,6 +36,8 @@ var Filter = function(options, impl) {
|
||||
if (options.topics) {
|
||||
console.warn('"topics" is deprecated, use "topic" instead');
|
||||
}
|
||||
|
||||
this._onWatchResult = options._onWatchEventResult;
|
||||
|
||||
// evaluate lazy properties
|
||||
options = {
|
||||
@ -71,7 +73,8 @@ Filter.prototype.changed = function(callback) {
|
||||
Filter.prototype.trigger = function(messages) {
|
||||
for (var i = 0; i < this.callbacks.length; i++) {
|
||||
for (var j = 0; j < messages.length; j++) {
|
||||
this.callbacks[i].call(this, messages[j]);
|
||||
var message = this._onWatchResult ? this._onWatchResult(messages[j]) : messages[j];
|
||||
this.callbacks[i].call(this, message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user