-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"; |
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"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
But it misses the log 0 from block x and includes log 0 from block x + 1. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./indexerClient.types.js"; |
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this ok ?