Skip to content

Commit bd364d7

Browse files
marcusmolchanyfrozeman
authored andcommitted
fix(allevents): update decode logic to handle no topics (web3#1330)
* fix(allevents): update decode logic to handle no topics * add tests
1 parent 616e06a commit bd364d7

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed

lib/web3/allevents.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ AllSolidityEvents.prototype.encode = function (options) {
5050

5151
AllSolidityEvents.prototype.decode = function (data) {
5252
data.data = data.data || '';
53-
data.topics = data.topics || [];
53+
54+
if (!utils.isArray(data.topics) || !utils.isString(data.topics[0])) {
55+
console.warn('cannot find event for log');
56+
return data;
57+
}
5458

5559
var eventTopic = data.topics[0].slice(2);
5660
var match = this._json.filter(function (j) {

test/allevents.decode.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
var chai = require('chai');
2+
var assert = chai.assert;
3+
var BigNumber = require('bignumber.js');
4+
var AllSolidityEvents = require('../lib/web3/allevents');
5+
var Web3 = require('../index');
6+
7+
8+
var name = 'event1';
9+
var address = '0x1234567890123456789012345678901234567890';
10+
11+
var tests = [{
12+
abi: {
13+
name: name,
14+
inputs: [{
15+
name: 'a',
16+
type: 'int',
17+
indexed: false
18+
}]
19+
},
20+
data: {
21+
logIndex: '0x1',
22+
transactionIndex: '0x10',
23+
transactionHash: '0x1234567890',
24+
address: address,
25+
blockHash: '0x1234567890',
26+
blockNumber: '0x1',
27+
data: '0x' +
28+
'0000000000000000000000000000000000000000000000000000000000000001' +
29+
'0000000000000000000000000000000000000000000000000000000000000004',
30+
topics: undefined,
31+
},
32+
expected: {
33+
logIndex: '0x1',
34+
transactionIndex: '0x10',
35+
transactionHash: '0x1234567890',
36+
address: address,
37+
blockHash: '0x1234567890',
38+
blockNumber: '0x1',
39+
data: '0x' +
40+
'0000000000000000000000000000000000000000000000000000000000000001' +
41+
'0000000000000000000000000000000000000000000000000000000000000004',
42+
topics: undefined,
43+
}
44+
}, {
45+
abi: {
46+
name: name,
47+
inputs: [{
48+
name: 'a',
49+
type: 'int',
50+
indexed: false
51+
}]
52+
},
53+
data: {
54+
logIndex: '0x1',
55+
transactionIndex: '0x10',
56+
transactionHash: '0x1234567890',
57+
address: address,
58+
blockHash: '0x1234567890',
59+
blockNumber: '0x1',
60+
data: '0x' +
61+
'0000000000000000000000000000000000000000000000000000000000000001' +
62+
'0000000000000000000000000000000000000000000000000000000000000004',
63+
topics: [],
64+
},
65+
expected: {
66+
logIndex: '0x1',
67+
transactionIndex: '0x10',
68+
transactionHash: '0x1234567890',
69+
address: address,
70+
blockHash: '0x1234567890',
71+
blockNumber: '0x1',
72+
data: '0x' +
73+
'0000000000000000000000000000000000000000000000000000000000000001' +
74+
'0000000000000000000000000000000000000000000000000000000000000004',
75+
topics: [],
76+
}
77+
}, {
78+
abi: {
79+
name: name,
80+
anonymous: true,
81+
inputs: [{
82+
name: 'a',
83+
type: 'int',
84+
indexed: false
85+
}, {
86+
name: 'b',
87+
type: 'int',
88+
indexed: true
89+
}, {
90+
name: 'c',
91+
type: 'int',
92+
indexed: false
93+
}, {
94+
name: 'd',
95+
type: 'int',
96+
indexed: true
97+
}]
98+
},
99+
data: {
100+
logIndex: '0x1',
101+
transactionIndex: '0x10',
102+
transactionHash: '0x1234567890',
103+
address: address,
104+
blockHash: '0x1234567890',
105+
blockNumber: '0x1',
106+
data: '0x' +
107+
'0000000000000000000000000000000000000000000000000000000000000001' +
108+
'0000000000000000000000000000000000000000000000000000000000000004',
109+
topics: [
110+
'0x000000000000000000000000000000000000000000000000000000000000000a',
111+
'0x0000000000000000000000000000000000000000000000000000000000000010'
112+
]
113+
},
114+
expected: {
115+
event: name,
116+
args: {
117+
a: new BigNumber(1),
118+
b: new BigNumber(10),
119+
c: new BigNumber(4),
120+
d: new BigNumber(16)
121+
},
122+
logIndex: 1,
123+
transactionIndex: 16,
124+
transactionHash: '0x1234567890',
125+
address: address,
126+
blockHash: '0x1234567890',
127+
blockNumber: 1
128+
}
129+
}];
130+
131+
describe('lib/web3/allevents', function () {
132+
describe('decode', function () {
133+
tests.forEach(function (test, index) {
134+
it('test no: ' + index, function () {
135+
var web3 = new Web3();
136+
var allEvents = new AllSolidityEvents(web3, test.abi, address);
137+
138+
var result = allEvents.decode(test.data);
139+
assert.deepEqual(result, test.expected);
140+
});
141+
});
142+
});
143+
});
144+

0 commit comments

Comments
 (0)