@apibara/indexer 0.2.1 → 0.2.3

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.
package/dist/config.d.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  /** Sink-related options. */
2
- export declare type SinkOptions = {
2
+ export type SinkOptions = {
3
3
  /** Sink type. */
4
4
  sinkType: string;
5
5
  /** Sink options. */
6
6
  sinkOptions: object;
7
7
  };
8
8
  /** Network-specific options. */
9
- export declare type NetworkOptions = {
9
+ export type NetworkOptions = {
10
10
  /** Network name. */
11
11
  network: string;
12
12
  /** Data filter. */
13
13
  filter: object;
14
14
  };
15
15
  /** Data finality. */
16
- export declare type Finality = "DATA_STATUS_FINALIZED" | "DATA_STATUS_ACCEPTED" | "DATA_STATUS_PENDING";
16
+ export type Finality = "DATA_STATUS_FINALIZED" | "DATA_STATUS_ACCEPTED" | "DATA_STATUS_PENDING";
17
17
  /** Stream-related options. */
18
- export declare type StreamOptions = {
18
+ export type StreamOptions = {
19
19
  /** The Apibara DNA stream url, e.g. `mainnet.starknet.a5a.ch`. */
20
20
  streamUrl?: string;
21
21
  /** Maximum message size, e.g. `50Mb` */
@@ -25,7 +25,7 @@ export declare type StreamOptions = {
25
25
  /** The Apibara DNA stream auth token. */
26
26
  authToken?: string;
27
27
  };
28
- export declare type Config<TNetworkOptions extends NetworkOptions = NetworkOptions, TSink extends SinkOptions = SinkOptions> = TSink & TNetworkOptions & StreamOptions & {
28
+ export type Config<TNetworkOptions extends NetworkOptions = NetworkOptions, TSink extends SinkOptions = SinkOptions> = TSink & TNetworkOptions & StreamOptions & {
29
29
  /** How many historical blocks to process in a single batch. */
30
30
  batchSize?: number;
31
31
  /** Finality of the data to process. */
@@ -1,5 +1,5 @@
1
1
  /** Console sink type. */
2
- export declare type Console = {
2
+ export type Console = {
3
3
  sinkType: "console";
4
4
  /** Accept any options.
5
5
  *
@@ -1,5 +1,5 @@
1
1
  /** MongoDB sink options */
2
- export declare type Mongo = {
2
+ export type Mongo = {
3
3
  sinkType: "mongo";
4
4
  sinkOptions: {
5
5
  /** MongoDB connection string. */
@@ -1,4 +1,4 @@
1
- export declare type Parquet = {
1
+ export type Parquet = {
2
2
  sinkType: "parquet";
3
3
  sinkOptions: {
4
4
  /** Target output directory. */
@@ -1,5 +1,5 @@
1
1
  /** PostgreSQL sink options. */
2
- export declare type Postgres = {
2
+ export type Postgres = {
3
3
  sinkType: "postgres";
4
4
  sinkOptions: {
5
5
  /** Postgres connection string. */
@@ -1,5 +1,5 @@
1
1
  /** Webhook sink options */
2
- export declare type Webhook = {
2
+ export type Webhook = {
3
3
  sinkType: "webhook";
4
4
  sinkOptions: {
5
5
  /** Target URL. */
@@ -1,5 +1,5 @@
1
1
  import { FieldElement } from "./felt";
2
- export declare type Block = {
2
+ export type Block = {
3
3
  /** Block header. */
4
4
  header?: BlockHeader;
5
5
  /** Transactions. */
@@ -11,7 +11,7 @@ export declare type Block = {
11
11
  /** State update. */
12
12
  stateUpdate?: StateUpdate;
13
13
  };
14
- export declare type BlockHeader = {
14
+ export type BlockHeader = {
15
15
  /** Block hash. */
16
16
  blockHash: FieldElement;
17
17
  /** Parent block hash. */
@@ -25,13 +25,13 @@ export declare type BlockHeader = {
25
25
  /** Block production timestamp. */
26
26
  timestamp: string;
27
27
  };
28
- export declare type TransactionWithReceipt = {
28
+ export type TransactionWithReceipt = {
29
29
  /** Transaction. */
30
30
  transaction: Transaction;
31
31
  /** Transaction receipt. */
32
32
  receipt: TransactionReceipt;
33
33
  };
34
- export declare type EventWithTransaction = {
34
+ export type EventWithTransaction = {
35
35
  /** Transaction. */
36
36
  transaction: Transaction;
37
37
  /** Transaction receipt. */
@@ -39,17 +39,17 @@ export declare type EventWithTransaction = {
39
39
  /** Event. */
40
40
  event: Event;
41
41
  };
42
- export declare type L2ToL1MessageWithTransaction = {
42
+ export type L2ToL1MessageWithTransaction = {
43
43
  /** Transaction. */
44
44
  transaction: Transaction;
45
45
  /** Message from L2 to L1. */
46
46
  message: L2ToL1Message;
47
47
  };
48
- export declare type Transaction = TransactionCommon & (InvokeTransactionV0 | InvokeTransactionV1 | DeployTransaction | DeclareTransaction | DeployAccountTransaction | L1HandlerTransaction);
49
- export declare type TransactionCommon = {
48
+ export type Transaction = TransactionCommon & (InvokeTransactionV0 | InvokeTransactionV1 | DeployTransaction | DeclareTransaction | DeployAccountTransaction | L1HandlerTransaction);
49
+ export type TransactionCommon = {
50
50
  meta: TransactionMeta;
51
51
  };
52
- export declare type TransactionMeta = {
52
+ export type TransactionMeta = {
53
53
  /** Transaction hash. */
54
54
  hash: FieldElement;
55
55
  /** Maximum fee. */
@@ -61,7 +61,7 @@ export declare type TransactionMeta = {
61
61
  /** Transaction version. */
62
62
  version: string;
63
63
  };
64
- export declare type InvokeTransactionV0 = {
64
+ export type InvokeTransactionV0 = {
65
65
  invokeV0?: {
66
66
  /** Target contract address. */
67
67
  contractAddress: FieldElement;
@@ -76,7 +76,7 @@ export declare type InvokeTransactionV0 = {
76
76
  l1Handler?: never;
77
77
  deployAccount?: never;
78
78
  };
79
- export declare type InvokeTransactionV1 = {
79
+ export type InvokeTransactionV1 = {
80
80
  invokeV1?: {
81
81
  /** Address of the account sending the transaction. */
82
82
  senderAddress: FieldElement;
@@ -89,7 +89,7 @@ export declare type InvokeTransactionV1 = {
89
89
  l1Handler?: never;
90
90
  deployAccount?: never;
91
91
  };
92
- export declare type DeployTransaction = {
92
+ export type DeployTransaction = {
93
93
  deploy?: {
94
94
  /** Constructor calldata. */
95
95
  constructorCalldata: FieldElement[];
@@ -104,7 +104,7 @@ export declare type DeployTransaction = {
104
104
  l1Handler?: never;
105
105
  deployAccount?: never;
106
106
  };
107
- export declare type DeclareTransaction = {
107
+ export type DeclareTransaction = {
108
108
  declare?: {
109
109
  /** Class hash. */
110
110
  classHash: FieldElement;
@@ -119,7 +119,7 @@ export declare type DeclareTransaction = {
119
119
  l1Handler?: never;
120
120
  deployAccount?: never;
121
121
  };
122
- export declare type DeployAccountTransaction = {
122
+ export type DeployAccountTransaction = {
123
123
  deployAccount?: {
124
124
  /** Constructor calldata. */
125
125
  constructorCalldata: FieldElement[];
@@ -134,7 +134,7 @@ export declare type DeployAccountTransaction = {
134
134
  declare?: never;
135
135
  l1Handler?: never;
136
136
  };
137
- export declare type L1HandlerTransaction = {
137
+ export type L1HandlerTransaction = {
138
138
  l1Handler?: {
139
139
  /** Target contract address. */
140
140
  contractAddress: FieldElement;
@@ -149,7 +149,7 @@ export declare type L1HandlerTransaction = {
149
149
  declare?: never;
150
150
  deployAccount?: never;
151
151
  };
152
- export declare type TransactionReceipt = {
152
+ export type TransactionReceipt = {
153
153
  /** Transaction status. */
154
154
  executionStatus: ExecutionStatus;
155
155
  /** Transaction hash. */
@@ -167,8 +167,8 @@ export declare type TransactionReceipt = {
167
167
  /** Revert reason. */
168
168
  revertReason?: string;
169
169
  };
170
- export declare type ExecutionStatus = "EXECUTION_STATUS_UNSPECIFIED" | "EXECUTION_STATUS_SUCCEEDED" | "EXECUTION_STATUS_REVERTED";
171
- export declare type Event = {
170
+ export type ExecutionStatus = "EXECUTION_STATUS_UNSPECIFIED" | "EXECUTION_STATUS_SUCCEEDED" | "EXECUTION_STATUS_REVERTED";
171
+ export type Event = {
172
172
  /** Event index. */
173
173
  index: number;
174
174
  /** Contract address. */
@@ -178,7 +178,7 @@ export declare type Event = {
178
178
  /** Event data. */
179
179
  data: FieldElement[];
180
180
  };
181
- export declare type L2ToL1Message = {
181
+ export type L2ToL1Message = {
182
182
  /** Message index. */
183
183
  index: number;
184
184
  /** L2 sender address. */
@@ -188,7 +188,7 @@ export declare type L2ToL1Message = {
188
188
  /** Calldata. */
189
189
  payload: FieldElement[];
190
190
  };
191
- export declare type StateUpdate = {
191
+ export type StateUpdate = {
192
192
  /** New state root. */
193
193
  newRoot: FieldElement;
194
194
  /** Old state root. */
@@ -196,7 +196,7 @@ export declare type StateUpdate = {
196
196
  /** State diff. */
197
197
  stateDiff: StateDiff;
198
198
  };
199
- export declare type StateDiff = {
199
+ export type StateDiff = {
200
200
  /** Changes in storage. */
201
201
  storageDiffs: StorageDiff[];
202
202
  /** Declared contracts. */
@@ -210,41 +210,41 @@ export declare type StateDiff = {
210
210
  /** Classes replaced. */
211
211
  replacedClasses: ReplacedClass[];
212
212
  };
213
- export declare type StorageDiff = {
213
+ export type StorageDiff = {
214
214
  /** Contract address. */
215
215
  contractAddress: FieldElement;
216
216
  /** Changes in storage. */
217
217
  storageEntries: StorageEntry[];
218
218
  };
219
- export declare type StorageEntry = {
219
+ export type StorageEntry = {
220
220
  /** Storage key. */
221
221
  key: FieldElement;
222
222
  /** New storage value. */
223
223
  value: FieldElement;
224
224
  };
225
- export declare type DeclaredContract = {
225
+ export type DeclaredContract = {
226
226
  /** Class hash. */
227
227
  classHash: FieldElement;
228
228
  };
229
- export declare type DeclaredClass = {
229
+ export type DeclaredClass = {
230
230
  /** Class hash. */
231
231
  classHash: FieldElement;
232
232
  /** Compiled class hash. */
233
233
  compiledClassHash: FieldElement;
234
234
  };
235
- export declare type ReplacedClass = {
235
+ export type ReplacedClass = {
236
236
  /** Contract address. */
237
237
  contractAddress: FieldElement;
238
238
  /** Class hash. */
239
239
  classHash: FieldElement;
240
240
  };
241
- export declare type DeployedContract = {
241
+ export type DeployedContract = {
242
242
  /** Contract address. */
243
243
  contractAddress: FieldElement;
244
244
  /** Class hash. */
245
245
  classHash: FieldElement;
246
246
  };
247
- export declare type NonceUpdate = {
247
+ export type NonceUpdate = {
248
248
  /** Contract address. */
249
249
  contractAddress: FieldElement;
250
250
  /** New nonce. */
@@ -1,3 +1,3 @@
1
1
  import { z } from "zod";
2
- export declare type FieldElement = `0x${string}`;
2
+ export type FieldElement = `0x${string}`;
3
3
  export declare const FieldElement: z.ZodEffects<z.ZodString, `0x${string}`, string>;
@@ -1,5 +1,5 @@
1
1
  import { FieldElement } from "./felt";
2
- export declare type Filter = {
2
+ export type Filter = {
3
3
  /** Header information. */
4
4
  header?: HeaderFilter;
5
5
  /** Include transactions. */
@@ -11,14 +11,14 @@ export declare type Filter = {
11
11
  /** Include state updates. */
12
12
  stateUpdate?: StateUpdateFilter;
13
13
  };
14
- export declare type HeaderFilter = {
14
+ export type HeaderFilter = {
15
15
  weak?: boolean;
16
16
  };
17
- export declare type TransactionFilterCommon = {
17
+ export type TransactionFilterCommon = {
18
18
  /*** Include reverted transactions. */
19
19
  includeReverted?: boolean;
20
20
  };
21
- export declare type InvokeTransactionV0Filter = {
21
+ export type InvokeTransactionV0Filter = {
22
22
  invokeV0?: {
23
23
  /** Filter by contract address. */
24
24
  contractAddress?: FieldElement;
@@ -33,7 +33,7 @@ export declare type InvokeTransactionV0Filter = {
33
33
  l1Handler?: never;
34
34
  deployAccount?: never;
35
35
  };
36
- export declare type InvokeTransactionV1Filter = {
36
+ export type InvokeTransactionV1Filter = {
37
37
  invokeV1?: {
38
38
  /** Filter by sender address. */
39
39
  senderAddress?: FieldElement;
@@ -46,7 +46,7 @@ export declare type InvokeTransactionV1Filter = {
46
46
  l1Handler?: never;
47
47
  deployAccount?: never;
48
48
  };
49
- export declare type DeployTransactionFilter = {
49
+ export type DeployTransactionFilter = {
50
50
  deploy?: {
51
51
  /** Filter by contract address salt. */
52
52
  contractAddressSalt?: FieldElement;
@@ -61,7 +61,7 @@ export declare type DeployTransactionFilter = {
61
61
  l1Handler?: never;
62
62
  deployAccount?: never;
63
63
  };
64
- export declare type DeclareTransactionFilter = {
64
+ export type DeclareTransactionFilter = {
65
65
  declare?: {
66
66
  /** Filter by class hash. */
67
67
  classHash?: FieldElement;
@@ -74,7 +74,7 @@ export declare type DeclareTransactionFilter = {
74
74
  l1Handler?: never;
75
75
  deployAccount?: never;
76
76
  };
77
- export declare type L1HandlerTransactionFilter = {
77
+ export type L1HandlerTransactionFilter = {
78
78
  l1Handler?: {
79
79
  /** Filter by contract address. */
80
80
  contractAddress?: FieldElement;
@@ -89,7 +89,7 @@ export declare type L1HandlerTransactionFilter = {
89
89
  declare?: never;
90
90
  deployAccount?: never;
91
91
  };
92
- export declare type DeployAccountTransactionFilter = {
92
+ export type DeployAccountTransactionFilter = {
93
93
  deployAccount?: {
94
94
  /** Filter by contract address salt. */
95
95
  contractAddressSalt?: FieldElement;
@@ -104,8 +104,8 @@ export declare type DeployAccountTransactionFilter = {
104
104
  declare?: never;
105
105
  l1Handler?: never;
106
106
  };
107
- export declare type TransactionFilter = TransactionFilterCommon & (InvokeTransactionV0Filter | InvokeTransactionV1Filter | DeployTransactionFilter | DeclareTransactionFilter | L1HandlerTransactionFilter | DeployAccountTransactionFilter);
108
- export declare type EventFilter = {
107
+ export type TransactionFilter = TransactionFilterCommon & (InvokeTransactionV0Filter | InvokeTransactionV1Filter | DeployTransactionFilter | DeclareTransactionFilter | L1HandlerTransactionFilter | DeployAccountTransactionFilter);
108
+ export type EventFilter = {
109
109
  /** Filter by contract address. */
110
110
  fromAddress?: FieldElement;
111
111
  /** Filter by event keys (selector). */
@@ -114,8 +114,12 @@ export declare type EventFilter = {
114
114
  data?: FieldElement[];
115
115
  /** Include events from reverted transactions. */
116
116
  includeReverted?: boolean;
117
+ /** Include the transaction that emitted the event. Defaults to true. */
118
+ includeTransaction?: boolean;
119
+ /** Include the receipt of the transaction that emitted the event. Defaults to true. */
120
+ includeReceipt?: boolean;
117
121
  };
118
- export declare type L2ToL1MessageFilter = {
122
+ export type L2ToL1MessageFilter = {
119
123
  /** Filter by destination address. */
120
124
  toAddress?: FieldElement;
121
125
  /** Filter by payload. */
@@ -123,7 +127,7 @@ export declare type L2ToL1MessageFilter = {
123
127
  /** Include messages from reverted transactions. */
124
128
  includeReverted?: boolean;
125
129
  };
126
- export declare type StateUpdateFilter = {
130
+ export type StateUpdateFilter = {
127
131
  /** Filter storage diffs. */
128
132
  storageDiffs?: StorageDiffFilter[];
129
133
  /** Filter declared contracts. */
@@ -137,33 +141,33 @@ export declare type StateUpdateFilter = {
137
141
  /** Filter replaced classes. */
138
142
  replacedClasses?: ReplacedClassFilter[];
139
143
  };
140
- export declare type StorageDiffFilter = {
144
+ export type StorageDiffFilter = {
141
145
  /** Filter by contract address. */
142
146
  contractAddress?: FieldElement;
143
147
  };
144
- export declare type DeclaredContractFilter = {
148
+ export type DeclaredContractFilter = {
145
149
  /** Filter by class hash. */
146
150
  classHash?: FieldElement;
147
151
  };
148
- export declare type DeployedContractFilter = {
152
+ export type DeployedContractFilter = {
149
153
  /** Filter by contract address. */
150
154
  contractAddress?: FieldElement;
151
155
  /** Filter by class hash. */
152
156
  classHash?: FieldElement;
153
157
  };
154
- export declare type NonceUpdateFilter = {
158
+ export type NonceUpdateFilter = {
155
159
  /** Filter by contract address. */
156
160
  contractAddress?: FieldElement;
157
161
  /** Filter by nonce value. */
158
162
  nonce?: FieldElement;
159
163
  };
160
- export declare type DeclaredClassFilter = {
164
+ export type DeclaredClassFilter = {
161
165
  /** Filter by class hash. */
162
166
  classHash?: FieldElement;
163
167
  /** Filter by compiled class hash. */
164
168
  compiledClassHash?: FieldElement;
165
169
  };
166
- export declare type ReplacedClassFilter = {
170
+ export type ReplacedClassFilter = {
167
171
  /** Filter by contract address. */
168
172
  contractAddress?: FieldElement;
169
173
  /** Filter by class hash. */
@@ -1,8 +1,9 @@
1
1
  export * from "./filter";
2
2
  export * from "./block";
3
+ export { Contract } from "./parser";
3
4
  import { Filter } from "./filter";
4
5
  /** Starknet network type and data filter. */
5
- export declare type Starknet = {
6
+ export type Starknet = {
6
7
  network: "starknet";
7
8
  filter: Filter;
8
9
  };
@@ -1,3 +1,4 @@
1
1
  export * from "./filter";
2
2
  export * from "./block";
3
+ export { Contract } from "./parser";
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/starknet/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/starknet/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC"}
@@ -0,0 +1,12 @@
1
+ import { FieldElement } from "./felt";
2
+ import { EventFilter } from "./filter";
3
+ import { type Abi } from "starknet";
4
+ /** Build a stream filter from a contract ABI. */
5
+ export declare class Contract {
6
+ readonly contractAddress: FieldElement;
7
+ readonly contractAbi: Abi;
8
+ constructor(contractAddress: FieldElement, contractAbi: Abi);
9
+ private _findEvent;
10
+ /** Filter events based on their name from the contract's ABI. */
11
+ eventFilter(name: string, opts?: Pick<EventFilter, "includeReceipt" | "includeTransaction" | "includeReverted">): EventFilter;
12
+ }
@@ -0,0 +1,33 @@
1
+ import { hash } from "starknet";
2
+ /** Build a stream filter from a contract ABI. */
3
+ export class Contract {
4
+ // Read-only properties for the contract's address and its ABI.
5
+ contractAddress;
6
+ contractAbi;
7
+ constructor(contractAddress, contractAbi) {
8
+ this.contractAddress = contractAddress;
9
+ this.contractAbi = contractAbi;
10
+ }
11
+ _findEvent(name) {
12
+ // Find the event in the ABI matching the provided name.
13
+ const event = this.contractAbi.find((item) => item.type === "event" && item.name === name);
14
+ return event;
15
+ }
16
+ /** Filter events based on their name from the contract's ABI. */
17
+ eventFilter(name, opts = {}) {
18
+ const event = this._findEvent(name);
19
+ // Throw an error if the event is not found in the ABI
20
+ if (!event) {
21
+ throw new Error(`Event ${name} not found in contract ABI`);
22
+ }
23
+ // Return the found event.
24
+ return {
25
+ fromAddress: this.contractAddress,
26
+ keys: [hash.getSelectorFromName(event.name)],
27
+ includeTransaction: opts.includeTransaction ?? false,
28
+ includeReceipt: opts.includeReceipt ?? false,
29
+ includeReverted: opts.includeReverted ?? false,
30
+ };
31
+ }
32
+ }
33
+ //# sourceMappingURL=parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/starknet/parser.ts"],"names":[],"mappings":"AAGA,OAAO,EAAsB,IAAI,EAAE,MAAM,UAAU,CAAC;AAEpD,iDAAiD;AACjD,MAAM,OAAO,QAAQ;IACnB,+DAA+D;IACtD,eAAe,CAAe;IAC9B,WAAW,CAAM;IAE1B,YAAY,eAA6B,EAAE,WAAgB;QACzD,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAEO,UAAU,CAAC,IAAY;QAC7B,wDAAwD;QACxD,MAAM,KAAK,GAAyB,IAAI,CAAC,WAAW,CAAC,IAAI,CACvD,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,CACtD,CAAC;QAEF,OAAO,KAAK,CAAC;IACf,CAAC;IAED,iEAAiE;IACjE,WAAW,CACT,IAAY,EACZ,OAGI,EAAE;QAEN,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAEpC,sDAAsD;QACtD,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,SAAS,IAAI,4BAA4B,CAAC,CAAC;SAC5D;QAED,0BAA0B;QAC1B,OAAO;YACL,WAAW,EAAE,IAAI,CAAC,eAAe;YACjC,IAAI,EAAE,CAAC,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,IAAI,CAAkB,CAAC;YAC7D,kBAAkB,EAAE,IAAI,CAAC,kBAAkB,IAAI,KAAK;YACpD,cAAc,EAAE,IAAI,CAAC,cAAc,IAAI,KAAK;YAC5C,eAAe,EAAE,IAAI,CAAC,eAAe,IAAI,KAAK;SAC/C,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import { Contract } from "./parser";
2
+ import { describe, expect, it } from "vitest";
3
+ describe("Contract class tests", () => {
4
+ const mockContractAddress = "0x0";
5
+ const mockEventAbi = {
6
+ type: "event",
7
+ name: "MockEvent",
8
+ kind: "struct",
9
+ members: [{ name: "param1", type: "felt", kind: "key" }],
10
+ };
11
+ const mockContractAbi = [mockEventAbi];
12
+ it("should create an EventFilter for a valid event name", () => {
13
+ const contract = new Contract(mockContractAddress, mockContractAbi);
14
+ const filter = contract.eventFilter("MockEvent");
15
+ expect(filter).toBeDefined();
16
+ expect(filter.fromAddress).toBe(mockContractAddress);
17
+ expect(filter.includeReceipt).toBeFalsy();
18
+ });
19
+ it("overrides some arguments", () => {
20
+ const contract = new Contract(mockContractAddress, mockContractAbi);
21
+ const filter = contract.eventFilter("MockEvent", { includeReceipt: true });
22
+ expect(filter).toBeDefined();
23
+ expect(filter.includeReceipt).toBeTruthy();
24
+ });
25
+ it("should throw an error for an invalid event name", () => {
26
+ const contract = new Contract(mockContractAddress, mockContractAbi);
27
+ expect(() => {
28
+ contract.eventFilter("InvalidEvent");
29
+ }).toThrow("Event InvalidEvent not found in contract ABI");
30
+ });
31
+ });
32
+ //# sourceMappingURL=parser.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.test.js","sourceRoot":"","sources":["../../src/starknet/parser.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAI9C,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IACpC,MAAM,mBAAmB,GAAiB,KAAK,CAAC;IAChD,MAAM,YAAY,GAAa;QAC7B,IAAI,EAAE,OAAO;QACb,IAAI,EAAE,WAAW;QACjB,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;KACzD,CAAC;IAEF,MAAM,eAAe,GAAG,CAAC,YAAY,CAAC,CAAC;IAEvC,EAAE,CAAC,qDAAqD,EAAE,GAAG,EAAE;QAC7D,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;QACpE,MAAM,MAAM,GAAG,QAAQ,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;QAEjD,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QACrD,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,SAAS,EAAE,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0BAA0B,EAAE,GAAG,EAAE;QAClC,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;QACpE,MAAM,MAAM,GAAG,QAAQ,CAAC,WAAW,CAAC,WAAW,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC,CAAC;QAE3E,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,UAAU,EAAE,CAAC;IAC7C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iDAAiD,EAAE,GAAG,EAAE;QACzD,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;QAEpE,MAAM,CAAC,GAAG,EAAE;YACV,QAAQ,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;QACvC,CAAC,CAAC,CAAC,OAAO,CAAC,8CAA8C,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apibara/indexer",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -48,6 +48,7 @@
48
48
  "vitest": "^0.34.3"
49
49
  },
50
50
  "dependencies": {
51
+ "starknet": "^5.24.3",
51
52
  "zod": "^3.22.2"
52
53
  },
53
54
  "scripts": {
@@ -136,6 +136,10 @@ export type EventFilter = {
136
136
  data?: FieldElement[];
137
137
  /** Include events from reverted transactions. */
138
138
  includeReverted?: boolean;
139
+ /** Include the transaction that emitted the event. Defaults to true. */
140
+ includeTransaction?: boolean;
141
+ /** Include the receipt of the transaction that emitted the event. Defaults to true. */
142
+ includeReceipt?: boolean;
139
143
  };
140
144
 
141
145
  export type L2ToL1MessageFilter = {
@@ -1,5 +1,6 @@
1
1
  export * from "./filter";
2
2
  export * from "./block";
3
+ export { Contract } from "./parser";
3
4
 
4
5
  import { Filter } from "./filter";
5
6
 
@@ -0,0 +1,41 @@
1
+ import { Contract } from "./parser";
2
+ import { describe, expect, it } from "vitest";
3
+ import { FieldElement } from "./felt";
4
+ import { EventAbi } from "starknet";
5
+
6
+ describe("Contract class tests", () => {
7
+ const mockContractAddress: FieldElement = "0x0";
8
+ const mockEventAbi: EventAbi = {
9
+ type: "event",
10
+ name: "MockEvent",
11
+ kind: "struct",
12
+ members: [{ name: "param1", type: "felt", kind: "key" }],
13
+ };
14
+
15
+ const mockContractAbi = [mockEventAbi];
16
+
17
+ it("should create an EventFilter for a valid event name", () => {
18
+ const contract = new Contract(mockContractAddress, mockContractAbi);
19
+ const filter = contract.eventFilter("MockEvent");
20
+
21
+ expect(filter).toBeDefined();
22
+ expect(filter.fromAddress).toBe(mockContractAddress);
23
+ expect(filter.includeReceipt).toBeFalsy();
24
+ });
25
+
26
+ it("overrides some arguments", () => {
27
+ const contract = new Contract(mockContractAddress, mockContractAbi);
28
+ const filter = contract.eventFilter("MockEvent", { includeReceipt: true });
29
+
30
+ expect(filter).toBeDefined();
31
+ expect(filter.includeReceipt).toBeTruthy();
32
+ });
33
+
34
+ it("should throw an error for an invalid event name", () => {
35
+ const contract = new Contract(mockContractAddress, mockContractAbi);
36
+
37
+ expect(() => {
38
+ contract.eventFilter("InvalidEvent");
39
+ }).toThrow("Event InvalidEvent not found in contract ABI");
40
+ });
41
+ });
@@ -0,0 +1,50 @@
1
+ import { FieldElement } from "./felt";
2
+ import { EventFilter } from "./filter";
3
+
4
+ import { type Abi, EventAbi, hash } from "starknet";
5
+
6
+ /** Build a stream filter from a contract ABI. */
7
+ export class Contract {
8
+ // Read-only properties for the contract's address and its ABI.
9
+ readonly contractAddress: FieldElement;
10
+ readonly contractAbi: Abi;
11
+
12
+ constructor(contractAddress: FieldElement, contractAbi: Abi) {
13
+ this.contractAddress = contractAddress;
14
+ this.contractAbi = contractAbi;
15
+ }
16
+
17
+ private _findEvent(name: string): EventAbi | undefined {
18
+ // Find the event in the ABI matching the provided name.
19
+ const event: EventAbi | undefined = this.contractAbi.find(
20
+ (item) => item.type === "event" && item.name === name,
21
+ );
22
+
23
+ return event;
24
+ }
25
+
26
+ /** Filter events based on their name from the contract's ABI. */
27
+ eventFilter(
28
+ name: string,
29
+ opts: Pick<
30
+ EventFilter,
31
+ "includeReceipt" | "includeTransaction" | "includeReverted"
32
+ > = {},
33
+ ): EventFilter {
34
+ const event = this._findEvent(name);
35
+
36
+ // Throw an error if the event is not found in the ABI
37
+ if (!event) {
38
+ throw new Error(`Event ${name} not found in contract ABI`);
39
+ }
40
+
41
+ // Return the found event.
42
+ return {
43
+ fromAddress: this.contractAddress,
44
+ keys: [hash.getSelectorFromName(event.name) as `0x${string}`],
45
+ includeTransaction: opts.includeTransaction ?? false,
46
+ includeReceipt: opts.includeReceipt ?? false,
47
+ includeReverted: opts.includeReverted ?? false,
48
+ };
49
+ }
50
+ }