-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.ts
175 lines (153 loc) · 5.79 KB
/
types.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
import {Abi} from 'abitype';
import {EIP1193DATA, EIP1193Log, EIP1193QUANTITY} from 'eip-1193';
import {DecodeEventLogReturnType} from 'viem';
import {NumberifiedLog} from './internal/decoding/LogEventFetcher';
import {LogTransactionData} from './internal/engine/ethereum';
import {LogFetcherConfig} from './internal/engine/LogFetcher';
import {JSONObject} from './internal/types';
export type EventBlock<ABI extends Abi> = {
number: number;
hash: string;
events: LogEvent<ABI>[]; //this could be replacec by start: number;end: number but we would need access to the old coreresponding events
};
export type LogParsedData<ABI extends Abi> = DecodeEventLogReturnType<ABI, string, `0x${string}`[], `0x${string}`>;
export type BaseLogEvent<Extra extends JSONObject | undefined = undefined> = NumberifiedLog & {
removedStreamID?: number;
} & {
extra: Extra;
blockTimestamp?: number;
transaction?: LogTransactionData;
};
export type ParsedLogEvent<ABI extends Abi, Extra extends JSONObject | undefined = undefined> = BaseLogEvent<Extra> &
LogParsedData<ABI>;
export type LogEventWithParsingFailure<Extra extends JSONObject | undefined = undefined> = BaseLogEvent<Extra> & {
decodeError: string;
};
export type LogEvent<ABI extends Abi, Extra extends JSONObject | undefined = undefined> =
| ParsedLogEvent<ABI, Extra>
| LogEventWithParsingFailure<Extra>;
export type EventProcessor<ABI extends Abi, ProcessResultType = void> = {
getVersionHash(): string;
load: (
source: IndexingSource<ABI>,
streamConfig: UsedStreamConfig
) => Promise<{state: ProcessResultType; lastSync: LastSync<ABI>} | undefined>;
process: (eventStream: LogEvent<ABI>[], lastSync: LastSync<ABI>) => Promise<ProcessResultType>;
reset: () => Promise<void>;
clear: () => Promise<void>;
};
export type EventProcessorWithInitialState<ABI extends Abi, ProcessResultType, ProcessorConfig> = EventProcessor<
ABI,
ProcessResultType
> & {
createInitialState(): ProcessResultType;
configure(config: ProcessorConfig): void;
};
export type IncludedEIP1193Log = EIP1193Log & {
blockNumber: EIP1193DATA;
logIndex: EIP1193DATA;
blockHash: EIP1193DATA;
transactionIndex: EIP1193QUANTITY;
transactionHash: EIP1193DATA;
};
export type ContextIdentifier = {source: {startBlock: number; hash: string}[]; config: string; processor: string};
export type LastSync<ABI extends Abi> = {
context: ContextIdentifier;
latestBlock: number;
lastFromBlock: number;
lastToBlock: number;
unconfirmedBlocks: EventBlock<ABI>[];
};
export type ContractData<ABI extends Abi> = {
readonly abi: ABI;
readonly address: `0x${string}`;
readonly startBlock?: number;
readonly history?: readonly {readonly abi: ABI; readonly startBlock?: number}[];
};
export type AllContractData<ABI extends Abi> = {
readonly abi: ABI;
readonly startBlock?: number;
};
export type IndexingSource<ABI extends Abi> = {
readonly contracts: readonly ContractData<ABI>[] | AllContractData<ABI>;
readonly chainId: string;
};
export type StreamFecther<ABI extends Abi> = (
source: IndexingSource<ABI>,
fromBlock: number
) => Promise<{lastSync: LastSync<ABI>; eventStream: LogEvent<ABI>[]} | undefined>;
export type StreamSaver<ABI extends Abi> = (
source: IndexingSource<ABI>,
stream: {
lastSync: LastSync<ABI>;
eventStream: LogEvent<ABI>[];
}
) => Promise<void>;
export type StreamClearer<ABI extends Abi> = (source: IndexingSource<ABI>) => Promise<void>;
type OptionsFlags<Type> = {
[Property in keyof Type]: boolean;
};
type LogValuesFlags = OptionsFlags<NumberifiedLog>;
export type UsedStreamConfig = ProvidedStreamConfig & {
finality: number;
};
export type ProvidedStreamConfig = {
finality?: number;
alwaysFetchTimestamps?: boolean;
alwaysFetchTransactions?: boolean;
parse?: LogParseConfig;
};
export type FetchConfig = Omit<LogFetcherConfig, 'filters'>;
export type ProvidedIndexerConfig<ABI extends Abi> = {
fetch?: FetchConfig;
stream?: ProvidedStreamConfig;
providerSupportsETHBatch?: boolean;
feedBatchSize?: number;
keepStream?: ExistingStream<ABI>;
};
export type UsedIndexerConfig<ABI extends Abi> = ProvidedIndexerConfig<ABI> & {
stream: UsedStreamConfig;
feedBatchSize: number;
};
export type ExistingStream<ABI extends Abi> = {
fetchFrom: StreamFecther<ABI>;
saveNewEvents: StreamSaver<ABI>;
clear: StreamClearer<ABI>;
};
export type LogParseConfig = {
parseAllEventsIrrespectiveOfAddresses?: boolean;
logValues?: LogValuesFlags;
filters?: {
// for each event name we can specify a list of filter
// each filter is an array of (topic or topic[])
// so this is an array of array of (topic | topic[])
[eventName: string]: (`0x${string}` | `0x${string}`[])[][];
// Note we do not provide type arg here (could have done it via abitype) because multiple event could share the same order
};
};
export type ProcessorContext<ABI extends Abi, ProcessorConfig> = ProcessorConfig extends undefined
? {
readonly source: IndexingSource<ABI>;
version?: string;
}
: {
readonly source: IndexingSource<ABI>;
readonly config: ProcessorConfig;
version?: string;
};
export type AllData<ABI extends Abi, ProcessResultType, Extra> = {
state: ProcessResultType;
lastSync: LastSync<ABI>;
} & Extra;
export type ExistingStateFecther<ABI extends Abi, ProcessResultType, Extra, ProcessorConfig> = (
context: ProcessorContext<ABI, ProcessorConfig>
) => Promise<AllData<ABI, ProcessResultType, Extra>>;
export type StateSaver<ABI extends Abi, ProcessResultType, Extra, ProcessorConfig> = (
context: ProcessorContext<ABI, ProcessorConfig>,
all: AllData<ABI, ProcessResultType, Extra>
) => Promise<void>;
export type KeepState<ABI extends Abi, ProcessResultType, Extra, ProcessorConfig> = {
fetch: ExistingStateFecther<ABI, ProcessResultType, Extra, ProcessorConfig>;
save: StateSaver<ABI, ProcessResultType, Extra, ProcessorConfig>;
clear: (context: ProcessorContext<ABI, ProcessorConfig>) => Promise<void>;
};