@apibara/indexer 0.1.1 → 0.2.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.
Files changed (45) hide show
  1. package/dist/config.d.ts +2 -2
  2. package/dist/config.test-d.d.ts +1 -0
  3. package/dist/config.test-d.js +38 -0
  4. package/dist/config.test-d.js.map +1 -0
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/sink/console.d.ts +1 -1
  9. package/dist/sink/console.test-d.d.ts +1 -0
  10. package/dist/sink/console.test-d.js +12 -0
  11. package/dist/sink/console.test-d.js.map +1 -0
  12. package/dist/sink/mongo.d.ts +3 -1
  13. package/dist/sink/parquet.d.ts +1 -1
  14. package/dist/sink/postgres.d.ts +1 -1
  15. package/dist/sink/webhook.d.ts +1 -1
  16. package/dist/starknet/block.d.ts +1 -1
  17. package/dist/starknet/felt.d.ts +3 -0
  18. package/dist/starknet/felt.js +12 -0
  19. package/dist/starknet/felt.js.map +1 -0
  20. package/dist/starknet/felt.test.d.ts +1 -0
  21. package/dist/starknet/felt.test.js +14 -0
  22. package/dist/starknet/felt.test.js.map +1 -0
  23. package/dist/starknet/filter.d.ts +171 -1
  24. package/dist/starknet/filter.test-d.d.ts +1 -0
  25. package/dist/starknet/filter.test-d.js +166 -0
  26. package/dist/starknet/filter.test-d.js.map +1 -0
  27. package/dist/starknet/index.d.ts +4 -4
  28. package/dist/starknet/index.js +2 -2
  29. package/dist/starknet/index.js.map +1 -1
  30. package/package.json +51 -10
  31. package/src/config.test-d.ts +55 -0
  32. package/src/config.ts +47 -0
  33. package/src/index.ts +1 -0
  34. package/src/sink/console.test-d.ts +14 -0
  35. package/src/sink/console.ts +11 -0
  36. package/src/sink/mongo.ts +14 -0
  37. package/src/sink/parquet.ts +9 -0
  38. package/src/sink/postgres.ts +10 -0
  39. package/src/sink/webhook.ts +12 -0
  40. package/src/starknet/block.ts +181 -0
  41. package/src/starknet/felt.test.ts +19 -0
  42. package/src/starknet/felt.ts +16 -0
  43. package/src/starknet/filter.test-d.ts +197 -0
  44. package/src/starknet/filter.ts +201 -0
  45. package/src/starknet/index.ts +10 -0
@@ -0,0 +1,55 @@
1
+ import { assertType, describe, test } from "vitest";
2
+
3
+ import { Config } from "./config";
4
+
5
+ describe("Config", () => {
6
+ test("without any network or sink type", () => {
7
+ assertType<Config>({
8
+ network: "test-network",
9
+ filter: {},
10
+ sinkType: "test-sink",
11
+ sinkOptions: {},
12
+ });
13
+ });
14
+
15
+ test("with restrictions on network and sink", () => {
16
+ type Network = {
17
+ network: "test-network";
18
+ filter: { a: number };
19
+ };
20
+
21
+ type Sink = {
22
+ sinkType: "test-sink";
23
+ sinkOptions: {
24
+ b: string;
25
+ };
26
+ };
27
+
28
+ assertType<Config<Network, Sink>>({
29
+ network: "test-network",
30
+ filter: {
31
+ a: 1,
32
+ },
33
+ sinkType: "test-sink",
34
+ sinkOptions: {
35
+ b: "test",
36
+ },
37
+ });
38
+ });
39
+
40
+ test("network options are required", () => {
41
+ // @ts-expect-error - missing network options
42
+ assertType<Config>({
43
+ sinkType: "test-sink",
44
+ sinkOptions: {},
45
+ });
46
+ });
47
+
48
+ test("sink options are required", () => {
49
+ // @ts-expect-error - missing sink options
50
+ assertType<Config>({
51
+ network: "test-network",
52
+ filter: {},
53
+ });
54
+ });
55
+ });
package/src/config.ts ADDED
@@ -0,0 +1,47 @@
1
+ /** Sink-related options. */
2
+ export type SinkOptions = {
3
+ /** Sink type. */
4
+ sinkType: string;
5
+ /** Sink options. */
6
+ sinkOptions: object;
7
+ };
8
+
9
+ /** Network-specific options. */
10
+ export type NetworkOptions = {
11
+ /** Network name. */
12
+ network: string;
13
+ /** Data filter. */
14
+ filter: object;
15
+ };
16
+
17
+ /** Data finality. */
18
+ export type Finality =
19
+ | "DATA_STATUS_FINALIZED"
20
+ | "DATA_STATUS_ACCEPTED"
21
+ | "DATA_STATUS_PENDING";
22
+
23
+ /** Stream-related options. */
24
+ export type StreamOptions = {
25
+ /** The Apibara DNA stream url, e.g. `mainnet.starknet.a5a.ch`. */
26
+ streamUrl?: string;
27
+ /** Maximum message size, e.g. `50Mb` */
28
+ maxMessageSize?: string;
29
+ /** Additional metadata to send when connecting to the stream, in `key: value` form. */
30
+ metadata?: string[];
31
+ /** The Apibara DNA stream auth token. */
32
+ authToken?: string;
33
+ };
34
+
35
+ export type Config<
36
+ TNetworkOptions extends NetworkOptions = NetworkOptions,
37
+ TSink extends SinkOptions = SinkOptions,
38
+ > = TSink &
39
+ TNetworkOptions &
40
+ StreamOptions & {
41
+ /** How many historical blocks to process in a single batch. */
42
+ batchSize?: number;
43
+ /** Finality of the data to process. */
44
+ finality?: Finality;
45
+ /** Start streaming data from this block. */
46
+ startingBlock?: number;
47
+ };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./config";
@@ -0,0 +1,14 @@
1
+ import { assertType, describe, test } from "vitest";
2
+
3
+ import { Console } from "./console";
4
+
5
+ describe("Console", () => {
6
+ test("accepts any option", () => {
7
+ assertType<Console>({
8
+ sinkType: "console",
9
+ sinkOptions: {
10
+ hello: "world",
11
+ },
12
+ });
13
+ });
14
+ });
@@ -0,0 +1,11 @@
1
+ /** Console sink type. */
2
+ export type Console = {
3
+ sinkType: "console";
4
+
5
+ /** Accept any options.
6
+ *
7
+ * @remarks We do this so that users can quickly switch to the console sink to debug,
8
+ * without having to remove the options from the config file.
9
+ */
10
+ sinkOptions: object;
11
+ };
@@ -0,0 +1,14 @@
1
+ /** MongoDB sink options */
2
+ export type Mongo = {
3
+ sinkType: "mongo";
4
+ sinkOptions: {
5
+ /** MongoDB connection string. */
6
+ connectionString?: string;
7
+ /** Target database name. */
8
+ database?: string;
9
+ /** Target collection name. */
10
+ collectionName?: string;
11
+ /** Enable entity mode. */
12
+ entityMode?: boolean;
13
+ };
14
+ };
@@ -0,0 +1,9 @@
1
+ export type Parquet = {
2
+ sinkType: "parquet";
3
+ sinkOptions: {
4
+ /** Target output directory. */
5
+ outputDir?: string;
6
+ /** How many blocks to include in each file. */
7
+ batchSize?: number;
8
+ };
9
+ };
@@ -0,0 +1,10 @@
1
+ /** PostgreSQL sink options. */
2
+ export type Postgres = {
3
+ sinkType: "postgres";
4
+ sinkOptions: {
5
+ /** Postgres connection string. */
6
+ connectionString?: string;
7
+ /** Target table name. */
8
+ tableName?: string;
9
+ };
10
+ };
@@ -0,0 +1,12 @@
1
+ /** Webhook sink options */
2
+ export type Webhook = {
3
+ sinkType: "webhook";
4
+ sinkOptions: {
5
+ /** Target URL. */
6
+ targetUrl?: string;
7
+ /** Additional headers to send with the request, `key: value` format. */
8
+ header?: string[];
9
+ /** Send the data returned from the transform function as the request body. */
10
+ raw?: boolean;
11
+ };
12
+ };
@@ -0,0 +1,181 @@
1
+ import { FieldElement } from "./felt";
2
+
3
+ export type Block = {
4
+ header?: BlockHeader;
5
+ transactions: TransactionWithReceipt[];
6
+ events: EventWithTransaction[];
7
+ l2ToL1Messages: L2ToL1MessageWithTransaction[];
8
+ stateUpdate: StateUpdate;
9
+ };
10
+
11
+ export type BlockHeader = {
12
+ blockHash: FieldElement;
13
+ parentBlockHash: FieldElement;
14
+ blockNumber: string;
15
+ sequencerAddress: FieldElement;
16
+ newRoot: FieldElement;
17
+ timestamp: string;
18
+ };
19
+
20
+ export type TransactionWithReceipt = {
21
+ transaction: Transaction;
22
+ receipt: TransactionReceipt;
23
+ };
24
+
25
+ export type EventWithTransaction = {
26
+ transaction: Transaction;
27
+ receipt: TransactionReceipt;
28
+ event: Event;
29
+ };
30
+
31
+ export type L2ToL1MessageWithTransaction = {
32
+ transaction: Transaction;
33
+ message: L2ToL1Message;
34
+ };
35
+
36
+ export type Transaction = {
37
+ meta: TransactionMeta;
38
+ } & TransactionBody;
39
+
40
+ export type TransactionBody =
41
+ | {
42
+ invokeV0: InvokeTransactionV0;
43
+ }
44
+ | {
45
+ invokeV1: InvokeTransactionV1;
46
+ }
47
+ | {
48
+ deploy: DeployTransaction;
49
+ }
50
+ | {
51
+ declare: DeclareTransaction;
52
+ }
53
+ | {
54
+ deployAccount: DeployAccountTransaction;
55
+ }
56
+ | {
57
+ l1Handler: L1HandlerTransaction;
58
+ };
59
+
60
+ export type TransactionMeta = {
61
+ hash: FieldElement;
62
+ maxFee: FieldElement;
63
+ signature: FieldElement[];
64
+ nonce: FieldElement;
65
+ version: string;
66
+ };
67
+
68
+ export type InvokeTransactionV0 = {
69
+ contractAddress: FieldElement;
70
+ entryPointSelector: FieldElement;
71
+ calldata: FieldElement[];
72
+ };
73
+
74
+ export type InvokeTransactionV1 = {
75
+ contractAddress: FieldElement;
76
+ entryPointSelector: FieldElement;
77
+ calldata: FieldElement[];
78
+ };
79
+
80
+ export type DeployTransaction = {
81
+ constructorCalldata: FieldElement[];
82
+ contractAddressSalt: FieldElement;
83
+ classHash: FieldElement;
84
+ };
85
+
86
+ export type DeclareTransaction = {
87
+ classHash: FieldElement;
88
+ senderAddress: FieldElement;
89
+ compiledClassHash: FieldElement;
90
+ };
91
+
92
+ export type DeployAccountTransaction = {
93
+ constructorCalldata: FieldElement[];
94
+ contractAddressSalt: FieldElement;
95
+ classHash: FieldElement;
96
+ };
97
+
98
+ export type L1HandlerTransaction = {
99
+ contractAddress: FieldElement;
100
+ entryPointSelector: FieldElement;
101
+ calldata: FieldElement[];
102
+ };
103
+
104
+ export type TransactionReceipt = {
105
+ executionStatus: ExecutionStatus;
106
+ transactionHash: FieldElement;
107
+ transactionIndex: string;
108
+ actualFee: FieldElement;
109
+ contractAddress: FieldElement;
110
+ l2ToL1Messages: L2ToL1Message[];
111
+ events: Event[];
112
+ revertReason?: string;
113
+ };
114
+
115
+ export type ExecutionStatus =
116
+ | "EXECUTION_STATUS_UNSPECIFIED"
117
+ | "EXECUTION_STATUS_SUCCEEDED"
118
+ | "EXECUTION_STATUS_REVERTED";
119
+
120
+ export type Event = {
121
+ index: number;
122
+ fromAddress: FieldElement;
123
+ keys: FieldElement[];
124
+ data: FieldElement[];
125
+ };
126
+
127
+ export type L2ToL1Message = {
128
+ index: number;
129
+ fromAddress: FieldElement;
130
+ toAddress: FieldElement;
131
+ payload: FieldElement[];
132
+ };
133
+
134
+ export type StateUpdate = {
135
+ newRoot: FieldElement;
136
+ oldRoot: FieldElement;
137
+ stateDiff: StateDiff;
138
+ };
139
+
140
+ export type StateDiff = {
141
+ storageDiffs: StorageDiff[];
142
+ declaredContracts: DeclaredContract[];
143
+ deployedContracts: DeployedContract[];
144
+ nonces: NonceUpdate[];
145
+ declaredClasses: DeclaredClass[];
146
+ replacedClasses: ReplacedClass[];
147
+ };
148
+
149
+ export type StorageDiff = {
150
+ contractAddress: FieldElement;
151
+ storageEntries: StorageEntry[];
152
+ };
153
+
154
+ export type StorageEntry = {
155
+ key: FieldElement;
156
+ value: FieldElement;
157
+ };
158
+
159
+ export type DeclaredContract = {
160
+ classHash: FieldElement;
161
+ };
162
+
163
+ export type DeclaredClass = {
164
+ classHash: FieldElement;
165
+ compiledClassHash: FieldElement;
166
+ };
167
+
168
+ export type ReplacedClass = {
169
+ contractAddress: FieldElement;
170
+ classHash: FieldElement;
171
+ };
172
+
173
+ export type DeployedContract = {
174
+ contractAddress: FieldElement;
175
+ classHash: FieldElement;
176
+ };
177
+
178
+ export type NonceUpdate = {
179
+ contractAddress: FieldElement;
180
+ nonce: FieldElement;
181
+ };
@@ -0,0 +1,19 @@
1
+ import { describe, expect, test } from "vitest";
2
+
3
+ import { FieldElement } from "./felt";
4
+
5
+ describe("FieldElement", () => {
6
+ test("parse address", () => {
7
+ FieldElement.parse(
8
+ "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
9
+ );
10
+ });
11
+
12
+ test("parse value", () => {
13
+ FieldElement.parse("0x1");
14
+ });
15
+
16
+ test("parse invalid value", () => {
17
+ expect(() => FieldElement.parse("0x")).toThrowError();
18
+ });
19
+ });
@@ -0,0 +1,16 @@
1
+ import { z } from "zod";
2
+
3
+ export type FieldElement = `0x${string}`;
4
+
5
+ export const FieldElement = z.string().transform((value, ctx) => {
6
+ const regex = /^0x[0-9a-fA-F]{1,64}$/;
7
+
8
+ if (!regex.test(value)) {
9
+ ctx.addIssue({
10
+ code: z.ZodIssueCode.custom,
11
+ message: `FieldElement must be a hex string with a 0x prefix, got ${value}`,
12
+ });
13
+ }
14
+
15
+ return value as FieldElement;
16
+ });
@@ -0,0 +1,197 @@
1
+ import { assertType, describe, test } from "vitest";
2
+
3
+ import { FieldElement } from "./felt";
4
+ import {
5
+ EventFilter,
6
+ Filter,
7
+ L2ToL1MessageFilter,
8
+ StateUpdateFilter,
9
+ TransactionFilter,
10
+ } from "./filter";
11
+
12
+ const address =
13
+ "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
14
+ const entryPointSelector =
15
+ "0x03943907ef0ef6f9d2e2408b05e520a66daaf74293dbf665e5a20b117676170e";
16
+ const calldata = [FieldElement.parse("0x01"), FieldElement.parse("0x02")];
17
+
18
+ describe("Filter", () => {
19
+ test("all optional", () => {
20
+ assertType<Filter>({});
21
+ });
22
+ });
23
+
24
+ describe("TransactionFilter", () => {
25
+ test("all optional", () => {
26
+ assertType<TransactionFilter>({});
27
+ });
28
+
29
+ test("includeReverted", () => {
30
+ assertType<TransactionFilter>({ includeReverted: true });
31
+ });
32
+
33
+ test("only one inner filter", () => {
34
+ // @ts-expect-error - multiple inner filters
35
+ assertType<TransactionFilter>({
36
+ invokeV0: {},
37
+ deployAccount: {},
38
+ });
39
+ });
40
+
41
+ test("filter invoke transaction v0", () => {
42
+ assertType<TransactionFilter>({
43
+ invokeV0: {
44
+ contractAddress: address,
45
+ entryPointSelector,
46
+ calldata,
47
+ },
48
+ });
49
+ });
50
+
51
+ test("filter invoke transaction v1", () => {
52
+ assertType<TransactionFilter>({
53
+ invokeV1: {
54
+ senderAddress: address,
55
+ calldata,
56
+ },
57
+ });
58
+ });
59
+
60
+ test("filter deploy transaction", () => {
61
+ assertType<TransactionFilter>({
62
+ deploy: {
63
+ contractAddressSalt: address,
64
+ classHash: address,
65
+ constructorCalldata: calldata,
66
+ },
67
+ });
68
+ });
69
+
70
+ test("filter declare transaction", () => {
71
+ assertType<TransactionFilter>({
72
+ declare: {
73
+ senderAddress: address,
74
+ classHash: address,
75
+ },
76
+ });
77
+ });
78
+
79
+ test("filter l1 handler transaction", () => {
80
+ assertType<TransactionFilter>({
81
+ l1Handler: {
82
+ contractAddress: address,
83
+ entryPointSelector,
84
+ calldata,
85
+ },
86
+ });
87
+ });
88
+
89
+ test("filter deploy account transaction", () => {
90
+ assertType<TransactionFilter>({
91
+ deployAccount: {
92
+ contractAddressSalt: address,
93
+ classHash: address,
94
+ constructorCalldata: calldata,
95
+ },
96
+ });
97
+ });
98
+ });
99
+
100
+ describe("EventFilter", () => {
101
+ test("all optional", () => {
102
+ assertType<EventFilter>({});
103
+ });
104
+
105
+ test("with properties", () => {
106
+ assertType<EventFilter>({
107
+ fromAddress: address,
108
+ keys: [entryPointSelector],
109
+ includeReverted: true,
110
+ data: calldata,
111
+ });
112
+ });
113
+ });
114
+
115
+ describe("L2ToL1MessageFilter", () => {
116
+ test("all optional", () => {
117
+ assertType<L2ToL1MessageFilter>({});
118
+ });
119
+
120
+ test("with properties", () => {
121
+ assertType<L2ToL1MessageFilter>({
122
+ toAddress: address,
123
+ includeReverted: true,
124
+ payload: calldata,
125
+ });
126
+ });
127
+ });
128
+
129
+ describe("StateUpdateFilter", () => {
130
+ test("all optional", () => {
131
+ assertType<StateUpdateFilter>({});
132
+ });
133
+
134
+ test("with storage diffs", () => {
135
+ assertType<StateUpdateFilter>({
136
+ storageDiffs: [
137
+ {
138
+ contractAddress: address,
139
+ },
140
+ ],
141
+ });
142
+ });
143
+
144
+ test("with declared contracts", () => {
145
+ assertType<StateUpdateFilter>({
146
+ declaredContracts: [
147
+ {
148
+ classHash: address,
149
+ },
150
+ ],
151
+ });
152
+ });
153
+
154
+ test("with deployed contracts", () => {
155
+ assertType<StateUpdateFilter>({
156
+ deployedContracts: [
157
+ {
158
+ classHash: address,
159
+ contractAddress: address,
160
+ },
161
+ ],
162
+ });
163
+ });
164
+
165
+ test("with nonces", () => {
166
+ assertType<StateUpdateFilter>({
167
+ nonces: [
168
+ {
169
+ contractAddress: address,
170
+ nonce: "0x1",
171
+ },
172
+ ],
173
+ });
174
+ });
175
+
176
+ test("with declared classes", () => {
177
+ assertType<StateUpdateFilter>({
178
+ declaredClasses: [
179
+ {
180
+ compiledClassHash: address,
181
+ classHash: address,
182
+ },
183
+ ],
184
+ });
185
+ });
186
+
187
+ test("with replaced classes", () => {
188
+ assertType<StateUpdateFilter>({
189
+ replacedClasses: [
190
+ {
191
+ contractAddress: address,
192
+ classHash: address,
193
+ },
194
+ ],
195
+ });
196
+ });
197
+ });