Skip to content

Commit

Permalink
feat: types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomariscal committed Jul 17, 2024
1 parent 2eec17c commit 6383952
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GraphQLClient } from 'graphql-request';
import type {
AnnouncementSubgraphQueryVariables,
GetAnnouncementsUsingSubgraphParams,
GetAnnouncementsUsingSubgraphReturnType,
SubgraphAnnouncementEntity
Expand Down Expand Up @@ -50,15 +51,18 @@ async function getAnnouncementsUsingSubgraph({
}
`;

const variables = {
const variables: AnnouncementSubgraphQueryVariables = {
fromBlock: fromBlock ? Number(fromBlock) : undefined,
toBlock: toBlock ? Number(toBlock) : undefined,
caller: filterOptions?.caller
};

const allAnnouncements: AnnouncementLog[] = [];

for await (const batch of fetchPages<SubgraphAnnouncementEntity>({
for await (const batch of fetchPages<
SubgraphAnnouncementEntity,
AnnouncementSubgraphQueryVariables
>({
client,
gqlQuery,
variables,
Expand Down
16 changes: 12 additions & 4 deletions src/lib/actions/getAnnouncementsUsingSubgraph/subgraphHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { SubgraphAnnouncementEntity } from './types';
import type { AnnouncementLog } from '../getAnnouncements/types';
import { ERC5564_CONTRACT } from '../../../config';

export async function* fetchPages<T>({
export type PaginationVariables = {
first: number;
skip: number;
};

export async function* fetchPages<T, V extends Record<string, unknown>>({
client,
gqlQuery,
variables,
Expand All @@ -12,16 +17,19 @@ export async function* fetchPages<T>({
}: {
client: GraphQLClient;
gqlQuery: string;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
variables: Record<string, any>;
variables: V;
pageSize: number;
entity: 'announcementEntities'; // The name of the entity to fetch from the subgraph
}): AsyncGenerator<T[], void, undefined> {
let skip = 0;
let moreData = true;

while (moreData) {
const currentVariables = { ...variables, first: pageSize, skip };
const currentVariables = {
...variables,
first: pageSize,
skip
};
try {
const response = await client.request<{ [key: string]: T[] }>(
gqlQuery,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/actions/getAnnouncementsUsingSubgraph/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export type GetAnnouncementsUsingSubgraphParams = {
pageSize?: number;
};

export type AnnouncementSubgraphQueryVariables = {
fromBlock?: number;
toBlock?: number;
caller?: string;
};

export type GetAnnouncementsUsingSubgraphFilterOptions = {
caller?: `0x${string}`;
};
Expand Down

0 comments on commit 6383952

Please sign in to comment.