Skip to content

Commit 3282cc4

Browse files
committed
indexer: customize contract's event filter
1 parent ce095f9 commit 3282cc4

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

.changeset/pretty-ravens-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apibara/indexer": patch
3+
---
4+
5+
Customize Contract event filter

packages/indexer/src/starknet/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./filter";
22
export * from "./block";
3+
export { Contract } from "./parser";
34

45
import { Filter } from "./filter";
56

packages/indexer/src/starknet/parser.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ describe("Contract class tests", () => {
2020

2121
expect(filter).toBeDefined();
2222
expect(filter.fromAddress).toBe(mockContractAddress);
23-
// Further assertions can be made based on the expected structure of EventFilter
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();
2432
});
2533

2634
it("should throw an error for an invalid event name", () => {

packages/indexer/src/starknet/parser.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { EventFilter } from "./filter";
33

44
import { type Abi, EventAbi, hash } from "starknet";
55

6-
// The Contract class implements the EventFilterParser interface.
6+
/** Build a stream filter from a contract ABI. */
77
export class Contract {
88
// Read-only properties for the contract's address and its ABI.
99
readonly contractAddress: FieldElement;
1010
readonly contractAbi: Abi;
1111

12-
// Constructor to initialize a Contract instance with a contract address and ABI.
1312
constructor(contractAddress: FieldElement, contractAbi: Abi) {
1413
this.contractAddress = contractAddress;
1514
this.contractAbi = contractAbi;
@@ -24,8 +23,14 @@ export class Contract {
2423
return event;
2524
}
2625

27-
// Method to filter events based on their name from the contract's ABI.
28-
eventFilter(name: string): EventFilter {
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 {
2934
const event = this._findEvent(name);
3035

3136
// Throw an error if the event is not found in the ABI
@@ -37,8 +42,9 @@ export class Contract {
3742
return {
3843
fromAddress: this.contractAddress,
3944
keys: [hash.getSelectorFromName(event.name) as `0x${string}`],
40-
includeTransaction: false,
41-
includeReceipt: false,
45+
includeTransaction: opts.includeTransaction ?? false,
46+
includeReceipt: opts.includeReceipt ?? false,
47+
includeReverted: opts.includeReverted ?? false,
4248
};
4349
}
4450
}

0 commit comments

Comments
 (0)