/
index.ts
83 lines (73 loc) · 2.09 KB
/
index.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
import { Result } from '@ethersproject/abi';
import { TransactionReceipt } from '@ethersproject/abstract-provider';
import { TypedEvent } from '@nomad-xyz/contracts-core';
export class Annotated<U extends Result, T extends TypedEvent<U>> {
readonly domain: number;
readonly eventName?: string;
readonly event: T;
readonly receipt: TransactionReceipt;
constructor(
domain: number,
receipt: TransactionReceipt,
event: T,
callerKnowsWhatTheyAreDoing = false,
) {
if (!callerKnowsWhatTheyAreDoing) {
throw new Error('Please instantiate using fromEvent or fromEvents');
}
this.domain = domain;
this.receipt = receipt;
this.eventName = event.eventSignature?.split('(')[0];
this.event = event;
}
static async fromEvent<U extends Result, T extends TypedEvent<U>>(
domain: number,
event: T,
): Promise<Annotated<U, T>> {
const receipt = await event.getTransactionReceipt();
return new Annotated(domain, receipt, event, true);
}
static async fromEvents<U extends Result, T extends TypedEvent<U>>(
domain: number,
events: T[],
): Promise<Annotated<U, T>[]> {
return Promise.all(
events.map(async (event) => Annotated.fromEvent(domain, event)),
);
}
get contractAddress(): string {
// ok to use ! assertion here as we assume that the event is in the receipt
const address = this.receipt.logs.find(
(log) => log.logIndex === this.event.logIndex,
)?.address;
if (!address)
throw new Error('Missing receipt. Class is in an inconsistent state');
return address;
}
get transactionHash(): string {
return this.receipt.transactionHash;
}
get blockNumber(): number {
return this.receipt.blockNumber;
}
get blockHash(): string {
return this.receipt.blockHash;
}
}
export type {
AnnotatedDispatch,
AnnotatedUpdate,
AnnotatedProcess,
AnnotatedLifecycleEvent,
NomadLifecyleEvent,
DispatchEvent,
ProcessEvent,
UpdateEvent,
UpdateArgs,
UpdateTypes,
ProcessArgs,
ProcessTypes,
DispatchArgs,
DispatchTypes,
} from './nomadEvents';
export * from './fetch';