Skip to content

Commit

Permalink
indexer: customize contract's event filter
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jan 17, 2024
1 parent ce095f9 commit 3282cc4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-ravens-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apibara/indexer": patch
---

Customize Contract event filter
1 change: 1 addition & 0 deletions packages/indexer/src/starknet/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./filter";
export * from "./block";
export { Contract } from "./parser";

import { Filter } from "./filter";

Expand Down
10 changes: 9 additions & 1 deletion packages/indexer/src/starknet/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ describe("Contract class tests", () => {

expect(filter).toBeDefined();
expect(filter.fromAddress).toBe(mockContractAddress);
// Further assertions can be made based on the expected structure of EventFilter
expect(filter.includeReceipt).toBeFalsy();
});

it("overrides some arguments", () => {
const contract = new Contract(mockContractAddress, mockContractAbi);
const filter = contract.eventFilter("MockEvent", { includeReceipt: true });

expect(filter).toBeDefined();
expect(filter.includeReceipt).toBeTruthy();
});

it("should throw an error for an invalid event name", () => {
Expand Down
18 changes: 12 additions & 6 deletions packages/indexer/src/starknet/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { EventFilter } from "./filter";

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

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

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

// Method to filter events based on their name from the contract's ABI.
eventFilter(name: string): EventFilter {
/** Filter events based on their name from the contract's ABI. */
eventFilter(
name: string,
opts: Pick<
EventFilter,
"includeReceipt" | "includeTransaction" | "includeReverted"
> = {},
): EventFilter {
const event = this._findEvent(name);

// Throw an error if the event is not found in the ABI
Expand All @@ -37,8 +42,9 @@ export class Contract {
return {
fromAddress: this.contractAddress,
keys: [hash.getSelectorFromName(event.name) as `0x${string}`],
includeTransaction: false,
includeReceipt: false,
includeTransaction: opts.includeTransaction ?? false,
includeReceipt: opts.includeReceipt ?? false,
includeReverted: opts.includeReverted ?? false,
};
}
}

0 comments on commit 3282cc4

Please sign in to comment.