UNPKG

1.41 kBJavaScriptView Raw
1const callable = require('../util/callable');
2const EventCoder = require('./EventCoder');
3
4/**
5 * @memberOf ContractEvent
6 */
7class LogFilter {
8 constructor({ address, topics }, event) {
9 this.address = address;
10 this.topics = topics;
11 Reflect.defineProperty(this, 'event', { value: event }); // avoid for JSON.stringify
12 }
13
14 async getLogs(options) {
15 const logs = await this.event.conflux.getLogs({ ...options, address: this.address, topics: this.topics });
16
17 logs.forEach(log => {
18 log.params = this.event.decodeLog(log);
19 });
20
21 return logs;
22 }
23}
24
25class ContractEvent extends EventCoder {
26 constructor(fragment, contract, conflux) {
27 super(fragment);
28 this.contract = contract;
29 this.conflux = conflux;
30
31 return callable(this, this.call.bind(this));
32 }
33
34 call(...args) {
35 const address = this.contract.address; // dynamic get `contract.address`
36 const topics = [this.signature, ...this.encodeTopics(args)];
37 return new LogFilter({ address, topics }, this);
38 }
39
40 decodeLog(log) {
41 const namedTuple = super.decodeLog(log);
42 return {
43 name: this.name,
44 fullName: this.fullName,
45 type: this.type,
46 signature: this.signature,
47 array: [...namedTuple],
48 object: namedTuple.toObject(),
49 };
50 }
51}
52
53module.exports = ContractEvent;
54module.exports.LogFilter = LogFilter;