Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/payment-detection/README.md
Original file line number Diff line number Diff line change
@@ -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/).
Expand Down Expand Up @@ -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
```
8 changes: 7 additions & 1 deletion packages/payment-detection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { DeclarativePaymentDetector } from './declarative';
import * as Erc20PaymentNetwork 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,
formatAddress,
Expand Down Expand Up @@ -57,6 +62,7 @@ export {
initPaymentDetectionApiKeys,
getDefaultProvider,
getTheGraphClient,
getTheGraphClientUrl,
getTheGraphEvmClient,
getTheGraphNearClient,
parseLogArgs,
Expand Down
4 changes: 2 additions & 2 deletions packages/payment-detection/src/payment-network-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,7 +105,7 @@ export class PaymentNetworkFactory {

private buildOptions(options: Partial<PaymentNetworkOptions>): PaymentNetworkOptions {
const defaultOptions: PaymentNetworkOptions = {
getSubgraphClient: defaultGetTheGraphClient,
getSubgraphClient: getTheGraphClient,
explorerApiKeys: {},
getRpcProvider: getDefaultProvider,
};
Expand Down
26 changes: 13 additions & 13 deletions packages/payment-detection/src/thegraph/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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 */
subgraphUrl?: string;
};

/** Splits the input options into "client options" to pass to the SDK, and "query options" to use in queries */
Expand Down Expand Up @@ -111,10 +113,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);
Expand All @@ -134,10 +142,12 @@ export const getTheGraphNearClient = (url: string, options?: TheGraphClientOptio
return sdk;
};

export const defaultGetTheGraphClientUrl = (
export const getTheGraphClientUrl = (
network: CurrencyTypes.ChainName,
options?: TheGraphClientOptions,
) => {
if (options?.subgraphUrl) return options.subgraphUrl;

const chain = network.replace('aurora', 'near') as CurrencyTypes.ChainName;
const theGraphExplorerSubgraphId = THE_GRAPH_EXPLORER_SUBGRAPH_ID[chain];
const { theGraphExplorerApiKey } = options || {};
Expand Down Expand Up @@ -170,13 +180,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);
};
20 changes: 10 additions & 10 deletions packages/payment-detection/test/thegraph/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import { defaultGetTheGraphClientUrl } from '../../src/thegraph';
import { getTheGraphClientUrl } from '../../src/thegraph';

describe('defaultGetTheGraphClientUrl', () => {
describe('getTheGraphClientUrl', () => {
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',
);
Expand Down