Skip to content

Commit

Permalink
fix: write a generic method and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Dec 18, 2024
1 parent aa49ef6 commit 1386695
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 214 deletions.
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";
25 changes: 6 additions & 19 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 @@ -20,23 +22,8 @@ export interface IIndexerClient {

/**
* 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
* @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 } }
]
}
]
`);
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

0 comments on commit 1386695

Please sign in to comment.