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

chore: migrate queries to new indexer #23

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 36 additions & 62 deletions server/src/ext/indexer/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GraphQLClient } from 'graphql-request';
import type { Logger } from 'winston';
import { createLogger } from '@/logger';
import type {
RoundApplicationsQueryResponse,
Expand All @@ -13,52 +15,27 @@ import type {
Claim,
PoolStakesQueryResponse,
} from './types';
import request from 'graphql-request';
import {
getRoundsWithApplications,
getRoundMatchingDistributions,
getRounds,
getPoolStakes,
getPoolStakesAndClaimsByStaker,
} from './queries';
import type { Logger } from 'winston';
import { IsNullError, NotFoundError } from '@/errors';
import { NotFoundError } from '@/errors';
import { env } from '@/env';
import { claimableRounds } from '@/utils/getMerkleAirdrop';
import { createGraphQLClient } from './utils';

class IndexerClient {
private static instance: IndexerClient | null = null;
private readonly indexerEndpoint: string;
private readonly stakingIndexerEndpoint: string;
private readonly logger: Logger;
private readonly alloIndexerClient: GraphQLClient;
private readonly stakingIndexerClient: GraphQLClient;

private constructor() {
this.indexerEndpoint = env.INDEXER_URL ?? '';
this.stakingIndexerEndpoint = env.STAKING_INDEXER_URL ?? '';

if (this.indexerEndpoint === '') {
throw new IsNullError('INDEXER_URL is not set');
}

if (this.stakingIndexerEndpoint === '') {
throw new IsNullError('STAKING_INDEXER_URL is not set');
}

if (this.indexerEndpoint.endsWith('/')) {
this.indexerEndpoint = this.indexerEndpoint.slice(0, -1);
}

if (this.stakingIndexerEndpoint.endsWith('/')) {
this.stakingIndexerEndpoint = this.stakingIndexerEndpoint.slice(0, -1);
}

if (!this.indexerEndpoint.endsWith('/graphql')) {
this.indexerEndpoint += '/graphql';
}

if (!this.stakingIndexerEndpoint.endsWith('/graphql')) {
this.stakingIndexerEndpoint += '/graphql';
}

this.alloIndexerClient = createGraphQLClient(env.INDEXER_URL);
this.stakingIndexerClient = createGraphQLClient(env.STAKING_INDEXER_URL);
this.logger = createLogger('Indexer.ts');
}

Expand Down Expand Up @@ -107,11 +84,11 @@ class IndexerClient {
};

try {
const response: RoundApplicationsQueryResponse = await request(
this.indexerEndpoint,
getRoundsWithApplications,
requestVariables
);
const response: RoundApplicationsQueryResponse =
await this.alloIndexerClient.request(
getRoundsWithApplications,
requestVariables
);

if (response.rounds.length === 0) {
this.logger.warn(
Expand Down Expand Up @@ -144,11 +121,11 @@ class IndexerClient {
}): Promise<PoolOverview[]> {
const requestVariables = { chainId, roundIds };
try {
const response: RoundApplicationsQueryResponse = await request(
this.indexerEndpoint,
getRoundsWithApplications,
requestVariables
);
const response: RoundApplicationsQueryResponse =
await this.alloIndexerClient.request(
getRoundsWithApplications,
requestVariables
);

const rounds = response.rounds;

Expand Down Expand Up @@ -204,11 +181,11 @@ class IndexerClient {
const requestVariables = { chainId, roundId };

try {
const response: RoundMatchingDistributionsQueryResponse = await request(
this.indexerEndpoint,
getRoundMatchingDistributions,
requestVariables
);
const response: RoundMatchingDistributionsQueryResponse =
await this.alloIndexerClient.request(
getRoundMatchingDistributions,
requestVariables
);

if (response.rounds.length === 0) {
this.logger.warn(
Expand Down Expand Up @@ -239,11 +216,8 @@ class IndexerClient {
}): Promise<Round[]> {
const requestVariables = { chainId, roundIds };
try {
const response: GetRoundsQueryResponse = await request(
this.indexerEndpoint,
getRounds,
requestVariables
);
const response: GetRoundsQueryResponse =
await this.alloIndexerClient.request(getRounds, requestVariables);

return response.rounds;
} catch (error) {
Expand All @@ -261,11 +235,11 @@ class IndexerClient {
}): Promise<Stake[]> {
const requestVariables = { chainId, poolId };
try {
const response: PoolStakesQueryResponse = await request(
this.stakingIndexerEndpoint,
getPoolStakes,
requestVariables
);
const response: PoolStakesQueryResponse =
await this.stakingIndexerClient.request(
getPoolStakes,
requestVariables
);

return response.TokenLock_Locked;
} catch (error) {
Expand All @@ -283,11 +257,11 @@ class IndexerClient {
}): Promise<{ staked: Stake[]; unstaked: UnStake[]; claims: Claim[] }> {
const requestVariables = { staker };
try {
const response: PoolStakesAndClaimsQueryResponse = await request(
this.stakingIndexerEndpoint,
getPoolStakesAndClaimsByStaker,
requestVariables
);
const response: PoolStakesAndClaimsQueryResponse =
await this.stakingIndexerClient.request(
getPoolStakesAndClaimsByStaker,
requestVariables
);

return {
staked: response.TokenLock_Locked,
Expand Down
26 changes: 11 additions & 15 deletions server/src/ext/indexer/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gql } from 'graphql-request';

export const getRounds = gql`
query Rounds($chainId: Int!, $roundIds: [String!]!) {
rounds(filter: { chainId: { equalTo: $chainId }, id: { in: $roundIds } }) {
rounds(where: { chainId: { _eq: $chainId }, id: { _in: $roundIds } }) {
id
chainId
roundMetadata
Expand All @@ -15,15 +15,15 @@ export const getRounds = gql`

export const getRoundsWithApplications = gql`
query RoundsApplications($chainId: Int!, $roundIds: [String!]!) {
rounds(filter: { chainId: { equalTo: $chainId }, id: { in: $roundIds } }) {
rounds(where: { chainId: { _eq: $chainId }, id: { _in: $roundIds } }) {
chainId
id
roundMetadata
roundMetadataCid
donationsStartTime
donationsEndTime
matchTokenAddress
applications(filter: { status: { equalTo: APPROVED } }) {
applications(where: { status: { _eq: APPROVED } }) {
id
anchorAddress
metadata
Expand All @@ -32,7 +32,7 @@ export const getRoundsWithApplications = gql`
projectId
totalDonationsCount
totalAmountDonatedInUsd
project: canonicalProject {
project {
metadata
metadataCid
}
Expand All @@ -43,14 +43,14 @@ export const getRoundsWithApplications = gql`

export const getRoundsWithApplicationsStatus = gql`
query RoundsWithApplicationsStatus($chainId: Int!, $roundIds: [String!]!) {
rounds(filter: { chainId: { equalTo: $chainId }, id: { in: $roundIds } }) {
rounds(where: { chainId: { _eq: $chainId }, id: { _in: $roundIds } }) {
chainId
id
roundMetadata
roundMetadataCid
donationsStartTime
donationsEndTime
applications(filter: { status: { equalTo: APPROVED } }) {
applications(where: { status: { _eq: APPROVED } }) {
id
anchorAddress
metadata
Expand All @@ -59,7 +59,7 @@ export const getRoundsWithApplicationsStatus = gql`
projectId
totalDonationsCount
totalAmountDonatedInUsd
project: canonicalProject {
project {
metadata
metadataCid
}
Expand All @@ -70,22 +70,20 @@ export const getRoundsWithApplicationsStatus = gql`

export const getRoundWithApplications = gql`
query RoundApplications($chainId: Int!, $roundId: String!) {
rounds(
filter: { chainId: { equalTo: $chainId }, id: { equalTo: $roundId } }
) {
rounds(where: { chainId: { _eq: $chainId }, id: { _eq: $roundId } }) {
chainId
id
roundMetadata
roundMetadataCid
donationsStartTime
donationsEndTime
applications(filter: { status: { equalTo: APPROVED } }) {
applications(where: { status: { _eq: APPROVED } }) {
id
metadata
metadataCid
status
projectId
project: canonicalProject {
project {
metadata
metadataCid
}
Expand All @@ -112,9 +110,7 @@ export const getApplicationWithRound = gql`

export const getRoundMatchingDistributions = gql`
query RoundMatchingDistributions($chainId: Int!, $roundId: String!) {
rounds(
filter: { chainId: { equalTo: $chainId }, id: { equalTo: $roundId } }
) {
rounds(where: { chainId: { _eq: $chainId }, id: { _eq: $roundId } }) {
matchAmount
donationsEndTime
matchingDistribution
Expand Down
23 changes: 23 additions & 0 deletions server/src/ext/indexer/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GraphQLClient } from 'graphql-request';
import { IsNullError } from '@/errors';

export const createGraphQLClient = (
graphqlEndpoint?: string,
opts?: { envName: string }
) => {
if (!graphqlEndpoint) {
throw new IsNullError(
`${opts?.envName ?? 'graphqlEndpoint variable'} is not set`
);
}

if (graphqlEndpoint.endsWith('/')) {
graphqlEndpoint = graphqlEndpoint.slice(0, -1);
}

if (!graphqlEndpoint.endsWith('/graphql')) {
graphqlEndpoint += '/graphql';
}

return new GraphQLClient(graphqlEndpoint);
};
Loading