This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaces.ts
504 lines (469 loc) · 14.4 KB
/
interfaces.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/**
* Defines general interfaces for chain event fetching and processing.
*/
import * as SubstrateTypes from './chains/substrate/types';
import * as MolochTypes from './chains/moloch/types';
import * as CompoundTypes from './chains/compound/types';
import * as Erc20Types from './chains/erc20/types';
import * as Erc721Types from './chains/erc721/types';
import * as AaveTypes from './chains/aave/types';
import * as CommonwealthTypes from './chains/commonwealth/types';
// add other events here as union types
export type IChainEntityKind =
| SubstrateTypes.EntityKind
| MolochTypes.EntityKind
| CompoundTypes.EntityKind
| AaveTypes.EntityKind
| CommonwealthTypes.EntityKind;
export type IChainEventData =
| SubstrateTypes.IEventData
| MolochTypes.IEventData
| CompoundTypes.IEventData
| AaveTypes.IEventData
| Erc20Types.IEventData
| Erc721Types.IEventData
| CommonwealthTypes.IEventData;
export type IChainEventKind =
| SubstrateTypes.EventKind
| MolochTypes.EventKind
| CompoundTypes.EventKind
| AaveTypes.EventKind
| Erc20Types.EventKind
| Erc721Types.EventKind
| CommonwealthTypes.EventKind;
export const ChainEventKinds = [
...SubstrateTypes.EventKinds,
...MolochTypes.EventKinds,
...CompoundTypes.EventKinds,
...AaveTypes.EventKinds,
...Erc20Types.EventKinds,
...Erc721Types.EventKinds,
...CommonwealthTypes.EventKinds,
];
// eslint-disable-next-line no-shadow
export enum SupportedNetwork {
Substrate = 'substrate',
Aave = 'aave',
Compound = 'compound',
Moloch = 'moloch',
ERC20 = 'erc20',
ERC721 = 'erc721',
Commonwealth = 'commonwealth',
}
// eslint-disable-next-line no-shadow
export enum EntityEventKind {
Create = 0,
Update,
Vote,
Complete,
}
export interface CWEvent<IEventData = IChainEventData> {
blockNumber: number;
includeAddresses?: string[];
excludeAddresses?: string[];
data: IEventData;
network: SupportedNetwork;
chain?: string;
received?: number;
}
// handles individual events by sending them off to storage/notifying
export abstract class IEventHandler<DBEventType = IChainEventData> {
// throws on error, returns a db event, or void
public abstract handle(
event: CWEvent,
dbEvent?: DBEventType
): Promise<DBEventType>;
}
// parses events out of blocks into a standard format and
// passes them through to the handler
export abstract class IEventProcessor<Api, RawEvent> {
constructor(protected _api: Api) {}
// throws on error
public abstract process(block: RawEvent): Promise<CWEvent[]>;
}
// fetches blocks from chain in real-time via subscription for processing
export abstract class IEventSubscriber<Api, RawEvent> {
constructor(protected _api: Api, protected _verbose = false) {}
public get api(): Api {
return this._api;
}
// throws on error
public abstract subscribe(cb: (event: RawEvent) => void): Promise<void>;
public abstract unsubscribe(): void;
}
export interface IDisconnectedRange {
startBlock?: number;
endBlock?: number;
maxResults?: number;
}
export interface ISubscribeOptions<Api> {
chain: string;
api: Api;
handlers: IEventHandler<IChainEventData>[];
skipCatchup?: boolean;
archival?: boolean;
startBlock?: number;
discoverReconnectRange?: () => Promise<IDisconnectedRange>;
verbose?: boolean;
}
export type SubscribeFunc<
Api,
RawEvent,
Options extends ISubscribeOptions<Api>
> = (options: Options) => Promise<IEventSubscriber<Api, RawEvent>>;
// synthesizes events from chain storage
export abstract class IStorageFetcher<Api> {
constructor(protected _api: Api) {}
public abstract fetch(
range?: IDisconnectedRange,
fetchAllCompleted?: boolean
): Promise<CWEvent[]>;
public abstract fetchOne(
id: string,
kind?: IChainEntityKind
): Promise<CWEvent[]>;
}
// fetches historical blocks from chain for processing
export abstract class IEventPoller<Api, RawEvent> {
constructor(protected _api: Api) {}
// throws on error
public abstract poll(
range: IDisconnectedRange,
maxRange?: number
): Promise<RawEvent[]>;
}
// a set of labels used to display notifications
export interface IEventLabel {
heading: string;
label: string;
linkUrl?: string;
}
// a function that prepares chain data for user display
export type LabelerFilter = (
blockNumber: number,
chainId: string,
data: IChainEventData,
...formatters
) => IEventLabel;
export interface IEventTitle {
title: string;
description: string;
}
export type TitlerFilter = (kind: IChainEventKind) => IEventTitle;
export function entityToFieldName(
network: SupportedNetwork,
entity: IChainEntityKind
): string | null {
if (network === SupportedNetwork.Compound) {
return 'id';
}
if (network === SupportedNetwork.Aave) {
return 'id';
}
if (network === SupportedNetwork.Moloch) {
return 'proposalIndex';
}
if (network === SupportedNetwork.Commonwealth) {
return 'id';
}
switch (entity) {
case SubstrateTypes.EntityKind.DemocracyProposal: {
return 'proposalIndex';
}
case SubstrateTypes.EntityKind.DemocracyReferendum: {
return 'referendumIndex';
}
case SubstrateTypes.EntityKind.DemocracyPreimage: {
return 'proposalHash';
}
case SubstrateTypes.EntityKind.TreasuryProposal: {
return 'proposalIndex';
}
case SubstrateTypes.EntityKind.TreasuryBounty: {
return 'bountyIndex';
}
case SubstrateTypes.EntityKind.CollectiveProposal: {
return 'proposalHash';
}
case SubstrateTypes.EntityKind.SignalingProposal: {
return 'proposalHash';
}
case SubstrateTypes.EntityKind.TipProposal: {
return 'proposalHash';
}
default: {
return null;
}
}
}
export function eventToEntity(
network: SupportedNetwork,
event: IChainEventKind
): [IChainEntityKind, EntityEventKind] {
if (network === SupportedNetwork.Moloch) {
switch (event) {
case MolochTypes.EventKind.SubmitProposal: {
return [MolochTypes.EntityKind.Proposal, EntityEventKind.Create];
}
case MolochTypes.EventKind.SubmitVote: {
return [MolochTypes.EntityKind.Proposal, EntityEventKind.Vote];
}
case MolochTypes.EventKind.ProcessProposal: {
return [MolochTypes.EntityKind.Proposal, EntityEventKind.Complete];
}
case MolochTypes.EventKind.Abort: {
return [MolochTypes.EntityKind.Proposal, EntityEventKind.Complete];
}
default:
return null;
}
}
if (network === SupportedNetwork.Compound) {
switch (event) {
case CompoundTypes.EventKind.ProposalCanceled: {
return [CompoundTypes.EntityKind.Proposal, EntityEventKind.Complete];
}
case CompoundTypes.EventKind.ProposalCreated: {
return [CompoundTypes.EntityKind.Proposal, EntityEventKind.Create];
}
case CompoundTypes.EventKind.ProposalExecuted: {
return [CompoundTypes.EntityKind.Proposal, EntityEventKind.Complete];
}
case CompoundTypes.EventKind.ProposalQueued: {
return [CompoundTypes.EntityKind.Proposal, EntityEventKind.Update];
}
case CompoundTypes.EventKind.VoteCast: {
return [CompoundTypes.EntityKind.Proposal, EntityEventKind.Vote];
}
default:
return null;
}
}
if (network === SupportedNetwork.Aave) {
switch (event) {
case AaveTypes.EventKind.ProposalCreated: {
return [AaveTypes.EntityKind.Proposal, EntityEventKind.Create];
}
case AaveTypes.EventKind.VoteEmitted: {
return [AaveTypes.EntityKind.Proposal, EntityEventKind.Vote];
}
case AaveTypes.EventKind.ProposalQueued: {
return [AaveTypes.EntityKind.Proposal, EntityEventKind.Update];
}
case AaveTypes.EventKind.ProposalExecuted:
case AaveTypes.EventKind.ProposalCanceled: {
return [AaveTypes.EntityKind.Proposal, EntityEventKind.Complete];
}
default:
return null;
}
}
if (network === SupportedNetwork.Commonwealth) {
switch (event) {
case CommonwealthTypes.EventKind.ProjectCreated: {
return [CommonwealthTypes.EntityKind.Project, EntityEventKind.Create];
}
// TODO: verify vote is correct here
case CommonwealthTypes.EventKind.ProjectBacked:
case CommonwealthTypes.EventKind.ProjectCurated:
case CommonwealthTypes.EventKind.ProjectWithdraw: {
return [CommonwealthTypes.EntityKind.Project, EntityEventKind.Vote];
}
case CommonwealthTypes.EventKind.ProjectSucceeded:
case CommonwealthTypes.EventKind.ProjectFailed: {
return [CommonwealthTypes.EntityKind.Project, EntityEventKind.Update];
}
default: {
return null;
}
}
}
if (network === SupportedNetwork.Substrate) {
switch (event) {
// SUBSTRATE
// Democracy Events
case SubstrateTypes.EventKind.DemocracyProposed: {
return [
SubstrateTypes.EntityKind.DemocracyProposal,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.DemocracyTabled: {
return [
SubstrateTypes.EntityKind.DemocracyProposal,
EntityEventKind.Complete,
];
}
case SubstrateTypes.EventKind.DemocracyStarted: {
return [
SubstrateTypes.EntityKind.DemocracyReferendum,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.DemocracyVoted: {
return [
SubstrateTypes.EntityKind.DemocracyReferendum,
EntityEventKind.Vote,
];
}
case SubstrateTypes.EventKind.DemocracyPassed: {
return [
SubstrateTypes.EntityKind.DemocracyReferendum,
EntityEventKind.Update,
];
}
case SubstrateTypes.EventKind.DemocracyNotPassed:
case SubstrateTypes.EventKind.DemocracyCancelled:
case SubstrateTypes.EventKind.DemocracyExecuted: {
return [
SubstrateTypes.EntityKind.DemocracyReferendum,
EntityEventKind.Complete,
];
}
// Preimage Events
case SubstrateTypes.EventKind.PreimageNoted: {
return [
SubstrateTypes.EntityKind.DemocracyPreimage,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.PreimageUsed:
case SubstrateTypes.EventKind.PreimageInvalid:
case SubstrateTypes.EventKind.PreimageReaped: {
return [
SubstrateTypes.EntityKind.DemocracyPreimage,
EntityEventKind.Complete,
];
}
// Tip Events
case SubstrateTypes.EventKind.NewTip: {
return [SubstrateTypes.EntityKind.TipProposal, EntityEventKind.Create];
}
case SubstrateTypes.EventKind.TipVoted:
case SubstrateTypes.EventKind.TipClosing: {
return [SubstrateTypes.EntityKind.TipProposal, EntityEventKind.Update];
}
case SubstrateTypes.EventKind.TipClosed:
case SubstrateTypes.EventKind.TipRetracted:
case SubstrateTypes.EventKind.TipSlashed: {
return [
SubstrateTypes.EntityKind.TipProposal,
EntityEventKind.Complete,
];
}
// Treasury Events
case SubstrateTypes.EventKind.TreasuryProposed: {
return [
SubstrateTypes.EntityKind.TreasuryProposal,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.TreasuryRejected:
case SubstrateTypes.EventKind.TreasuryAwarded: {
return [
SubstrateTypes.EntityKind.TreasuryProposal,
EntityEventKind.Complete,
];
}
// Bounty Events
case SubstrateTypes.EventKind.TreasuryBountyProposed: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.TreasuryBountyAwarded: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Update,
];
}
case SubstrateTypes.EventKind.TreasuryBountyBecameActive: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Update,
];
}
case SubstrateTypes.EventKind.TreasuryBountyCanceled: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Complete,
];
}
case SubstrateTypes.EventKind.TreasuryBountyClaimed: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Complete,
];
}
case SubstrateTypes.EventKind.TreasuryBountyExtended: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Update,
];
}
case SubstrateTypes.EventKind.TreasuryBountyRejected: {
return [
SubstrateTypes.EntityKind.TreasuryBounty,
EntityEventKind.Complete,
];
}
// Collective Events
case SubstrateTypes.EventKind.CollectiveProposed: {
return [
SubstrateTypes.EntityKind.CollectiveProposal,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.CollectiveVoted: {
return [
SubstrateTypes.EntityKind.CollectiveProposal,
EntityEventKind.Vote,
];
}
case SubstrateTypes.EventKind.CollectiveApproved: {
return [
SubstrateTypes.EntityKind.CollectiveProposal,
EntityEventKind.Update,
];
}
case SubstrateTypes.EventKind.CollectiveDisapproved:
case SubstrateTypes.EventKind.CollectiveExecuted: {
return [
SubstrateTypes.EntityKind.CollectiveProposal,
EntityEventKind.Complete,
];
}
// Signaling Events
case SubstrateTypes.EventKind.SignalingNewProposal: {
return [
SubstrateTypes.EntityKind.SignalingProposal,
EntityEventKind.Create,
];
}
case SubstrateTypes.EventKind.SignalingCommitStarted:
case SubstrateTypes.EventKind.SignalingVotingStarted: {
return [
SubstrateTypes.EntityKind.SignalingProposal,
EntityEventKind.Update,
];
}
case SubstrateTypes.EventKind.SignalingVotingCompleted: {
return [
SubstrateTypes.EntityKind.SignalingProposal,
EntityEventKind.Complete,
];
}
default: {
return null;
}
}
}
return null;
}
export function isEntityCompleted(entityEvents: CWEvent[]): boolean {
return entityEvents.some(({ network, data: { kind } }) => {
const entityData = eventToEntity(network, kind);
return entityData && entityData[1] === EntityEventKind.Complete;
});
}