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: events fetcher & indexer client #4

Merged
merged 8 commits into from
Oct 10, 2024
Merged

feat: events fetcher & indexer client #4

merged 8 commits into from
Oct 10, 2024

Conversation

0xkenj1
Copy link
Collaborator

@0xkenj1 0xkenj1 commented Oct 8, 2024

🤖 Linear

Closes GIT-53 GIT-84 GIT-85

Description

Implementation of

  • data-flow => EventsFetcher
  • indexer-client => EnvioIndexerClient

Checklist before requesting a review

  • I have conducted a self-review of my code.
  • I have conducted a QA.
  • If it is a core feature, I have included comprehensive tests.

Copy link

linear bot commented Oct 8, 2024

packages/data-flow/package.json Outdated Show resolved Hide resolved
packages/data-flow/src/eventsFetcher.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,10 @@
import { AnyProtocolEvent } from "@grants-stack-indexer/shared";

Copy link
Collaborator

Choose a reason for hiding this comment

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

add natspec

packages/data-flow/src/interfaces/index.ts Outdated Show resolved Hide resolved
packages/indexer-client/package.json Outdated Show resolved Hide resolved
limit: number = 100,
): Promise<AnyProtocolEvent[]> {
try {
const response = (await this.client.rawRequest(gql`
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this library provide a way to type the response?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will investigate about that , maybe we can use some generics :)

query getEventsByChainIdBlockNumberAndLogIndex {
raw_events(
where: {
chain_id: { _eq: ${chainId} }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is ok for now but rather than using string interpolation we can use variables which also prevent injection issues

see variables:
https://graphql.org/learn/queries/

in this case I think you could do this

const query = gql`
    query getEventsByChainIdBlockNumberAndLogIndex($chainId: Int!, $blockNumber: Int!, $logIndex: Int!, $limit: Int!) {
        raw_events(
            where: {
                chain_id: { _eq: $chainId }
                block_number: { _gte: $blockNumber }
                log_index: { _gt: $logIndex }
            }
            limit: $limit
        ) {
            block_number
            block_timestamp
            chain_id
            contract_name
            event_name
            log_index
            params
            src_address
        }
    }
`;

const response = await this.client.request(query, {
    chainId, blockNumber, logIndex, limit,
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks for this recommendation, this makes lot more sense :)

Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

Starting to get deep into the core of the project 🫡

packages/data-flow/src/exceptions/index.ts Outdated Show resolved Hide resolved
export interface IEventsFetcher {
fetcEventsByBlockNumberAndLogIndex(
chainId: number,
blockNumber: number,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should blockNumber be a BigInt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Now I'm thinking if ProtocolEvent["block_number"] should also be bigint 🤔

Copy link
Collaborator Author

@0xkenj1 0xkenj1 Oct 9, 2024

Choose a reason for hiding this comment

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

Not sure if we need it actually. Number.MAX_SAFE_INTEGER = 9007199254740991 :rofl

Copy link
Collaborator

Choose a reason for hiding this comment

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

Viem treats it like bigint, we will have an inconsistency here. We need to define a common type at least to the internal limits of our backend, and only convert to number or bigint when interfacing with external (IndexerClient, Viem lib, etc)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Along the lines of Nigiri's comment, it's more of a semantic type thingy.

Whenever you get a block number out of viem or any other client, you as a dev will need to know (and be extremely sure) that you are working with a particular set of block numbers that can be safely casted from bigint to number. By using a bigint, you just forget about that problem in the future. You move from a "it will probably work" scenario to a "it works" scenario.

this.client.setHeader("x-hasura-admin-secret", secret);
}

public async getEventsByBlockNumberAndLogIndex(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Super small detail but feels like it could be more descriptive to name this method as

Suggested change
public async getEventsByBlockNumberAndLogIndex(
public async getEventsAfterBlockNumberAndLogIndex(

0xyaco
0xyaco previously approved these changes Oct 9, 2024
jahabeebs
jahabeebs previously approved these changes Oct 9, 2024
@0xkenj1 0xkenj1 dismissed stale reviews from jahabeebs and 0xyaco via 9cdbedf October 10, 2024 14:30
@0xkenj1 0xkenj1 requested review from 0xyaco and jahabeebs October 10, 2024 14:34
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

Interested in seeing if it's possible to type gql requests, would be awesome

Copy link
Collaborator

@0xnigir1 0xnigir1 left a comment

Choose a reason for hiding this comment

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

as Yaco said, would be great to type the GraphQL queries (maybe defining the queries in a separate .gql file or the schemas)

@0xkenj1 0xkenj1 merged commit 41ff0c0 into dev Oct 10, 2024
6 checks passed
@0xkenj1 0xkenj1 deleted the feat/events-fetcher branch October 10, 2024 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants