-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable no-console, no-await-in-loop */ | ||
import { gql, request } from 'graphql-request'; | ||
|
||
import { SUPPORTED_NETWORK_INFO } from './client'; | ||
|
||
const GRAPH_HEALTH_ENDPOINT = 'https://api.thegraph.com/index-node/graphql'; | ||
|
||
const statusQuery = gql` | ||
query getSubgraphStatus($subgraph: String!) { | ||
status: indexingStatusForCurrentVersion(subgraphName: $subgraph) { | ||
chains { | ||
latestBlock { | ||
number | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const getLatestBlock = async (subgraph: string): Promise<number> => { | ||
const data = await request(GRAPH_HEALTH_ENDPOINT, statusQuery, { | ||
subgraph, | ||
}); | ||
return data.status.chains[0].latestBlock.number; | ||
}; | ||
|
||
const UPDATE_INTERVAL = 10000; | ||
|
||
class SubgraphHealthStore { | ||
graphHealth: Record<string, number> = {}; | ||
|
||
constructor() { | ||
// console.debug('@quest-chains/sdk: health store init'); | ||
this.updateSubgraphHealth(); | ||
} | ||
|
||
public async updateSubgraphHealth() { | ||
await Promise.all( | ||
Object.values(SUPPORTED_NETWORK_INFO).map(async info => { | ||
this.graphHealth[info.chainId] = await getLatestBlock(info.subgraphName); | ||
}), | ||
); | ||
// console.debug('@quest-chains/sdk: updated graph health:', this.graphHealth); | ||
setTimeout(() => this.updateSubgraphHealth(), UPDATE_INTERVAL); | ||
} | ||
|
||
status() { | ||
return this.graphHealth; | ||
} | ||
} | ||
|
||
const HealthStoreSingleton = (function () { | ||
let instance: SubgraphHealthStore; | ||
|
||
function createInstance() { | ||
return new SubgraphHealthStore(); | ||
} | ||
|
||
return { | ||
getInstance: function () { | ||
if (!instance) { | ||
instance = createInstance(); | ||
} | ||
return instance; | ||
}, | ||
}; | ||
})(); | ||
|
||
const getSubgraphStatus = () => HealthStoreSingleton.getInstance().status(); | ||
|
||
const initSubgraphHealthStore = getSubgraphStatus; | ||
|
||
initSubgraphHealthStore(); | ||
|
||
export const getSubgraphLatestBlock = (chainId: string): number => getSubgraphStatus()[chainId]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
|
||
import { Log, TransactionReceipt } from '@ethersproject/abstract-provider'; | ||
import { ethers } from 'ethers'; | ||
|
||
import { getSubgraphLatestBlock } from './graphql/health'; | ||
|
||
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms)); | ||
|
||
const UPDATE_INTERVAL = 10000; | ||
|
||
const MAX_RETRIES = 6; | ||
|
||
export const waitUntilSubgraphIndexed = async (chainId: string, block: number): Promise<boolean> => { | ||
let latestBlock = getSubgraphLatestBlock(chainId); | ||
let tries = 0; | ||
while (latestBlock < block && tries < MAX_RETRIES) { | ||
await sleep(UPDATE_INTERVAL); | ||
tries += 1; | ||
latestBlock = getSubgraphLatestBlock(chainId); | ||
} | ||
return latestBlock >= block; | ||
}; | ||
|
||
export const getQuestChainAddressFromTx = async (receipt: TransactionReceipt) => { | ||
if (!receipt || !receipt.logs) return ''; | ||
const abi = new ethers.utils.Interface(['event QuestChainCreated(uint256 id, address questChain)']); | ||
const eventFragment = abi.events[Object.keys(abi.events)[0]]; | ||
const eventTopic = abi.getEventTopic(eventFragment); | ||
const event = receipt.logs.find((e: Log) => e.topics[0] === eventTopic); | ||
if (event) { | ||
const decodedLog = abi.decodeEventLog(eventFragment, event.data, event.topics); | ||
return decodedLog.questChain; | ||
} | ||
return ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as graphql from './graphql'; | ||
import * as contracts from './contracts'; | ||
import * as metadata from './metadata'; | ||
import * as helpers from './helpers'; | ||
|
||
export { graphql, contracts, metadata }; | ||
export { graphql, contracts, metadata, helpers }; |