diff --git a/packages/payment-detection/README.md b/packages/payment-detection/README.md index 501572fb34..4544df8ffd 100644 --- a/packages/payment-detection/README.md +++ b/packages/payment-detection/README.md @@ -1,6 +1,6 @@ # @requestnetwork/payment-detection -`@requestnetwork/payment-detection` is a typescript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork). +`@requestnetwork/payment-detection` is a TypeScript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork). It contains the implementation of request-related events interpretations, typically onchain payments of requests. 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< 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`. -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. +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 subgraphs schema and on the queries. The code generation is included in the pre-build script and can be run manually: -``` +```sh yarn codegen ``` # Test -```bash +```sh yarn run test ``` diff --git a/packages/payment-detection/src/index.ts b/packages/payment-detection/src/index.ts index 53ae786748..42227f874d 100644 --- a/packages/payment-detection/src/index.ts +++ b/packages/payment-detection/src/index.ts @@ -11,7 +11,12 @@ import * as Erc20PaymentNetwork from './erc20'; import { ERC20TransferableReceivablePaymentDetector } from './erc20'; import { AnyToERC20PaymentDetector, AnyToEthFeeProxyPaymentDetector } from './any'; import { EthFeeProxyPaymentDetector, EthInputDataPaymentDetector } from './eth'; -import { getTheGraphClient, getTheGraphEvmClient, getTheGraphNearClient } from './thegraph'; +import { + getTheGraphClient, + getTheGraphClientUrl, + getTheGraphEvmClient, + getTheGraphNearClient, +} from './thegraph'; import { calculateEscrowState, flattenRequestByPnId, @@ -57,6 +62,7 @@ export { initPaymentDetectionApiKeys, getDefaultProvider, getTheGraphClient, + getTheGraphClientUrl, getTheGraphEvmClient, getTheGraphNearClient, parseLogArgs, diff --git a/packages/payment-detection/src/payment-network-factory.ts b/packages/payment-detection/src/payment-network-factory.ts index fc2d4d505d..b88a2689d9 100644 --- a/packages/payment-detection/src/payment-network-factory.ts +++ b/packages/payment-detection/src/payment-network-factory.ts @@ -25,7 +25,7 @@ import { EthFeeProxyPaymentDetector, EthInputDataPaymentDetector } from './eth'; import { AnyToERC20PaymentDetector, AnyToEthFeeProxyPaymentDetector } from './any'; import { NearConversionNativeTokenPaymentDetector, NearNativeTokenPaymentDetector } from './near'; import { getPaymentNetworkExtension } from './utils'; -import { defaultGetTheGraphClient } from './thegraph'; +import { getTheGraphClient } from './thegraph'; import { getDefaultProvider } from 'ethers'; const PN_ID = ExtensionTypes.PAYMENT_NETWORK_ID; @@ -105,7 +105,7 @@ export class PaymentNetworkFactory { private buildOptions(options: Partial): PaymentNetworkOptions { const defaultOptions: PaymentNetworkOptions = { - getSubgraphClient: defaultGetTheGraphClient, + getSubgraphClient: getTheGraphClient, explorerApiKeys: {}, getRpcProvider: getDefaultProvider, }; diff --git a/packages/payment-detection/src/thegraph/client.ts b/packages/payment-detection/src/thegraph/client.ts index d3f7010864..27acbb72a9 100644 --- a/packages/payment-detection/src/thegraph/client.ts +++ b/packages/payment-detection/src/thegraph/client.ts @@ -83,6 +83,8 @@ export type TheGraphClientOptions = RequestConfig & { minIndexedBlock?: number | undefined; /** API key for accessing subgraphs hosted on TheGraph Explorer */ theGraphExplorerApiKey?: string; + /** URL to access the subgraph. Using this option will ignore theGraphExplorerApiKey */ + url?: string; }; /** 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 = ( return [clientOptions, queryOptions]; }; -export const getTheGraphClient = (network: string, url: string, options?: TheGraphClientOptions) => - NearChains.isChainSupported(network) +export const getTheGraphClient = ( + network: CurrencyTypes.ChainName, + options?: TheGraphClientOptions, +) => { + const url = getTheGraphClientUrl(network, options); + if (!url) return; + return NearChains.isChainSupported(network) ? getTheGraphNearClient(url, options) : getTheGraphEvmClient(url, options); +}; export const getTheGraphEvmClient = (url: string, options?: TheGraphClientOptions) => { const [clientOptions, queryOptions] = extractClientOptions(url, options); @@ -135,10 +143,12 @@ export const getTheGraphNearClient = (url: string, options?: TheGraphClientOptio return sdk; }; -export const defaultGetTheGraphClientUrl = ( +export const getTheGraphClientUrl = ( network: CurrencyTypes.ChainName, options?: TheGraphClientOptions, ) => { + if (options?.url) return options.url; + const chain = network.replace('aurora', 'near') as CurrencyTypes.ChainName; const theGraphExplorerSubgraphId = THE_GRAPH_EXPLORER_SUBGRAPH_ID[chain]; const { theGraphExplorerApiKey } = options || {}; @@ -171,13 +181,3 @@ export const defaultGetTheGraphClientUrl = ( : theGraphStudioUrl; } }; - -export const defaultGetTheGraphClient = ( - network: CurrencyTypes.ChainName, - options?: TheGraphClientOptions, -) => { - const url = defaultGetTheGraphClientUrl(network, options); - if (!url) return; - if (NearChains.isChainSupported(network)) return getTheGraphNearClient(url, options); - return getTheGraphEvmClient(url, options); -}; diff --git a/packages/payment-detection/test/thegraph/client.test.ts b/packages/payment-detection/test/thegraph/client.test.ts index d0042ecdef..d8cccc4d76 100644 --- a/packages/payment-detection/test/thegraph/client.test.ts +++ b/packages/payment-detection/test/thegraph/client.test.ts @@ -1,44 +1,48 @@ -import { defaultGetTheGraphClientUrl } from '../../src/thegraph'; +import { getTheGraphClientUrl } from '../../src/thegraph'; -describe('defaultGetTheGraphClientUrl', () => { +describe('getTheGraphClientUrl', () => { + it('should use the URL passed as option if any', () => { + const url = getTheGraphClientUrl('base', { url: 'test' }); + expect(url).toBe('test'); + }); it('should build the correct URL for network supported by Alchemy', () => { - const url = defaultGetTheGraphClientUrl('base'); + const url = getTheGraphClientUrl('base'); expect(url).toBe( 'https://subgraph.satsuma-prod.com/e2e4905ab7c8/request-network--434873/request-payments-base/api', ); }); it('should build the correct URL when using TheGraph Explorer API key', () => { - const url = defaultGetTheGraphClientUrl('base', { theGraphExplorerApiKey: 'test' }); + const url = getTheGraphClientUrl('base', { theGraphExplorerApiKey: 'test' }); expect(url).toBe( 'https://gateway.thegraph.com/api/test/subgraphs/id/CcTtKy6BustyyVZ5XvFD4nLnbkgMBT1vcAEJ3sAx6bRe', ); }); it('should build the correct URL for Mantle', () => { - const url = defaultGetTheGraphClientUrl('mantle'); + const url = getTheGraphClientUrl('mantle'); expect(url).toBe( 'https://subgraph-api.mantle.xyz/api/public/555176e7-c1f4-49f9-9180-f2f03538b039/subgraphs/requestnetwork/request-payments-mantle/v0.1.0/gn', ); }); it('should build the correct URL for Near', () => { - const urlNear = defaultGetTheGraphClientUrl('near'); + const urlNear = getTheGraphClientUrl('near'); expect(urlNear).toBe( 'https://api.studio.thegraph.com/query/67444/request-payments-near/version/latest', ); - const urlNearTestnet = defaultGetTheGraphClientUrl('near-testnet'); + const urlNearTestnet = getTheGraphClientUrl('near-testnet'); expect(urlNearTestnet).toBe( 'https://api.studio.thegraph.com/query/67444/request-payments-near-testnet/version/latest', ); - const urlAurora = defaultGetTheGraphClientUrl('aurora'); + const urlAurora = getTheGraphClientUrl('aurora'); expect(urlAurora).toBe( 'https://api.studio.thegraph.com/query/67444/request-payments-near/version/latest', ); - const urlAuroraTestnet = defaultGetTheGraphClientUrl('aurora-testnet'); + const urlAuroraTestnet = getTheGraphClientUrl('aurora-testnet'); expect(urlAuroraTestnet).toBe( 'https://api.studio.thegraph.com/query/67444/request-payments-near-testnet/version/latest', ); }); it('should build the correct URL for Near with TheGraph Explorer API key', () => { - const url = defaultGetTheGraphClientUrl('near', { theGraphExplorerApiKey: 'test' }); + const url = getTheGraphClientUrl('near', { theGraphExplorerApiKey: 'test' }); expect(url).toBe( 'https://gateway.thegraph.com/api/test/subgraphs/id/9yEg3h46CZiv4VuSqo1erMMBx5sHxRuW5Ai2V8goSpQL', );