Skip to content

Commit 610eb5a

Browse files
refactor(thegraph)!: simpler getTheGraphClient interface (#1621)
Co-authored-by: MantisClone <[email protected]>
1 parent 0ebcb25 commit 610eb5a

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed

packages/payment-detection/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @requestnetwork/payment-detection
22

3-
`@requestnetwork/payment-detection` is a typescript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork).
3+
`@requestnetwork/payment-detection` is a TypeScript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork).
44
It contains the implementation of request-related events interpretations, typically onchain payments of requests.
55

66
The interpretation of events is specified by the payment extension added to the request. [Cf. advanced-logic specifications](../advanced-logic/specs/).
@@ -57,16 +57,17 @@ export abstract class PaymentDetectorBase<
5757

5858
For TheGraph-based information retrieval, a client can be retrieved using `getTheGraphClient()` in `./src/thegraph/index.ts`. It provides a strongly typed interface, generated based on the queries in `/src/thegraph/queries`.
5959

60-
The automated type generation is configured within files `./codegen.yml` (for EVM chains) and `./codegen-near.yml` (for Near) and output in `./src/thegraph/generated`. It depends on the deployed subgraphes schema and on the queries.
60+
The automated type generation is configured within files `./codegen.yml` (for EVM chains) and `./codegen-near.yml` (for Near) and output in `./src/thegraph/generated`.
61+
It depends on the deployed subgraphs schema and on the queries.
6162

6263
The code generation is included in the pre-build script and can be run manually:
6364

64-
```
65+
```sh
6566
yarn codegen
6667
```
6768

6869
# Test
6970

70-
```bash
71+
```sh
7172
yarn run test
7273
```

