Skip to content

Commit

Permalink
common: implement getIndexingStatusOfGraftableSubgraph function
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Oct 24, 2023
1 parent 0d8de24 commit 1e6582c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/indexer-common/src/graph-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export const parseGraphQLBlockPointer = (block: any): BlockPointer | null =>
}
: null

export class GraphNode {
export interface GraphNodeInterface {
indexingStatus(deployments: SubgraphDeploymentID[]): Promise<IndexingStatus[]>
}

export class GraphNode implements GraphNodeInterface {
admin: RpcClient
private queryBaseURL: URL
status: Client
Expand Down Expand Up @@ -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
Expand Down
41 changes: 40 additions & 1 deletion packages/indexer-common/src/subgraphs.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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<SubgraphGraftStatus> {
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 }
}

0 comments on commit 1e6582c

Please sign in to comment.