import axios from 'axios' import { sleep } from './helpers' import { AVAX_TESTNET, Chain } from './chain' export enum SubgraphType { DEX, } type IUrlMap = { [chain in Chain]?: { [subgraphType in SubgraphType]: string } } export class BaseSubgraph { private static readonly urlMap: IUrlMap = { [AVAX_TESTNET]: { [SubgraphType.DEX]: 'https://api.thegraph.com/subgraphs/name/raupprafael/ryze-dex-fuji', }, } protected static url(chain: Chain, type: SubgraphType) { return this.urlMap[chain]?.[type] } protected static _blockString(block?: number) { return block ? `block: { number_gte: ${ block } }, ` : '' } protected static async _query( url: string, operationName: string, query: string, variables: object = {}, ): Promise { const response = (await axios.post( url, { operationName, query, variables, }, )).data const errorMessage = response?.errors?.[0]?.message?.toLowerCase() if (errorMessage) { if ( errorMessage.includes('failed to decode') && errorMessage.includes('block.number') && errorMessage.includes('has only indexed up to block number') ) { await sleep(3000) return await this._query(url, operationName, query, variables) as T } throw new Error(errorMessage) } return response.data } }