Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
support optimism and arbitrum subgraphs (#2001)
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Domingue authored Jul 12, 2021
1 parent 380ca11 commit 52790ac
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/state/data/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { DocumentNode } from 'graphql'
import { ClientError, gql, GraphQLClient } from 'graphql-request'
import { AppState } from 'state'

const UNISWAP_V3_GRAPH_URL = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3'
// List of supported subgraphs. Note that the app currently only support one active subgraph at a time
const CHAIN_SUBGRAPH_URL: Record<number, string> = {
[SupportedChainId.MAINNET]: 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3',
[SupportedChainId.ARBITRUM_ONE]: 'https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-arbitrum-one',
[SupportedChainId.OPTIMISM]: 'https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-optimism',
}

export const api = createApi({
reducerPath: 'dataApi',
Expand Down Expand Up @@ -78,23 +83,19 @@ function graphqlRequestBaseQuery(): BaseQueryFn<
try {
const chainId = (getState() as AppState).application.chainId

let client: GraphQLClient | null = null
const subgraphUrl = chainId ? CHAIN_SUBGRAPH_URL[chainId] : undefined

switch (chainId) {
case SupportedChainId.MAINNET:
client = new GraphQLClient(UNISWAP_V3_GRAPH_URL)
break
default:
return {
error: {
name: 'UnsupportedChainId',
message: `Subgraph queries again ChainId ${chainId} are not supported.`,
stack: '',
},
}
if (!subgraphUrl) {
return {
error: {
name: 'UnsupportedChainId',
message: `Subgraph queries against ChainId ${chainId} are not supported.`,
stack: '',
},
}
}

return { data: await client.request(document, variables), meta: {} }
return { data: await new GraphQLClient(subgraphUrl).request(document, variables), meta: {} }
} catch (error) {
if (error instanceof ClientError) {
const { name, message, stack, request, response } = error
Expand Down

0 comments on commit 52790ac

Please sign in to comment.