/
on-chain-certificates.listener.ts
185 lines (149 loc) · 7.55 KB
/
on-chain-certificates.listener.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { constants, providers } from 'ethers';
import { CertificateUtils, IBlockchainProperties } from '@energyweb/issuer';
import { EventBus } from '@nestjs/cqrs';
import { ConfigService } from '@nestjs/config';
import { BlockchainPropertiesService } from '../../blockchain/blockchain-properties.service';
import { CertificatesCreatedEvent } from '../events/certificates-created-event';
import { SyncCertificateEvent } from '../events/sync-certificate-event';
import { BlockchainEventType } from '../types';
import { NewTransactionProcessedEvent } from '../events/new-transaction-processed.event';
@Injectable()
export class OnChainCertificateWatcher implements OnModuleInit {
private readonly logger = new Logger(OnChainCertificateWatcher.name);
public provider: providers.FallbackProvider | providers.JsonRpcProvider;
public registry: IBlockchainProperties['registry'];
constructor(
private readonly configService: ConfigService,
private readonly blockchainPropertiesService: BlockchainPropertiesService,
private readonly eventBus: EventBus
) {}
public async onModuleInit(): Promise<void> {
this.logger.debug('OnModuleInit');
const { web3, registry } = await this.blockchainPropertiesService.getWrapped();
this.provider = web3;
this.registry = registry;
this.provider.pollingInterval =
Number(this.configService.get<string>('BLOCKCHAIN_POLLING_INTERVAL')) ||
this.provider.pollingInterval;
this.provider.on(
this.registry.filters.IssuanceSingle(null, null, null),
(event: providers.Log) => this.processEvent(BlockchainEventType.IssuanceSingle, event)
);
this.provider.on(
this.registry.filters.IssuanceBatch(null, null, null),
(event: providers.Log) => this.processEvent(BlockchainEventType.IssuanceBatch, event)
);
this.provider.on(
this.registry.filters.TransferSingle(null, null, null, null, null),
(event: providers.Log) => this.processEvent(BlockchainEventType.TransferSingle, event)
);
this.provider.on(
this.registry.filters.TransferBatch(null, null, null, null, null),
(event: providers.Log) => this.processEvent(BlockchainEventType.TransferBatch, event)
);
this.provider.on(
this.registry.filters.TransferBatchMultiple(null, null, null, null, null),
(event: providers.Log) =>
this.processEvent(BlockchainEventType.TransferBatchMultiple, event)
);
this.provider.on(
this.registry.filters.ClaimSingle(null, null, null, null, null, null),
(event: providers.Log) => this.processEvent(BlockchainEventType.ClaimSingle, event)
);
this.provider.on(
this.registry.filters.ClaimBatch(null, null, null, null, null, null),
(event: providers.Log) => this.processEvent(BlockchainEventType.ClaimBatch, event)
);
this.provider.on(
this.registry.filters.ClaimBatchMultiple(null, null, null, null, null, null),
(event: providers.Log) =>
this.processEvent(BlockchainEventType.ClaimBatchMultiple, event)
);
}
async processEvent(eventType: BlockchainEventType, rawEvent: providers.Log): Promise<void> {
if (!this.provider) {
this.logger.debug(`Provider unavailable: ${JSON.stringify(this.provider)}`);
return;
}
this.logger.debug(`Processing event ${eventType}: ${JSON.stringify(rawEvent)}`);
const event = await CertificateUtils.decodeEvent(eventType, rawEvent, this.registry);
const logEvent = (type: BlockchainEventType, ids: number[]) =>
this.logger.log(`Detected a new event: ${type} on Certificate ${JSON.stringify(ids)}`);
switch (eventType) {
case BlockchainEventType.IssuanceSingle:
logEvent(BlockchainEventType.IssuanceSingle, [event._id.toNumber()]);
this.eventBus.publish(new CertificatesCreatedEvent([event._id.toNumber()]));
break;
case BlockchainEventType.IssuanceBatch:
const ids = event._ids.map((id: any) => id.toNumber());
logEvent(BlockchainEventType.IssuanceBatch, ids);
this.eventBus.publish(new CertificatesCreatedEvent(ids));
break;
case BlockchainEventType.TransferSingle:
logEvent(BlockchainEventType.TransferSingle, [event.id.toNumber()]);
if (event.from === constants.AddressZero) {
this.logger.debug(
`Skipping TransferSingle handler for certificate ${event.id.toNumber()} because it's an issuance.`
);
break;
}
this.eventBus.publish(
new SyncCertificateEvent(event.id.toNumber(), event.transactionHash)
);
break;
case BlockchainEventType.TransferBatch:
case BlockchainEventType.TransferBatchMultiple:
if (event.from === constants.AddressZero) {
this.logger.debug(
`Skipping TransferBatch handler for certificates ${event.ids
.map((id: any) => id.toNumber())
.join(', ')} because it's an issuance.`
);
break;
}
event.ids.forEach((id: any) => {
logEvent(BlockchainEventType.TransferBatch, [id.toNumber()]);
this.eventBus.publish(
new SyncCertificateEvent(id.toNumber(), event.transactionHash)
);
});
break;
case BlockchainEventType.ClaimSingle:
logEvent(BlockchainEventType.ClaimSingle, [event._id.toNumber()]);
this.eventBus.publish(
new SyncCertificateEvent(event._id.toNumber(), event.transactionHash)
);
break;
case BlockchainEventType.ClaimBatch:
case BlockchainEventType.ClaimBatchMultiple:
event._ids.forEach((id: any) => {
logEvent(BlockchainEventType.ClaimBatch, [id.toNumber()]);
this.eventBus.publish(
new SyncCertificateEvent(id.toNumber(), event.transactionHash)
);
});
break;
default:
this.logger.log(
`No handlers found for event: ${eventType} on Certificate ${event._id.toNumber()}`
);
}
this.eventBus.publish(
new NewTransactionProcessedEvent({
event,
transactionType: eventType
})
);
}
onApplicationShutdown(): void {
this.logger.debug('onApplicationShutdown');
this.provider.off(this.registry.filters.IssuanceSingle(null, null, null));
this.provider.off(this.registry.filters.IssuanceBatch(null, null, null));
this.provider.off(this.registry.filters.TransferSingle(null, null, null, null, null));
this.provider.off(this.registry.filters.TransferBatch(null, null, null, null, null));
this.provider.off(this.registry.filters.ClaimSingle(null, null, null, null, null, null));
this.provider.off(this.registry.filters.ClaimBatch(null, null, null, null, null, null));
this.provider = null;
}
}