diff --git a/package.json b/package.json index ed96038b..e54f3577 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "@emotion/react": "^11.10.4", "@emotion/styled": "^11.10.4", "@gnosis.pm/safe-apps-web3modal": "^17.0.0", - "@quest-chains/sdk": "^0.1.11", + "@quest-chains/sdk": "^0.1.12", "@react-three/drei": "9.26.1", "@react-three/fiber": "8.7.1", "@walletconnect/web3-provider": "^1.8.0", diff --git a/pages/_app.tsx b/pages/_app.tsx index 673fb232..a81e326e 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -11,15 +11,11 @@ import React, { useEffect } from 'react'; import { Toaster } from 'react-hot-toast'; import { AppLayout } from '@/components/Layout/AppLayout'; -import { initGraphHealthStore } from '@/stores/graphHealth'; import { globalStyles, theme } from '@/utils/theme'; import { WalletProvider } from '@/web3'; const App = ({ Component, pageProps }: AppProps): JSX.Element => { const router = useRouter(); - useEffect(() => { - initGraphHealthStore(); - }, []); function ForceDarkMode(props: { children: JSX.Element }) { const { colorMode, toggleColorMode } = useColorMode(); diff --git a/stores/graphHealth.ts b/stores/graphHealth.ts deleted file mode 100644 index 29991c96..00000000 --- a/stores/graphHealth.ts +++ /dev/null @@ -1,74 +0,0 @@ -/* eslint-disable no-console, no-await-in-loop */ -import { gql, request } from 'graphql-request'; - -import { GRAPH_HEALTH_ENDPOINT } from '@/utils/constants'; -import { SUPPORTED_NETWORK_INFO } from '@/web3/networks'; - -const statusQuery = gql` - query getGraphStatus($subgraph: String!) { - status: indexingStatusForCurrentVersion(subgraphName: $subgraph) { - chains { - latestBlock { - number - } - } - } - } -`; - -export const getLatestBlock = async (subgraph: string): Promise => { - const data = await request(GRAPH_HEALTH_ENDPOINT, statusQuery, { - subgraph, - }); - return data.status.chains[0].latestBlock.number; -}; - -const UPDATE_INTERVAL = 10000; - -class GraphHealthStore { - graphHealth: Record = {}; - - constructor() { - this.updateGraphHealth(); - } - - public async updateGraphHealth() { - await Promise.all( - Object.values(SUPPORTED_NETWORK_INFO).map(async info => { - this.graphHealth[info.chainId] = await getLatestBlock( - info.subgraphName, - ); - }), - ); - console.log('Updated Graph Health', this.graphHealth); - setTimeout(() => this.updateGraphHealth(), UPDATE_INTERVAL); - } - - status() { - return this.graphHealth; - } -} - -const HealthStoreSingleton = (function () { - let instance: GraphHealthStore; - - function createInstance() { - return new GraphHealthStore(); - } - - return { - getInstance: function () { - if (!instance) { - instance = createInstance(); - } - return instance; - }, - }; -})(); - -const getGraphStatus = () => HealthStoreSingleton.getInstance().status(); - -export const initGraphHealthStore = getGraphStatus; - -export const getGraphLatestBlock = (chainId: string): number => - getGraphStatus()[chainId]; diff --git a/utils/graphHelpers.ts b/utils/graphHelpers.ts index 3c834911..0aab510c 100644 --- a/utils/graphHelpers.ts +++ b/utils/graphHelpers.ts @@ -1,44 +1,5 @@ -/* eslint-disable no-await-in-loop */ +import { helpers } from '@quest-chains/sdk'; -import { Log, TransactionReceipt } from '@ethersproject/abstract-provider'; -import { ethers } from 'ethers'; +export const waitUntilBlock = helpers.waitUntilSubgraphIndexed; -import { getGraphLatestBlock } from '@/stores/graphHealth'; -import { sleep } from '@/utils/helpers'; - -const UPDATE_INTERVAL = 10000; - -const MAX_RETRIES = 6; - -export const waitUntilBlock = async ( - chainId: string, - block: number, -): Promise => { - let latestBlock = getGraphLatestBlock(chainId); - let tries = 0; - while (latestBlock < block && tries < MAX_RETRIES) { - await sleep(UPDATE_INTERVAL); - tries += 1; - latestBlock = getGraphLatestBlock(chainId); - } - return latestBlock >= block; -}; - -export const awaitQuestChainAddress = 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 ''; -}; +export const awaitQuestChainAddress = helpers.getQuestChainAddressFromTx; diff --git a/yarn.lock b/yarn.lock index a7329b65..3d5853f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2083,10 +2083,10 @@ resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.11.2.tgz#830beaec4b4091a9e9398ac50f865ddea52186b9" integrity sha512-92FRmppjjqz29VMJ2dn+xdyXZBrMlE42AV6Kq6BwjWV7CNUW1hs2FtxSNLQE+gJhaZ6AAmYuO9y8dshhcBl7vA== -"@quest-chains/sdk@^0.1.11": - version "0.1.11" - resolved "https://registry.yarnpkg.com/@quest-chains/sdk/-/sdk-0.1.11.tgz#fec53258b4c16d1ceb037eadd0565d392003da78" - integrity sha512-EVKK9CeMUTKMqyQEnYtYFvv3YLQhA9TDoHwjf673xGrfONW5jp8LWf2/lBTZtWmzHaca5KUM/+UqZo4HEtZk7A== +"@quest-chains/sdk@^0.1.12": + version "0.1.12" + resolved "https://registry.yarnpkg.com/@quest-chains/sdk/-/sdk-0.1.12.tgz#c4baa1a51be23997a2520d7c51901736e395b673" + integrity sha512-xt6vDa+mSEw4I6En407utHEac91ynUmVebe6r5DqRwbHLIbbZXqf1H9U+mBcTtDyOKhGukeOjF/kGVltjc+MhQ== dependencies: ajv "^8.11.0" axios "^1.1.1"