Skip to content

Commit

Permalink
fix(allevents): update decode logic to handle no topics (#1330)
Browse files Browse the repository at this point in the history
* fix(allevents): update decode logic to handle no topics

* add tests
  • Loading branch information
marcusmolchany authored and frozeman committed Feb 13, 2018
1 parent 616e06a commit bd364d7
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/web3/allevents.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ AllSolidityEvents.prototype.encode = function (options) {

AllSolidityEvents.prototype.decode = function (data) {
data.data = data.data || '';
data.topics = data.topics || [];

if (!utils.isArray(data.topics) || !utils.isString(data.topics[0])) {
console.warn('cannot find event for log');
return data;
}

var eventTopic = data.topics[0].slice(2);
var match = this._json.filter(function (j) {
Expand Down
144 changes: 144 additions & 0 deletions test/allevents.decode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
var chai = require('chai');
var assert = chai.assert;
var BigNumber = require('bignumber.js');
var AllSolidityEvents = require('../lib/web3/allevents');
var Web3 = require('../index');


var name = 'event1';
var address = '0x1234567890123456789012345678901234567890';

var tests = [{
abi: {
name: name,
inputs: [{
name: 'a',
type: 'int',
indexed: false
}]
},
data: {
logIndex: '0x1',
transactionIndex: '0x10',
transactionHash: '0x1234567890',
address: address,
blockHash: '0x1234567890',
blockNumber: '0x1',
data: '0x' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000004',
topics: undefined,
},
expected: {
logIndex: '0x1',
transactionIndex: '0x10',
transactionHash: '0x1234567890',
address: address,
blockHash: '0x1234567890',
blockNumber: '0x1',
data: '0x' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000004',
topics: undefined,
}
}, {
abi: {
name: name,
inputs: [{
name: 'a',
type: 'int',
indexed: false
}]
},
data: {
logIndex: '0x1',
transactionIndex: '0x10',
transactionHash: '0x1234567890',
address: address,
blockHash: '0x1234567890',
blockNumber: '0x1',
data: '0x' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000004',
topics: [],
},
expected: {
logIndex: '0x1',
transactionIndex: '0x10',
transactionHash: '0x1234567890',
address: address,
blockHash: '0x1234567890',
blockNumber: '0x1',
data: '0x' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000004',
topics: [],
}
}, {
abi: {
name: name,
anonymous: true,
inputs: [{
name: 'a',
type: 'int',
indexed: false
}, {
name: 'b',
type: 'int',
indexed: true
}, {
name: 'c',
type: 'int',
indexed: false
}, {
name: 'd',
type: 'int',
indexed: true
}]
},
data: {
logIndex: '0x1',
transactionIndex: '0x10',
transactionHash: '0x1234567890',
address: address,
blockHash: '0x1234567890',
blockNumber: '0x1',
data: '0x' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000004',
topics: [
'0x000000000000000000000000000000000000000000000000000000000000000a',
'0x0000000000000000000000000000000000000000000000000000000000000010'
]
},
expected: {
event: name,
args: {
a: new BigNumber(1),
b: new BigNumber(10),
c: new BigNumber(4),
d: new BigNumber(16)
},
logIndex: 1,
transactionIndex: 16,
transactionHash: '0x1234567890',
address: address,
blockHash: '0x1234567890',
blockNumber: 1
}
}];

describe('lib/web3/allevents', function () {
describe('decode', function () {
tests.forEach(function (test, index) {
it('test no: ' + index, function () {
var web3 = new Web3();
var allEvents = new AllSolidityEvents(web3, test.abi, address);

var result = allEvents.decode(test.data);
assert.deepEqual(result, test.expected);
});
});
});
});

0 comments on commit bd364d7

Please sign in to comment.