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 2 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
20 changes: 4 additions & 16 deletions packages/data-flow/src/eventsFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IIndexerClient } from "@grants-stack-indexer/indexer-client";
import { Address, AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";
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 @@ -21,19 +21,7 @@ export class EventsFetcher implements IEventsFetcher {
}

/** @inheritdoc */
async fetchEventsBySrcAddress(params: {
chainId: ChainId;
srcAddresses: Address[];
from?: {
blockNumber?: number;
logIndex?: number;
};
to: {
blockNumber: number;
logIndex: number;
};
limit?: number;
}): Promise<AnyIndexerFetchedEvent[]> {
return this.indexerClient.getEventsBySrcAddress(params);
async fetchEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]> {
return this.indexerClient.getEvents(params);
}
}
17 changes: 3 additions & 14 deletions packages/data-flow/src/interfaces/eventsFetcher.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";
import { GetEventsFilters } from "@grants-stack-indexer/indexer-client";
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";

/**
* Interface for the events fetcher
Expand Down Expand Up @@ -26,17 +27,5 @@ export interface IEventsFetcher {
* @param logIndex log index in the block to fetch events from
* @param limit limit of events to fetch
*/
fetchEventsBySrcAddress(params: {
chainId: ChainId;
srcAddresses: Address[];
from?: {
blockNumber?: number;
logIndex?: number;
};
to: {
blockNumber: number;
logIndex: number;
};
limit?: number;
}): Promise<AnyIndexerFetchedEvent[]>;
fetchEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]>;
}
2 changes: 1 addition & 1 deletion packages/data-flow/test/unit/eventsFetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("EventsFetcher", () => {
beforeEach(() => {
indexerClientMock = {
getEventsAfterBlockNumberAndLogIndex: vi.fn(),
getEventsBySrcAddress: 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";
27 changes: 7 additions & 20 deletions packages/indexer-client/src/interfaces/indexerClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Address, AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared";

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

/**
* Interface for the indexer client
Expand All @@ -19,24 +21,9 @@ export interface IIndexerClient {
): Promise<AnyIndexerFetchedEvent[]>;

/**
* Get the events by src address from the indexer service
* @param chainId Id of the chain
* @param srcAddresses Src addresses to fetch events from
* @param from Block number to start fetching events from
* @param logIndex Log index in the block
* @param limit Limit of events to fetch
* Get the events by filters from the indexer service
* @param params Filters to fetch events
* @returns Events fetched from the indexer service
*/
getEventsBySrcAddress(params: {
chainId: ChainId;
srcAddresses: Address[];
from?: {
blockNumber?: number;
logIndex?: number;
};
to: {
blockNumber: number;
logIndex: number;
};
limit?: number;
}): Promise<AnyIndexerFetchedEvent[]>;
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";
119 changes: 64 additions & 55 deletions packages/indexer-client/src/providers/envioIndexerClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { gql, GraphQLClient } from "graphql-request";

import { Address, AnyIndexerFetchedEvent, ChainId, stringify } from "@grants-stack-indexer/shared";
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 @@ -75,67 +75,72 @@ export class EnvioIndexerClient implements IIndexerClient {
}

/** @inheritdoc */
async getEventsBySrcAddress(params: {
chainId: ChainId;
srcAddresses: Address[];
from?: {
blockNumber?: number;
logIndex?: number;
};
to: {
blockNumber: number;
logIndex: number;
};
limit?: number;
}): Promise<AnyIndexerFetchedEvent[]> {
async getEvents(params: GetEventsFilters): Promise<AnyIndexerFetchedEvent[]> {
try {
const { chainId, srcAddresses, from, to, limit = 100 } = params;
const { blockNumber: toBlock, logIndex: toLogIndex } = to;
const { blockNumber: fromBlock, logIndex: fromLogIndex } = from ?? {
blockNumber: 0,
logIndex: 0,
};

// 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 getEventsBySrcAddress(
query getEvents(
$chainId: Int!
$srcAddresses: [String!]!
$fromBlock: Int!
$fromLogIndex: Int!
$toBlock: Int!
$toLogIndex: Int!
$srcAddresses: [String!]
$fromBlock: Int
$fromLogIndex: Int
$toBlock: Int
$toLogIndex: Int
$limit: Int!
) {
raw_events(
order_by: [{ block_number: asc }, { log_index: asc }]
where: {
chain_id: { _eq: $chainId }
src_address: { _in: $srcAddresses }
_and: [
{
_or: [
{ block_number: { _gt: $fromBlock } }
{
_and: [
{ block_number: { _eq: $fromBlock } }
{ log_index: { _gt: $fromLogIndex } }
]
}
]
}
{
_or: [
{ block_number: { _lt: $toBlock } }
{
_and: [
{ block_number: { _eq: $toBlock } }
{ log_index: { _lte: $toLogIndex } }
]
}
]
}
]
}
where: { ${whereClause} }
limit: $limit
) {
blockNumber: block_number
Expand All @@ -150,8 +155,12 @@ export class EnvioIndexerClient implements IIndexerClient {
}
}
`,
{ chainId, srcAddresses, fromBlock, fromLogIndex, toBlock, toLogIndex, limit },
{
...vars,
limit,
},
)) as { raw_events: AnyIndexerFetchedEvent[] };

if (response?.raw_events) {
return response.raw_events;
} else {
Expand Down
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