Skip to content

Commit

Permalink
common: update EpochSubgraph to always query the block number
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Sep 4, 2023
1 parent 892ef68 commit 458ce15
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
34 changes: 28 additions & 6 deletions packages/indexer-common/src/epoch-subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ import axios, { AxiosInstance, AxiosResponse } from 'axios'
import { DocumentNode, print } from 'graphql'
import { CombinedError } from '@urql/core'
import { QueryResult } from './network-subgraph'
import gql from 'graphql-tag'
import { mergeSelectionSets } from './utils'
import { Logger } from '@graphprotocol/common-ts'

const blockNumberQuery = gql`
{
_meta {
block {
number
}
}
}
`

export class EpochSubgraph {
private constructor(private endpointClient: AxiosInstance) {}
endpointClient: AxiosInstance
logger: Logger

public static async create(endpoint: string): Promise<EpochSubgraph> {
const endpointClient = axios.create({
constructor(endpoint: string, logger: Logger) {
this.endpointClient = axios.create({
baseURL: endpoint,
headers: { 'content-type': 'application/json' },

Expand All @@ -17,8 +31,7 @@ export class EpochSubgraph {
// Don't transform responses
transformResponse: (data) => data,
})
// Create the Epoch subgraph instance
return new EpochSubgraph(endpointClient)
this.logger = logger
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -27,11 +40,20 @@ export class EpochSubgraph {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
variables?: Record<string, any>,
): Promise<QueryResult<Data>> {
// Include block number in all queries
let updatedQuery
try {
updatedQuery = mergeSelectionSets(query, blockNumberQuery)
} catch (e) {
updatedQuery = query
}

const response = await this.endpointClient.post('', {
query: print(query),
query: print(updatedQuery),
variables,
})
const data = JSON.parse(response.data)
this.logger.trace('Epoch Subgraph query', { data })
if (data.errors) {
return { error: new CombinedError({ graphQLErrors: data.errors }) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ const setupMonitor = async () => {
'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-goerli',
deployment: undefined,
})
epochSubgraph = await EpochSubgraph.create(
epochSubgraph = new EpochSubgraph(
'https://api.thegraph.com/subgraphs/name/graphprotocol/goerli-epoch-block-oracle',
logger,
)
graphNode = new GraphNode(
logger,
Expand Down
5 changes: 0 additions & 5 deletions packages/indexer-common/src/indexer-management/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,6 @@ export class NetworkMonitor {
}
}
}
_meta {
block {
number
}
}
}
`,
{
Expand Down
3 changes: 2 additions & 1 deletion packages/indexer-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ export class Network {
// * -----------------------------------------------------------------------
// * Epoch Subgraph
// * -----------------------------------------------------------------------
const epochSubgraph = await EpochSubgraph.create(
const epochSubgraph = new EpochSubgraph(
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion --
* Accept the non-null `url` property of the Epoch Subgraph, as it has
* already been validated during parsing. Once indexing is supported,
* initialize it in the same way as the NetworkSubgraph
*/
specification.subgraphs.epochSubgraph.url!,
logger.child({ component: 'EpochSubgraph' }),
)

// * -----------------------------------------------------------------------
Expand Down

0 comments on commit 458ce15

Please sign in to comment.