Skip to content

Commit

Permalink
refactor: use chains/subgraphs define
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefJerry committed Dec 18, 2023
1 parent 1d70f56 commit ab34601
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 62 deletions.
2 changes: 1 addition & 1 deletion apis/farms/.dev.vars
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ ARBITRUM_ONE_NODE=https://arb1.arbitrum.io/rpc
LINEA_NODE=https://rpc.linea.build
BASE_NODE=https://mainnet.base.org
OPBNB_NODE=https://opbnb-mainnet-rpc.bnbchain.org
OPBNB_TESTNET_NODE=https://opbnb-testnet-rpc.bnbchain.org
OPBNB_TESTNET_NODE=https://opbnb-testnet-rpc.bnbchain.org
84 changes: 24 additions & 60 deletions apis/farms/src/v3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-param-reassign, no-await-in-loop */
import { ChainId } from '@pancakeswap/chains'
import { ChainId, V3_SUBGRAPHS } from '@pancakeswap/chains'
import { FarmV3SupportedChainId, masterChefV3Addresses } from '@pancakeswap/farms'
import { ERC20Token } from '@pancakeswap/sdk'
import { CurrencyAmount } from '@pancakeswap/swap-sdk-core'
Expand All @@ -13,64 +13,28 @@ import { z } from 'zod'
import { FarmKV } from './kv'
import { viemProviders } from './provider'

export const V3_SUBGRAPH_CLIENTS = {
[ChainId.ETHEREUM]: new GraphQLClient('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-eth', {
fetch,
}),
[ChainId.GOERLI]: new GraphQLClient('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-goerli', {
fetch,
}),
[ChainId.BSC]: new GraphQLClient('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-bsc', { fetch }),
[ChainId.BSC_TESTNET]: new GraphQLClient('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-chapel', {
fetch,
}),
[ChainId.ZKSYNC_TESTNET]: new GraphQLClient(
'https://api.studio.thegraph.com/query/45376/exchange-v3-zksync-testnet/version/latest',
{
fetch,
},
),
[ChainId.POLYGON_ZKEVM]: new GraphQLClient(
'https://api.studio.thegraph.com/query/45376/exchange-v3-polygon-zkevm/version/latest',
{
fetch,
},
),
[ChainId.ZKSYNC]: new GraphQLClient('https://api.studio.thegraph.com/query/45376/exchange-v3-zksync/version/latest', {
fetch,
}),
[ChainId.ARBITRUM_ONE]: new GraphQLClient('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-arb', {
fetch,
}),
[ChainId.LINEA]: new GraphQLClient('https://graph-query.linea.build/subgraphs/name/pancakeswap/exchange-v3-linea', {
fetch,
}),
[ChainId.BASE]: new GraphQLClient('https://api.studio.thegraph.com/query/45376/exchange-v3-base/version/latest', {
fetch,
}),
// @todo @Chef-Jerry add opbnb subgraph
[ChainId.OPBNB]: new GraphQLClient('https://opbnb-mainnet-graph.nodereal.io/subgraphs/name/pancakeswap/exchange-v3', {
fetch,
}),
} satisfies Record<
Exclude<FarmV3SupportedChainId, ChainId.POLYGON_ZKEVM_TESTNET | ChainId.OPBNB_TESTNET>,
GraphQLClient
>

const zChainId = z.enum([
String(ChainId.BSC),
String(ChainId.ETHEREUM),
String(ChainId.GOERLI),
String(ChainId.BSC_TESTNET),
String(ChainId.ZKSYNC_TESTNET),
String(ChainId.POLYGON_ZKEVM),
String(ChainId.ZKSYNC),
String(ChainId.ARBITRUM_ONE),
String(ChainId.LINEA),
String(ChainId.BASE),
String(ChainId.OPBNB),
String(ChainId.OPBNB_TESTNET),
])
export const V3_SUBGRAPH_CLIENTS_CHAIN_IDS = [
ChainId.ETHEREUM,
ChainId.GOERLI,
ChainId.BSC,
ChainId.BSC_TESTNET,
ChainId.ZKSYNC_TESTNET,
ChainId.POLYGON_ZKEVM,
ChainId.ZKSYNC,
ChainId.ARBITRUM_ONE,
ChainId.LINEA,
ChainId.BASE,
ChainId.OPBNB,
] as const

type SupportChainId = (typeof V3_SUBGRAPH_CLIENTS_CHAIN_IDS)[number]

export const V3_SUBGRAPH_CLIENTS = V3_SUBGRAPH_CLIENTS_CHAIN_IDS.reduce((acc, chainId) => {
acc[chainId] = new GraphQLClient(V3_SUBGRAPHS[chainId], { fetch })
return acc
}, {} as Record<Exclude<FarmV3SupportedChainId, ChainId.POLYGON_ZKEVM_TESTNET | ChainId.OPBNB_TESTNET>, GraphQLClient>)

const zChainId = z.enum(V3_SUBGRAPH_CLIENTS_CHAIN_IDS.map((chainId) => String(chainId)) as [string, ...string[]])

const zAddress = z.string().regex(/^0x[a-fA-F0-9]{40}$/)

Expand Down Expand Up @@ -182,7 +146,7 @@ const handler_ = async (req: Request, event: FetchEvent) => {
}

const { chainId: chainIdString, address: address_ } = parsed.data
const chainId = Number(chainIdString) as Exclude<FarmV3SupportedChainId, ChainId.POLYGON_ZKEVM_TESTNET>
const chainId = Number(chainIdString) as SupportChainId

const address = address_.toLowerCase()

Expand Down
2 changes: 1 addition & 1 deletion packages/chains/src/subgraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const V3_SUBGRAPHS = {
[ChainId.BSC_TESTNET]: 'https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-chapel',
[ChainId.ARBITRUM_ONE]: 'https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-arb',
[ChainId.ARBITRUM_GOERLI]: 'https://api.thegraph.com/subgraphs/name/chef-jojo/exhange-v3-arb-goerli',
[ChainId.POLYGON_ZKEVM]: 'https://api.studio.thegraph.com/query/45376/exchange-v3-polygon-zkevm/v0.0.0',
[ChainId.POLYGON_ZKEVM]: 'https://api.studio.thegraph.com/query/45376/exchange-v3-polygon-zkevm/version/latest',
[ChainId.POLYGON_ZKEVM_TESTNET]: null,
[ChainId.ZKSYNC]: 'https://api.studio.thegraph.com/query/45376/exchange-v3-zksync/version/latest',
[ChainId.ZKSYNC_TESTNET]: 'https://api.studio.thegraph.com/query/45376/exchange-v3-zksync-testnet/version/latest',
Expand Down

0 comments on commit ab34601

Please sign in to comment.