1 | const lodash = require('lodash');
|
2 | const callable = require('../util/callable');
|
3 |
|
4 | class ContractEventOverride {
|
5 | constructor(events, contract, conflux) {
|
6 | this.signatureToEvent = lodash.keyBy(events, 'signature');
|
7 | this.contract = contract;
|
8 | this.conflux = conflux;
|
9 |
|
10 | return callable(this, this.call.bind(this));
|
11 | }
|
12 |
|
13 | call(...args) {
|
14 | const acceptArray = [];
|
15 | const rejectArray = [];
|
16 |
|
17 | let filter;
|
18 | for (const event of Object.values(this.signatureToEvent)) {
|
19 | try {
|
20 | filter = event(...args);
|
21 | acceptArray.push(event.type);
|
22 | } catch (e) {
|
23 | rejectArray.push(event.type);
|
24 | }
|
25 | }
|
26 |
|
27 | if (!acceptArray.length) {
|
28 | throw new Error(`can not match override "${rejectArray.join(',')}" with args (${args.join(',')})`);
|
29 | }
|
30 | if (acceptArray.length > 1) {
|
31 | throw new Error(`can not determine override "${acceptArray.join('|')}" with args (${args.join(',')})`);
|
32 | }
|
33 |
|
34 | return filter;
|
35 | }
|
36 |
|
37 | decodeLog(log) {
|
38 | const topic = log.topics[0];
|
39 | const event = this.signatureToEvent[topic];
|
40 | return event.decodeLog(log);
|
41 | }
|
42 | }
|
43 |
|
44 | module.exports = ContractEventOverride;
|