Skip to content

Commit cd24ea8

Browse files
Merge branch 'master' into moduleresolution
2 parents 7960f48 + b09c9b8 commit cd24ea8

File tree

4 files changed

+134
-27
lines changed

4 files changed

+134
-27
lines changed

.github/auto_assign.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ reviewers:
1313
- MantisClone
1414
- aimensahnoun
1515
- rodrigopavezi
16+
- bassgeta
1617

1718
# A list of keywords to be skipped the process that add reviewers if pull requests include it
1819
skipKeywords:

packages/data-format/src/format/rnf_invoice/rnf_invoice-0.0.3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
},
125125
"unitPrice": {
126126
"type": "string",
127-
"pattern": "^\\d+$"
127+
"pattern": "^-?\\d+$"
128128
},
129129
"discount": {
130130
"type": "string",

packages/payment-detection/src/thegraph/client.ts

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { getSdk as getNearSdk } from './generated/graphql-near';
88
const 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+
1114
const 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 =
2023
const 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'];
4581
export 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(/^https:\/\/gateway-\w+\.network\.thegraph\.com\//)) {
@@ -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+
94175
export 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
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { defaultGetTheGraphClientUrl } from '../../src/thegraph';
2+
3+
describe('defaultGetTheGraphClientUrl', () => {
4+
it('should build the correct URL for network supported by Alchemy', () => {
5+
const url = defaultGetTheGraphClientUrl('base');
6+
expect(url).toBe(
7+
'https://subgraph.satsuma-prod.com/e2e4905ab7c8/request-network--434873/request-payments-base/api',
8+
);
9+
});
10+
it('should build the correct URL when using TheGraph Explorer API key', () => {
11+
const url = defaultGetTheGraphClientUrl('base', { theGraphExplorerApiKey: 'test' });
12+
expect(url).toBe(
13+
'https://gateway.thegraph.com/api/test/subgraphs/id/CcTtKy6BustyyVZ5XvFD4nLnbkgMBT1vcAEJ3sAx6bRe',
14+
);
15+
});
16+
it('should build the correct URL for Mantle', () => {
17+
const url = defaultGetTheGraphClientUrl('mantle');
18+
expect(url).toBe(
19+
'https://subgraph-api.mantle.xyz/api/public/555176e7-c1f4-49f9-9180-f2f03538b039/subgraphs/requestnetwork/request-payments-mantle/v0.1.0/gn',
20+
);
21+
});
22+
it('should build the correct URL for Near', () => {
23+
const urlNear = defaultGetTheGraphClientUrl('near');
24+
expect(urlNear).toBe(
25+
'https://api.studio.thegraph.com/query/67444/request-payments-near/version/latest',
26+
);
27+
const urlNearTestnet = defaultGetTheGraphClientUrl('near-testnet');
28+
expect(urlNearTestnet).toBe(
29+
'https://api.studio.thegraph.com/query/67444/request-payments-near-testnet/version/latest',
30+
);
31+
const urlAurora = defaultGetTheGraphClientUrl('aurora');
32+
expect(urlAurora).toBe(
33+
'https://api.studio.thegraph.com/query/67444/request-payments-near/version/latest',
34+
);
35+
const urlAuroraTestnet = defaultGetTheGraphClientUrl('aurora-testnet');
36+
expect(urlAuroraTestnet).toBe(
37+
'https://api.studio.thegraph.com/query/67444/request-payments-near-testnet/version/latest',
38+
);
39+
});
40+
it('should build the correct URL for Near with TheGraph Explorer API key', () => {
41+
const url = defaultGetTheGraphClientUrl('near', { theGraphExplorerApiKey: 'test' });
42+
expect(url).toBe(
43+
'https://gateway.thegraph.com/api/test/subgraphs/id/9yEg3h46CZiv4VuSqo1erMMBx5sHxRuW5Ai2V8goSpQL',
44+
);
45+
});
46+
});

0 commit comments

Comments
 (0)