univo 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,354 @@
1
+ import { RpcBlock, RpcTransactionReceipt } from 'viem';
2
+ import { Storage } from '../metadata.js';
3
+
4
+ type CamelToSnake<T extends string, P extends string = ""> = string extends T ? string : T extends `${infer C0}${infer R}` ? CamelToSnake<R, `${P}${C0 extends Lowercase<C0> ? "" : "_"}${Lowercase<C0>}`> : P;
5
+ type Flatten<T> = {
6
+ [K in keyof T as CamelToSnake<Extract<K, string>>]: T[K];
7
+ };
8
+
9
+ type Rpc = {
10
+ /**
11
+ * All available methods that can be requested
12
+ */
13
+ request: Record<string, any>;
14
+ /**
15
+ * All available event types to subscribe to a stream of values
16
+ */
17
+ subscribe: Record<string, any>;
18
+ };
19
+ /**
20
+ * Indexer -----------------------------------------------------------------------------------------------------------------------------------
21
+ */
22
+ type IndexerRpc = {
23
+ request: {
24
+ /**
25
+ * Accepts a chain identifier
26
+ * @returns Height of the last finalized block indexed
27
+ */
28
+ public_getFinalizedHeight: (chain: `0x${string}`) => Promise<number>;
29
+ /**
30
+ * Accepts a reorganised block and deletes all related events from storage
31
+ */
32
+ public_deleteReorganisedHead: (head: Head) => Promise<void>;
33
+ /**
34
+ * Accepts an unfinalized head and writes all events to storage
35
+ */
36
+ public_writeUnfinalizedHead: (head: Head) => Promise<void>;
37
+ /**
38
+ * Accepts a chain identifier and caches its current finalized height
39
+ */
40
+ public_writeFinalizedHeight: (chain: `0x${string}`) => Promise<void>;
41
+ /**
42
+ * Accepts a finalized head and executes all actions
43
+ */
44
+ public_writeFinalizedHead: (head: Head) => Promise<void>;
45
+ /**
46
+ * Accepts a chain identifier and finalizes the indexer
47
+ */
48
+ public_finalize: (chain: `0x${string}`) => Promise<void>;
49
+ /**
50
+ * @returns Specified metadata about the indexer
51
+ */
52
+ private_getMetadata: () => Promise<Metadata>;
53
+ /**
54
+ * @returns All events defined on this indexer
55
+ */
56
+ private_getEvents: () => Promise<{
57
+ id: string;
58
+ filters: Flatten<Filter>[];
59
+ }[]>;
60
+ /**
61
+ * Accepts a list of events and a batch of minified block data and writes to storage
62
+ * @returns A list of failures that occured for each event and block pair
63
+ */
64
+ private_writeEvents: (params: {
65
+ events: string[];
66
+ blocks: Block[];
67
+ }) => Promise<{
68
+ failures: Result[];
69
+ }>;
70
+ /**
71
+ * Accepts a block head and writes events to storage
72
+ * @returns A list of results and block keys that were accessed as those events were written to storage
73
+ */
74
+ private_writeEventsAndGetKeys: (params: {
75
+ events: string[];
76
+ head: Head;
77
+ }) => Promise<{
78
+ results: Result[];
79
+ keys: string[];
80
+ }>;
81
+ };
82
+ subscribe: Record<string, never>;
83
+ };
84
+
85
+ /**
86
+ * Types -----------------------------------------------------------------------------------------------------------------------------------
87
+ */
88
+ type Protocol = "http" | "wss" | "local";
89
+ type Transport<R extends Rpc, P extends Protocol = Protocol> = {
90
+ /**
91
+ * The underlying transport protocol.
92
+ */
93
+ protocol: P;
94
+ /**
95
+ * Performs an RPC request
96
+ * @returns The RPC response
97
+ */
98
+ request: <M extends keyof R["request"]>(opts: {
99
+ method: M;
100
+ signal?: AbortSignal;
101
+ params: Parameters<R["request"][M]>;
102
+ }) => Promise<Awaited<ReturnType<R["request"][M]>>>;
103
+ /**
104
+ * Subscribes to specific event to receive and invoke a callback for a stream of values
105
+ * @returns An async function to unsubscribe from the stream
106
+ */
107
+ subscribe: <M extends keyof R["subscribe"]>(param: M, handler: (message: R["subscribe"][M]) => void) => Promise<() => Promise<void>>;
108
+ };
109
+ /**
110
+ * Local -----------------------------------------------------------------------------------------------------------------------------------
111
+ */
112
+ declare function local<R extends Rpc>(rpc: R): Transport<R, "local">;
113
+ declare function wss<R extends Rpc>(url: string, opts?: {
114
+ quiet?: boolean;
115
+ }): Transport<R, "wss">;
116
+ /**
117
+ * HTTP -----------------------------------------------------------------------------------------------------------------------------------
118
+ */
119
+ declare function http<R extends Rpc>(url: string, opts?: {
120
+ signingKey?: string;
121
+ }): Transport<R, "http">;
122
+
123
+ /**
124
+ * The minimum RPC data required to process a block.
125
+ *
126
+ * Extend this interface in your application to expose additional RPC methods to every event:
127
+ *
128
+ * ```ts
129
+ * import "univo";
130
+ *
131
+ * declare module "univo" {
132
+ * interface Block {
133
+ * eth_getBalance: `0x${string}`;
134
+ * }
135
+ * }
136
+ * ```
137
+ *
138
+ * Your `getBlock` function must also return any required fields added by the augmentation.
139
+ */
140
+ interface Block {
141
+ eth_chainId: `0x${string}`;
142
+ eth_getBlockByNumber: RpcBlock<"latest", true>;
143
+ eth_getBlockReceipts: RpcTransactionReceipt[];
144
+ }
145
+ /**
146
+ * Filters -----------------------------------------------------------------------------------------------------------------------------------
147
+ */
148
+ type Filter = {
149
+ /** Index blocks with this chain id */
150
+ chain: number;
151
+ /** Index blocks from this start block (inclusive) */
152
+ fromBlock: number;
153
+ /** Index blocks until this stop block (inclusive) */
154
+ toBlock?: number;
155
+ /** Index blocks where this event topic was emitted */
156
+ event?: `0x${string}`;
157
+ /** Index blocks that involve this address */
158
+ address?: `0x${string}`;
159
+ };
160
+ /**
161
+ * Events -----------------------------------------------------------------------------------------------------------------------------------
162
+ */
163
+ type Event<TEvent> = {
164
+ /**
165
+ * A human-readable identifier for the event.
166
+ */
167
+ id: string;
168
+ /**
169
+ * Filters let you define what specific blocks you want this event to index.
170
+ *
171
+ * Blockchains are massive datasets and most of the time we are only ever interested in small portions of it.
172
+ * Sometimes that can be specific events like ERC20 transfers and other times it could be all events emitted
173
+ * by a specific contract. Filters provide a simple way for an event to define exactly which blocks we want
174
+ * to index.
175
+ *
176
+ * Filters reduce costs and improve backfill performance by ensuring we only index the blocks we need and
177
+ * ignore the blocks that don't have the data we are interested in.
178
+ *
179
+ * By default your event will not index any blocks, you must opt-in to indexing by providing atleast one filter.
180
+ *
181
+ * Each property in the filter operates like an AND statement. For example, if you specifiy an `address` and
182
+ * an `event` it implies that you only want to index blocks where the specific `address` emitted the specific
183
+ * `event` topic provided.
184
+ *
185
+ * However, when multiple filters are defined for a given event those filters operate like an OR statement. For
186
+ * example, if we define a second filter looking for a different `address` and `event` our event will now index
187
+ * any block that matches either the first filter or the second filter.
188
+ *
189
+ * Note that filters are a rudimentary method to dramatically reduce the number of blocks your application needs
190
+ * to index. Any advanced filtering should be performed in the event `handler` itself.
191
+ */
192
+ filters: Filter[];
193
+ /**
194
+ * Synchronously transforms a raw block into a list of events.
195
+ *
196
+ * When a block matches any of filters defined by your event it will be passed to this function to be synchronously
197
+ * transformed into a list of structured events.
198
+ *
199
+ * The input uses the shared `Block` interface. Extend it with `declare module "univo"` in your application
200
+ * to expose additional RPC methods returned by your indexer's `getBlock` function.
201
+ *
202
+ * The returned output value should be an array containg any valid JavaScript values. Each event that you return from
203
+ * your handler should not depend on any information outside of the input block data. It should directly map a given
204
+ * input (the raw block) to a given output (structured events). This ensures that handler remains idempotent and that
205
+ * repeated calls with the same input block produce the same output events.
206
+ */
207
+ handler: (block: Block) => TEvent[];
208
+ /**
209
+ * Storage adapter to persist events in your off-chain storage.
210
+ */
211
+ storage: {
212
+ /**
213
+ * Upserts a batch of events into your storage system.
214
+ *
215
+ * After a block is transformed into a list of structured events by your `handler` function, that batch
216
+ * is passed to this `upsert` function so they can be upserted into your storage system.
217
+ *
218
+ * This function must be idempotent. Functionally, this means that if the same batch of events is upserted
219
+ * multiple times it only produces a single set of events in your storage system.
220
+ */
221
+ upsert: (events: TEvent[]) => Promise<void>;
222
+ /**
223
+ * Deletes a batch of events from your storage system.
224
+ *
225
+ * When your indexer encounters a chain reorganisation, this `delete` function will be invoked with the
226
+ * same batch of events created by that reorganised block. This ensures that any events that were previously
227
+ * upserted into your storage system that are no longer part of the canonical chain are safely removed.
228
+ */
229
+ delete: (events: TEvent[]) => Promise<void>;
230
+ };
231
+ };
232
+ declare function defineEvent<TEvent = unknown>(event: Event<TEvent>): Event<TEvent>;
233
+ /**
234
+ * Actions -----------------------------------------------------------------------------------------------------------------------------------
235
+ */
236
+ type Action<TEvent> = {
237
+ /**
238
+ * A human-readable identifier for the action.
239
+ */
240
+ id: string;
241
+ /**
242
+ * The on-chain event that should invoke this action.
243
+ */
244
+ event: Event<TEvent>;
245
+ /**
246
+ * The action you want to execute when the event _finalizes_ on-chain.
247
+ *
248
+ * Actions are processed during realtime indexing only and are never invoked during historical
249
+ * backfills. Common actions include payment notifications for confirming payments or customer
250
+ * deposits, transaction monitoring for KYC/AML compliance tracking, DeFi protocol monitoring,
251
+ * or wallet activity notifications or alerts.
252
+ *
253
+ * Actions may be invoked multiple times. It is important that your application code is
254
+ * resilient to this by making use of an idempotency key (usually the id of the event passed
255
+ * to your handler).
256
+ *
257
+ * Actions can be extremely powerful when combined with a durable execution framework like
258
+ * Temporal, Inngest, Trigger.dev, Cloudflare Workflows, or Restate.dev to perform more
259
+ * advanced workflows that allow you to chain together multiple steps and handle retries.
260
+ *
261
+ * Actions operate with at-least-once delivery. This means that your actions may be invoked
262
+ * multiple times. It is important that your handler is resilient to this by making use of an
263
+ * idempotency key (usually the identifier in the event passed to your handler). We guarantee
264
+ * that the indexer will not finalize a given block until it achieves a non-erroring execution
265
+ * from your action.
266
+ */
267
+ handler: (event: TEvent) => Promise<void> | void;
268
+ };
269
+ declare function defineAction<TEvent>(action: Action<TEvent>): Action<TEvent>;
270
+ /**
271
+ * Indexer -----------------------------------------------------------------------------------------------------------------------------------
272
+ */
273
+ type Head = {
274
+ hash: `0x${string}`;
275
+ chain: `0x${string}`;
276
+ number: `0x${string}`;
277
+ parent_hash: `0x${string}`;
278
+ };
279
+ type Metadata = {
280
+ version: string;
281
+ language: string;
282
+ };
283
+ type Result = {
284
+ status: string;
285
+ event_id: string;
286
+ hash: `0x${string}`;
287
+ chain: `0x${string}`;
288
+ number: `0x${string}`;
289
+ parent_hash: `0x${string}`;
290
+ created_at: number;
291
+ };
292
+ type AnyEvent = Omit<Event<any>, "storage"> & {
293
+ storage: {
294
+ upsert(events: any[]): Promise<void>;
295
+ delete(events: any[]): Promise<void>;
296
+ };
297
+ };
298
+ type AnyAction = Omit<Action<any>, "event" | "handler"> & {
299
+ event: AnyEvent;
300
+ handler(event: any): Promise<void> | void;
301
+ };
302
+ type Indexer = {
303
+ /**
304
+ * Silences all logs including errors.
305
+ *
306
+ * Logs are emitted based on the environment LOG_LEVEL. Set `quiet: true` to surpress all logs.
307
+ * Available log options are `DEBUG`, `INFO`, `WARN`, and `ERROR`.
308
+ */
309
+ quiet?: boolean;
310
+ /**
311
+ * Events
312
+ */
313
+ events: AnyEvent[];
314
+ /**
315
+ * Actions
316
+ */
317
+ actions: AnyAction[];
318
+ /**
319
+ * Storage interface for durably persisting indexer metadata.
320
+ *
321
+ * Functionally, storing metadata is fundamental to ensure the correct operation of your indexer. It
322
+ * ensures that you indexer recovers from downtime and ensures that all blocks are processed correctly during
323
+ * chain reorganisations.
324
+ */
325
+ metadataStorage: Storage;
326
+ /**
327
+ * Loads raw block data in realtime from a trusted set of RPC sources.
328
+ *
329
+ * Note that historical backfills do not use this function to load block data.
330
+ *
331
+ * The response from this function determines the input data to your `handler` functions in each event.
332
+ * Generally, the shape is a list of RPC methods with their corresponding value from an RPC node. This
333
+ * generic format allows you support the full range of RPC data available to you from your node. This
334
+ * flexibility is important in cases where some methods are supported on specific chains only.
335
+ *
336
+ * Note that there is a minimum set of RPC methods expected on the response notably `eth_chainId`,
337
+ * `eth_getBlockByNumber` and `eth_getBlockReceipts`. These are expected so that we can safely match
338
+ * each block processed against your event filters. Additional methods may be returned. Remember to
339
+ * extend the shared `Block` interface using `declare module "univo"` in your application.
340
+ *
341
+ * Note that we provide only the block number to look up block data. During a chain reorganisation, this
342
+ * makes it possible for different RPC calls to return data for different block hashes. We manually verify
343
+ * that all methods return the expected block hash. However, for any custom methods you add you must manually
344
+ * verify the block hash is consistent with the other known methods.
345
+ */
346
+ getBlock: (block: {
347
+ chain: `0x${string}`;
348
+ number: `0x${string}` | "finalized";
349
+ }) => Promise<Block>;
350
+ };
351
+ declare function defineIndexer(opts: Indexer): Transport<IndexerRpc, "local">;
352
+
353
+ export { defineEvent as a, defineAction as b, defineIndexer as d, http as h, local as l, wss as w };
354
+ export type { Action as A, Block as B, Event as E, Filter as F, Head as H, IndexerRpc as I, Metadata as M, Rpc as R, Transport as T, Indexer as c, Result as e };