Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch events by src addresses #44

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/data-flow/src/eventsFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IIndexerClient } from "@grants-stack-indexer/indexer-client";
import { GetEventsFilters, IIndexerClient } from "@grants-stack-indexer/indexer-client";
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";

import { IEventsFetcher } from "./interfaces/index.js";
Expand All @@ -19,4 +19,9 @@ export class EventsFetcher implements IEventsFetcher {
limit,
);
}

/** @inheritdoc */
async fetchEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]> {
return this.indexerClient.getEvents(params);
}
}
11 changes: 11 additions & 0 deletions packages/data-flow/src/interfaces/eventsFetcher.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GetEventsFilters } from "@grants-stack-indexer/indexer-client";
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";

/**
Expand All @@ -17,4 +18,14 @@ export interface IEventsFetcher {
logIndex: number,
limit?: number,
): Promise<AnyIndexerFetchedEvent[]>;

/**
* Fetch the events by src address, block number and log index for a chain
* @param chainId id of the chain
* @param srcAddresses src addresses to fetch events from
* @param toBlock block number to fetch events from
* @param logIndex log index in the block to fetch events from
* @param limit limit of events to fetch
*/
Comment on lines +22 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this ok ?

fetchEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]>;
}
1 change: 1 addition & 0 deletions packages/data-flow/test/unit/eventsFetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("EventsFetcher", () => {
beforeEach(() => {
indexerClientMock = {
getEventsAfterBlockNumberAndLogIndex: vi.fn(),
getEvents: vi.fn(),
};

eventsFetcher = new EventsFetcher(indexerClientMock);
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-client/src/external.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type { IIndexerClient } from "./internal.js";

export { EnvioIndexerClient } from "./internal.js";

export type { GetEventsFilters } from "./internal.js";
9 changes: 9 additions & 0 deletions packages/indexer-client/src/interfaces/indexerClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";

import { GetEventsFilters } from "../internal.js";

/**
* Interface for the indexer client
*/
Expand All @@ -17,4 +19,11 @@ export interface IIndexerClient {
logIndex: number,
limit?: number,
): Promise<AnyIndexerFetchedEvent[]>;

/**
* Get the events by filters from the indexer service
* @param params Filters to fetch events
* @returns Events fetched from the indexer service
*/
getEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]>;
}
1 change: 1 addition & 0 deletions packages/indexer-client/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./exceptions/index.js";
export * from "./types/index.js";
export * from "./interfaces/index.js";
export * from "./providers/index.js";
102 changes: 101 additions & 1 deletion packages/indexer-client/src/providers/envioIndexerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gql, GraphQLClient } from "graphql-request";
import { AnyIndexerFetchedEvent, ChainId, stringify } from "@grants-stack-indexer/shared";

import { IndexerClientError, InvalidIndexerResponse } from "../exceptions/index.js";
import { IIndexerClient } from "../internal.js";
import { GetEventsFilters, IIndexerClient } from "../internal.js";

/**
* Indexer client for the Envio indexer service
Expand Down Expand Up @@ -73,4 +73,104 @@ export class EnvioIndexerClient implements IIndexerClient {
throw new IndexerClientError(stringify(error, Object.getOwnPropertyNames(error)));
}
}

/** @inheritdoc */
async getEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]> {
try {
const { chainId, srcAddresses, from, to, limit = 100 } = params;

// Build the _and conditions array
const andConditions = [];
andConditions.push(`chain_id: { _eq: $chainId }`);
const vars: Record<string, unknown> = { chainId };

// Add srcAddresses filter if provided
if (srcAddresses && srcAddresses.length > 0) {
andConditions.push(`src_address: { _in: $srcAddresses }`);
vars["srcAddresses"] = srcAddresses;
}

if (from != undefined && from != null) {
andConditions.push(`
_or: [
{ block_number: { _gt: $fromBlock } },
{
_and: [
{ block_number: { _eq: $fromBlock } },
{ log_index: { _gt: $fromLogIndex } }
]
}
]
Comment on lines +96 to +103
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be relevant as you might be using this provider in a super specific way but how would you get all events from a single block?

I can only think of something like this:

from: { blockNumber: x, logIndex: 0 }
to: { blockNumber: x + 1, logIndex: 0 }

But it misses the log 0 from block x and includes log 0 from block x + 1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't think of this case since we fetch events individually (blockNumber and logIndex), not by blocks but i get your point. i will add it to tech debt

`);
vars["fromBlock"] = from.blockNumber;
vars["fromLogIndex"] = from.logIndex;
}

if (to != undefined && to != null) {
andConditions.push(`
_or: [
{ block_number: { _lt: $toBlock } },
{
_and: [
{ block_number: { _eq: $toBlock } },
{ log_index: { _lte: $toLogIndex } }
]
}
]
`);
vars["toBlock"] = to.blockNumber;
vars["toLogIndex"] = to.logIndex;
}

const whereClause =
andConditions.length > 1
? `_and: [{ ${andConditions.join(" }, { ")} }]`
: andConditions[0];

const response = (await this.client.request(
gql`
query getEvents(
$chainId: Int!
$srcAddresses: [String!]
$fromBlock: Int
$fromLogIndex: Int
$toBlock: Int
$toLogIndex: Int
$limit: Int!
) {
raw_events(
order_by: [{ block_number: asc }, { log_index: asc }]
where: { ${whereClause} }
limit: $limit
) {
blockNumber: block_number
blockTimestamp: block_timestamp
chainId: chain_id
contractName: contract_name
eventName: event_name
logIndex: log_index
params
srcAddress: src_address
transactionFields: transaction_fields
}
}
`,
{
...vars,
limit,
},
)) as { raw_events: AnyIndexerFetchedEvent[] };

if (response?.raw_events) {
return response.raw_events;
} else {
throw new InvalidIndexerResponse(stringify(response));
}
} catch (error) {
if (error instanceof InvalidIndexerResponse) {
throw error;
}
throw new IndexerClientError(stringify(error, Object.getOwnPropertyNames(error)));
}
}
}
1 change: 1 addition & 0 deletions packages/indexer-client/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./indexerClient.types.js";
36 changes: 36 additions & 0 deletions packages/indexer-client/src/types/indexerClient.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Address, ChainId } from "@grants-stack-indexer/shared";

export type GetEventsFilters = {
/**
* Id of the chain to fetch events from
*/
chainId: ChainId;
/**
* Src addresses to filter events by
*/
srcAddresses?: Address[];
from?: {
/**
* Block number to start fetching events from
*/
blockNumber: number;
/**
* Log index in the block
*/
logIndex: number;
};
to?: {
/**
* Block number to end fetching events at
*/
blockNumber: number;
/**
* Log index in the block
*/
logIndex: number;
};
/**
* Limit of events to fetch
*/
limit?: number;
};
Loading
Loading