Skip to content

Commit

Permalink
fix bug when 2+ events are returned in logs subscription
Browse files Browse the repository at this point in the history
If 2 or more of the same event type are logged in a single transaction,
the eth_subscription result will contain multiple logs.
  • Loading branch information
ewingrj committed Sep 16, 2017
1 parent 5a113d6 commit 8976ed9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
12 changes: 7 additions & 5 deletions packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,13 @@ Subscription.prototype.subscribe = function() {
// call callback on notifications
_this.options.requestManager.addSubscription(_this.id, payload.params[0] , _this.options.type, function(err, result) {

// TODO remove once its fixed in geth
if(_.isArray(result))
result = result[0];

var output = _this._formatOutput(result);
// TODO remove the length check once this is fixed in geth
var output;
if(_.isArray(result) && result.length > 1) {
output = _.map(result, (function(r) { return _this._formatOutput(r); }));
} else {
output = _.isArray(result) ? _this._formatOutput(result[0]) : _this._formatOutput(result);
}

if (!err) {

Expand Down
9 changes: 8 additions & 1 deletion packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,14 @@ Contract.prototype._on = function(){
outputFormatter: this._decodeEventABI.bind(subOptions.event),
// DUBLICATE, also in web3-eth
subscriptionHandler: function (output) {
if(output.removed) {
var changed;
if (_.isArray(output)) {
changed = _.reduce(output, function (c, log) {return c || log.changed;}, false);
} else {
changed = output.removed;
}

if(changed) {
this.emit('changed', output);
} else {
this.emit('data', output);
Expand Down
9 changes: 8 additions & 1 deletion packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,14 @@ var Eth = function Eth() {
outputFormatter: formatter.outputLogFormatter,
// DUBLICATE, also in web3-eth-contract
subscriptionHandler: function (output) {
if(output.removed) {
var changed;
if (_.isArray(output)) {
changed = _.reduce(output, function (c, log) {return c || log.changed;}, false);
} else {
changed = output.removed;
}

if(changed) {
this.emit('changed', output);
} else {
this.emit('data', output);
Expand Down

0 comments on commit 8976ed9

Please sign in to comment.