packages/payment-detection/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import * as Erc20PaymentNetwork from './erc20';
1111
import { ERC20TransferableReceivablePaymentDetector } from './erc20';
1212
import { AnyToERC20PaymentDetector, AnyToEthFeeProxyPaymentDetector } from './any';
1313
import { EthFeeProxyPaymentDetector, EthInputDataPaymentDetector } from './eth';
14-
import { getTheGraphClient, getTheGraphEvmClient, getTheGraphNearClient } from './thegraph';
14+
import {
15+
getTheGraphClient,
16+
getTheGraphClientUrl,
17+
getTheGraphEvmClient,
18+
getTheGraphNearClient,
19+
} from './thegraph';
1520
import {
1621
calculateEscrowState,
1722
flattenRequestByPnId,
@@ -57,6 +62,7 @@ export {
5762
initPaymentDetectionApiKeys,
5863
getDefaultProvider,
5964
getTheGraphClient,
65+
getTheGraphClientUrl,
6066
getTheGraphEvmClient,
6167
getTheGraphNearClient,
6268
parseLogArgs,

packages/payment-detection/src/payment-network-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { EthFeeProxyPaymentDetector, EthInputDataPaymentDetector } from './eth';
2525
import { AnyToERC20PaymentDetector, AnyToEthFeeProxyPaymentDetector } from './any';
2626
import { NearConversionNativeTokenPaymentDetector, NearNativeTokenPaymentDetector } from './near';
2727
import { getPaymentNetworkExtension } from './utils';
28-
import { defaultGetTheGraphClient } from './thegraph';
28+
import { getTheGraphClient } from './thegraph';
2929
import { getDefaultProvider } from 'ethers';
3030

3131
const PN_ID = ExtensionTypes.PAYMENT_NETWORK_ID;
@@ -105,7 +105,7 @@ export class PaymentNetworkFactory {
105105

106106
private buildOptions(options: Partial<PaymentNetworkOptions>): PaymentNetworkOptions {
107107
const defaultOptions: PaymentNetworkOptions = {
108-
getSubgraphClient: defaultGetTheGraphClient,
108+
getSubgraphClient: getTheGraphClient,
109109
explorerApiKeys: {},
110110
getRpcProvider: getDefaultProvider,
111111
};

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export type TheGraphClientOptions = RequestConfig & {
8383
minIndexedBlock?: number | undefined;
8484
/** API key for accessing subgraphs hosted on TheGraph Explorer */
8585
theGraphExplorerApiKey?: string;
86+
/** URL to access the subgraph. Using this option will ignore theGraphExplorerApiKey */
87+
url?: string;
8688
};
8789

8890
/** Splits the input options into "client options" to pass to the SDK, and "query options" to use in queries */
@@ -112,10 +114,16 @@ const extractClientOptions = (
112114
return [clientOptions, queryOptions];
113115
};
114116

115-
export const getTheGraphClient = (network: string, url: string, options?: TheGraphClientOptions) =>
116-
NearChains.isChainSupported(network)
117+
export const getTheGraphClient = (
118+
network: CurrencyTypes.ChainName,
119+
options?: TheGraphClientOptions,
120+
) => {
121+
const url = getTheGraphClientUrl(network, options);
122+
if (!url) return;
123+
return NearChains.isChainSupported(network)
117124
? getTheGraphNearClient(url, options)
118125
: getTheGraphEvmClient(url, options);
126+
};
119127

120128
export const getTheGraphEvmClient = (url: string, options?: TheGraphClientOptions) => {
121129
const [clientOptions, queryOptions] = extractClientOptions(url, options);
@@ -135,10 +143,12 @@ export const getTheGraphNearClient = (url: string, options?: TheGraphClientOptio
135143
return sdk;
136144
};
137145

138-
export const defaultGetTheGraphClientUrl = (
146+
export const getTheGraphClientUrl = (
139147
network: CurrencyTypes.ChainName,
140148
options?: TheGraphClientOptions,
141149
) => {
150+
if (options?.url) return options.url;
151+
142152
const chain = network.replace('aurora', 'near') as CurrencyTypes.ChainName;
143153
const theGraphExplorerSubgraphId = THE_GRAPH_EXPLORER_SUBGRAPH_ID[chain];
144154
const { theGraphExplorerApiKey } = options || {};
@@ -171,13 +181,3 @@ export const defaultGetTheGraphClientUrl = (
171181
: theGraphStudioUrl;
172182
}
173183
};
174-
175-
export const defaultGetTheGraphClient = (
176-
network: CurrencyTypes.ChainName,
177-
options?: TheGraphClientOptions,
178-
) => {
179-
const url = defaultGetTheGraphClientUrl(network, options);
180-
if (!url) return;
181-
if (NearChains.isChainSupported(network)) return getTheGraphNearClient(url, options);
182-
return getTheGraphEvmClient(url, options);
183-
};

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
1-
import { defaultGetTheGraphClientUrl } from '../../src/thegraph';
1+
import { getTheGraphClientUrl } from '../../src/thegraph';
22

3-
describe('defaultGetTheGraphClientUrl', () => {
3+
describe('getTheGraphClientUrl', () => {
4+
it('should use the URL passed as option if any', () => {
5+
const url = getTheGraphClientUrl('base', { url: 'test' });
6+
expect(url).toBe('test');
7+
});
48
it('should build the correct URL for network supported by Alchemy', () => {
5-
const url = defaultGetTheGraphClientUrl('base');
9+
const url = getTheGraphClientUrl('base');
610
expect(url).toBe(
711
'https://subgraph.satsuma-prod.com/e2e4905ab7c8/request-network--434873/request-payments-base/api',
812
);
913
});
1014
it('should build the correct URL when using TheGraph Explorer API key', () => {
11-
const url = defaultGetTheGraphClientUrl('base', { theGraphExplorerApiKey: 'test' });
15+
const url = getTheGraphClientUrl('base', { theGraphExplorerApiKey: 'test' });
1216
expect(url).toBe(
1317
'https://gateway.thegraph.com/api/test/subgraphs/id/CcTtKy6BustyyVZ5XvFD4nLnbkgMBT1vcAEJ3sAx6bRe',
1418
);
1519
});
1620
it('should build the correct URL for Mantle', () => {
17-
const url = defaultGetTheGraphClientUrl('mantle');
21+
const url = getTheGraphClientUrl('mantle');
1822
expect(url).toBe(
1923
'https://subgraph-api.mantle.xyz/api/public/555176e7-c1f4-49f9-9180-f2f03538b039/subgraphs/requestnetwork/request-payments-mantle/v0.1.0/gn',
2024
);
2125
});
2226
it('should build the correct URL for Near', () => {
23-
const urlNear = defaultGetTheGraphClientUrl('near');
27+
const urlNear = getTheGraphClientUrl('near');
2428
expect(urlNear).toBe(
2529
'https://api.studio.thegraph.com/query/67444/request-payments-near/version/latest',
2630
);
27-
const urlNearTestnet = defaultGetTheGraphClientUrl('near-testnet');
31+
const urlNearTestnet = getTheGraphClientUrl('near-testnet');
2832
expect(urlNearTestnet).toBe(
2933
'https://api.studio.thegraph.com/query/67444/request-payments-near-testnet/version/latest',
3034
);
31-
const urlAurora = defaultGetTheGraphClientUrl('aurora');
35+
const urlAurora = getTheGraphClientUrl('aurora');
3236
expect(urlAurora).toBe(
3337
'https://api.studio.thegraph.com/query/67444/request-payments-near/version/latest',
3438
);
35-
const urlAuroraTestnet = defaultGetTheGraphClientUrl('aurora-testnet');
39+
const urlAuroraTestnet = getTheGraphClientUrl('aurora-testnet');
3640
expect(urlAuroraTestnet).toBe(
3741
'https://api.studio.thegraph.com/query/67444/request-payments-near-testnet/version/latest',
3842
);
3943
});
4044
it('should build the correct URL for Near with TheGraph Explorer API key', () => {
41-
const url = defaultGetTheGraphClientUrl('near', { theGraphExplorerApiKey: 'test' });
45+
const url = getTheGraphClientUrl('near', { theGraphExplorerApiKey: 'test' });
4246
expect(url).toBe(
4347
'https://gateway.thegraph.com/api/test/subgraphs/id/9yEg3h46CZiv4VuSqo1erMMBx5sHxRuW5Ai2V8goSpQL',
4448
);

0 commit comments

Comments
 (0)