diff --git a/packages/indexer-common/src/graph-node.ts b/packages/indexer-common/src/graph-node.ts index 5157ea4a2..e8b895b59 100644 --- a/packages/indexer-common/src/graph-node.ts +++ b/packages/indexer-common/src/graph-node.ts @@ -60,7 +60,11 @@ export const parseGraphQLBlockPointer = (block: any): BlockPointer | null => } : null -export class GraphNode { +export interface GraphNodeInterface { + indexingStatus(deployments: SubgraphDeploymentID[]): Promise +} + +export class GraphNode implements GraphNodeInterface { admin: RpcClient private queryBaseURL: URL status: Client @@ -442,9 +446,14 @@ export class GraphNode { }` const queryIndexingStatuses = async () => { - const result = await this.status - .query(query, { deployments: deployments.map((id) => id.ipfsHash) }) - .toPromise() + let result + try { + result = await this.status + .query(query, { deployments: deployments.map((id) => id.ipfsHash) }) + .toPromise() + } catch (error) { + throw indexerError(IndexerErrorCode.IE018, error) + } return ( result.data.indexingStatuses diff --git a/packages/indexer-common/src/subgraphs.ts b/packages/indexer-common/src/subgraphs.ts index 238ff43de..1998ad209 100644 --- a/packages/indexer-common/src/subgraphs.ts +++ b/packages/indexer-common/src/subgraphs.ts @@ -1,7 +1,7 @@ import { base58 } from 'ethers/lib/utils' import { BigNumber, utils } from 'ethers' import { Logger, SubgraphDeploymentID } from '@graphprotocol/common-ts' -import { SubgraphDeployment, SubgraphManifestSchema } from './types' +import { SubgraphDeployment, SubgraphManifestSchema, BlockPointer } from './types' import { INDEXING_RULE_GLOBAL, IndexingDecisionBasis, @@ -13,6 +13,7 @@ import { QueryResult } from './network-subgraph' import { mergeSelectionSets, sleep } from './utils' import * as yaml from 'yaml' import { indexerError, IndexerErrorCode } from './errors' +import { GraphNodeInterface } from './graph-node' import { z } from 'zod' export enum SubgraphIdentifierType { @@ -640,3 +641,41 @@ export async function fetchSubgraphManifest( throw indexerError(IndexerErrorCode.IE075, message) } } + +interface IndexingStatus { + latestBlock: BlockPointer | null + health: string + synced: boolean +} + +interface SubgraphGraftStatus extends GraftableSubgraph { + indexingStatus: IndexingStatus | null +} + +export async function getIndexingStatusOfGraftableSubgraph( + subgraph: GraftableSubgraph, + graphNode: GraphNodeInterface, +): Promise { + let response + try { + response = await graphNode.indexingStatus([subgraph.deployment]) + } catch (error) { + const message = `Failed to fetch indexing status when resolving subgraph grafts` + // TODO: log this error + throw indexerError(IndexerErrorCode.IE075, { message, error }) + } + let indexingStatus: IndexingStatus | null = null + if (response && response.length) { + const subgraphIndexingStatus = response[0] + let latestBlock: BlockPointer | null = null + if (subgraphIndexingStatus.chains && subgraphIndexingStatus.chains.length) { + latestBlock = subgraphIndexingStatus.chains[0].latestBlock + } + indexingStatus = { + health: subgraphIndexingStatus.health, + synced: subgraphIndexingStatus.synced, + latestBlock, + } + } + return { ...subgraph, indexingStatus } +}