@@ -8,6 +8,9 @@ import { getSdk as getNearSdk } from './generated/graphql-near';
88const THE_GRAPH_STUDIO_URL =
99 'https://api.studio.thegraph.com/query/67444/request-payments-$NETWORK/version/latest' ;
1010
11+ const THE_GRAPH_EXPLORER_URL =
12+ 'https://gateway.thegraph.com/api/$API_KEY/subgraphs/id/$SUBGRAPH_ID' ;
13+
1114const THE_GRAPH_ALCHEMY_URL =
1215 'https://subgraph.satsuma-prod.com/e2e4905ab7c8/request-network--434873/request-payments-$NETWORK/api' ;
1316
@@ -20,6 +23,39 @@ const THE_GRAPH_URL_MANTLE =
2023const THE_GRAPH_URL_CORE =
2124 'https://thegraph.coredao.org/subgraphs/name/requestnetwork/request-payments-core' ;
2225
26+ const THE_GRAPH_ALCHEMY_CHAINS : CurrencyTypes . ChainName [ ] = [
27+ 'arbitrum-one' ,
28+ 'avalanche' ,
29+ 'base' ,
30+ 'bsc' ,
31+ 'fantom' ,
32+ 'mainnet' ,
33+ 'matic' ,
34+ 'sepolia' ,
35+ 'optimism' ,
36+ 'zksyncera' ,
37+ ] ;
38+
39+ const THE_GRAPH_EXPLORER_SUBGRAPH_ID : Partial < Record < CurrencyTypes . ChainName , string > > = {
40+ [ 'arbitrum-one' ] : '3MtDdHbzvBVNBpzUTYXGuDDLgTd1b8bPYwoH1Hdssgp9' ,
41+ avalanche : 'A27V4PeZdKHeyuBkehdBJN8cxNtzVpXvYoqkjHUHRCFp' ,
42+ base : 'CcTtKy6BustyyVZ5XvFD4nLnbkgMBT1vcAEJ3sAx6bRe' ,
43+ bsc : '4PScFUi3CFDbop9XzT6gCDtD4RR8kRzyrzSjrHoXHZBt' ,
44+ celo : '5ts3PHjMcH2skCgKtvLLNE64WLjbhE5ipruvEcgqyZqC' ,
45+ fantom : '6AwmiYo5eY36W526ZDQeAkNBjXjXKYcMLYyYHeM67xAb' ,
46+ fuse : 'EHSpUBa7PAewX7WsaU2jbCKowF5it56yStr6Zgf8aDtx' ,
47+ mainnet : '5mXPGZRC2Caynh4NyVrTK72DAGB9dfcKmLsnxYWHQ9nd' ,
48+ matic : 'DPpU1WMxk2Z4H2TAqgwGbVBGpabjbC1972Mynak5jSuR' ,
49+ moonbeam : '4Jo3DwA25zyVLeDhyi7cks52dNrkVCWWhQJzm1hKnCfj' ,
50+ sepolia : '6e8Dcwt3cvsgNU3JYBtRQQ9Sj4P9VVVnXaPjJ3jUpYpY' ,
51+ sonic : 'CQbtmuANYsChysuXTk9jWP3BD4ArncARVVw1b8JpHiTk' ,
52+ near : '9yEg3h46CZiv4VuSqo1erMMBx5sHxRuW5Ai2V8goSpQL' ,
53+ [ 'near-testnet' ] : 'AusVyfndonsMVFrVzckuENLqx8t6kcXuxn6C6VbSGd7M' ,
54+ optimism : '525fra79nG3Z1w8aPZh3nHsH5zCVetrVmceB1hKcTrTX' ,
55+ xdai : '2UAW7B94eeeqaL5qUM5FDzTWJcmgA6ta1RcWMo3XuLmU' ,
56+ zksyncera : 'HJNZW9vRSGXrcCVyQMdNKxxuLKeZcV6yMjTCyY6T2oon' ,
57+ } ;
58+
2359// NB: the GraphQL client is automatically generated based on files present in ./queries,
2460// using graphql-codegen.
2561// To generate types, run `yarn codegen`, then open the generated files so that the code editor picks up the changes.
@@ -45,6 +81,8 @@ type RequestConfig = (typeof GraphQLClient.prototype)['requestConfig'];
4581export type TheGraphClientOptions = RequestConfig & {
4682 /** constraint to select indexers that have at least parsed this block */
4783 minIndexedBlock ?: number | undefined ;
84+ /** API key for accessing subgraphs hosted on TheGraph Explorer */
85+ theGraphExplorerApiKey ?: string ;
4886} ;
4987
5088/** Splits the input options into "client options" to pass to the SDK, and "query options" to use in queries */
@@ -56,7 +94,13 @@ const extractClientOptions = (
5694
5795 // build query options
5896 const queryOptions : TheGraphQueryOptions = { } ;
59- const { minIndexedBlock, ...clientOptions } = optionsObject ;
97+ const {
98+ minIndexedBlock,
99+ // ignore theGraphExplorerApiKey, it should not be part of clientOptions
100+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
101+ theGraphExplorerApiKey : _theGraphExplorerApiKey ,
102+ ...clientOptions
103+ } = optionsObject ;
60104 if ( minIndexedBlock ) {
61105 queryOptions . blockFilter = { number_gte : minIndexedBlock } ;
62106 } else if ( url . match ( / ^ h t t p s : \/ \/ g a t e w a y - \w + \. n e t w o r k \. t h e g r a p h \. c o m \/ / ) ) {
@@ -91,33 +135,49 @@ export const getTheGraphNearClient = (url: string, options?: TheGraphClientOptio
91135 return sdk ;
92136} ;
93137
138+ export const defaultGetTheGraphClientUrl = (
139+ network : CurrencyTypes . ChainName ,
140+ options ?: TheGraphClientOptions ,
141+ ) => {
142+ const chain = network . replace ( 'aurora' , 'near' ) as CurrencyTypes . ChainName ;
143+ const theGraphExplorerSubgraphId = THE_GRAPH_EXPLORER_SUBGRAPH_ID [ chain ] ;
144+ const { theGraphExplorerApiKey } = options || { } ;
145+
146+ // build URLs
147+ const theGraphStudioUrl = THE_GRAPH_STUDIO_URL . replace ( '$NETWORK' , chain ) ;
148+ const theGraphExplorerUrl = THE_GRAPH_EXPLORER_URL . replace (
149+ '$API_KEY' ,
150+ theGraphExplorerApiKey || '' ,
151+ ) . replace ( '$SUBGRAPH_ID' , theGraphExplorerSubgraphId || '' ) ;
152+ const theGraphAlchemyUrl = THE_GRAPH_ALCHEMY_URL . replace ( '$NETWORK' , chain ) ;
153+
154+ const shouldUseTheGraphExplorer = ! ! theGraphExplorerApiKey && ! ! theGraphExplorerSubgraphId ;
155+ const shouldUseAlchemy = THE_GRAPH_ALCHEMY_CHAINS . includes ( chain ) ;
156+
157+ switch ( true ) {
158+ case chain === 'private' :
159+ return ;
160+ case chain === 'mantle' :
161+ return THE_GRAPH_URL_MANTLE ;
162+ case chain === 'mantle-testnet' :
163+ return THE_GRAPH_URL_MANTLE_TESTNET ;
164+ case chain === 'core' :
165+ return THE_GRAPH_URL_CORE ;
166+ default :
167+ return shouldUseTheGraphExplorer
168+ ? theGraphExplorerUrl
169+ : shouldUseAlchemy
170+ ? theGraphAlchemyUrl
171+ : theGraphStudioUrl ;
172+ }
173+ } ;
174+
94175export const defaultGetTheGraphClient = (
95176 network : CurrencyTypes . ChainName ,
96177 options ?: TheGraphClientOptions ,
97178) => {
98- return network === 'private'
99- ? undefined
100- : NearChains . isChainSupported ( network )
101- ? getTheGraphNearClient (
102- `${ THE_GRAPH_STUDIO_URL . replace ( '$NETWORK' , network . replace ( 'aurora' , 'near' ) ) } ` ,
103- options ,
104- )
105- : network === 'mantle'
106- ? getTheGraphEvmClient ( THE_GRAPH_URL_MANTLE , options )
107- : network === 'mantle-testnet'
108- ? getTheGraphEvmClient ( THE_GRAPH_URL_MANTLE_TESTNET , options )
109- : network === 'core'
110- ? getTheGraphEvmClient ( THE_GRAPH_URL_CORE , options )
111- : network === 'mainnet' ||
112- network === 'sepolia' ||
113- network === 'matic' ||
114- network === 'bsc' ||
115- network === 'optimism' ||
116- network === 'arbitrum-one' ||
117- network === 'base' ||
118- network === 'zksyncera' ||
119- network === 'avalanche' ||
120- network === 'fantom'
121- ? getTheGraphEvmClient ( `${ THE_GRAPH_ALCHEMY_URL . replace ( '$NETWORK' , network ) } ` , options )
122- : getTheGraphEvmClient ( `${ THE_GRAPH_STUDIO_URL . replace ( '$NETWORK' , network ) } ` , options ) ;
179+ const url = defaultGetTheGraphClientUrl ( network , options ) ;
180+ if ( ! url ) return ;
181+ if ( NearChains . isChainSupported ( network ) ) return getTheGraphNearClient ( url , options ) ;
182+ return getTheGraphEvmClient ( url , options ) ;
123183} ;
0 commit comments