-
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 1 commit
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,5 +1,5 @@ | ||
import { IIndexerClient } from "@grants-stack-indexer/indexer-client"; | ||
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared"; | ||
import { Address, AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared"; | ||
|
||
import { IEventsFetcher } from "./interfaces/index.js"; | ||
|
||
|
@@ -19,4 +19,21 @@ export class EventsFetcher implements IEventsFetcher { | |
limit, | ||
); | ||
} | ||
|
||
/** @inheritdoc */ | ||
async fetchEventsBySrcAddress(params: { | ||
chainId: ChainId; | ||
srcAddresses: Address[]; | ||
from?: { | ||
blockNumber?: number; | ||
logIndex?: number; | ||
}; | ||
to: { | ||
blockNumber: number; | ||
logIndex: number; | ||
}; | ||
limit?: number; | ||
}): Promise<AnyIndexerFetchedEvent[]> { | ||
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. As a sidenote, you got the Is there any particular reason for that? And another question 👉 have you ever considered leveraging some kind of query builder pattern for the event fetcher? You could end up with a cool class: const query = EventsFetcher.buildQuery()
.withChainId(chainId)
.withSrcAddressess(addresses)
.withBlockNumber(blockNumber)
... If you envision having more 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. regarding first, now that you mention that you're right, originally i wrote the method only with regarding second, i like query builders 😄 but i don't think we will benefit too much for now 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. i keep the old method until i integrate it with the rest of the code cuz if not i'll break everything |
||
return this.indexerClient.getEventsBySrcAddress(params); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared"; | ||
import { Address, AnyIndexerFetchedEvent, ChainId } from "@grants-stack-indexer/shared"; | ||
|
||
/** | ||
* Interface for the events fetcher | ||
|
@@ -17,4 +17,26 @@ 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 | ||
*/ | ||
fetchEventsBySrcAddress(params: { | ||
Comment on lines
+22
to
+29
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. is this ok ? |
||
chainId: ChainId; | ||
srcAddresses: Address[]; | ||
from?: { | ||
blockNumber?: number; | ||
logIndex?: number; | ||
}; | ||
to: { | ||
blockNumber: number; | ||
logIndex: number; | ||
}; | ||
limit?: number; | ||
}): Promise<AnyIndexerFetchedEvent[]>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { gql, GraphQLClient } from "graphql-request"; | ||
|
||
import { AnyIndexerFetchedEvent, ChainId, stringify } from "@grants-stack-indexer/shared"; | ||
import { Address, AnyIndexerFetchedEvent, ChainId, stringify } from "@grants-stack-indexer/shared"; | ||
|
||
import { IndexerClientError, InvalidIndexerResponse } from "../exceptions/index.js"; | ||
import { IIndexerClient } from "../internal.js"; | ||
|
@@ -73,4 +73,95 @@ export class EnvioIndexerClient implements IIndexerClient { | |
throw new IndexerClientError(stringify(error, Object.getOwnPropertyNames(error))); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
async getEventsBySrcAddress(params: { | ||
chainId: ChainId; | ||
srcAddresses: Address[]; | ||
from?: { | ||
blockNumber?: number; | ||
logIndex?: number; | ||
}; | ||
to: { | ||
blockNumber: number; | ||
logIndex: number; | ||
}; | ||
limit?: number; | ||
}): Promise<AnyIndexerFetchedEvent[]> { | ||
try { | ||
const { chainId, srcAddresses, from, to, limit = 100 } = params; | ||
const { blockNumber: toBlock, logIndex: toLogIndex } = to; | ||
const { blockNumber: fromBlock, logIndex: fromLogIndex } = from ?? { | ||
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. What if 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. good catch |
||
blockNumber: 0, | ||
logIndex: 0, | ||
}; | ||
const response = (await this.client.request( | ||
gql` | ||
query getEventsBySrcAddress( | ||
$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: { | ||
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 } } | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
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 | ||
} | ||
} | ||
`, | ||
{ chainId, srcAddresses, fromBlock, fromLogIndex, toBlock, toLogIndex, 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))); | ||
} | ||
} | ||
} |
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.
should it be like this ?