Skip to content

Commit

Permalink
proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque authored and alexandre-abrioux committed Nov 28, 2023
1 parent 77be643 commit c1ab567
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
44 changes: 24 additions & 20 deletions packages/payment-detection/src/thegraph/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import { CurrencyTypes } from '@requestnetwork/types';
import { NearChains } from '@requestnetwork/currency';
import { GraphQLClient } from 'graphql-request';
import { getSdk } from './generated/graphql';
import { Block_Height, Maybe, getSdk } from './generated/graphql';
import { getSdk as getNearSdk } from './generated/graphql-near';
import { pick } from 'lodash';

const HOSTED_THE_GRAPH_URL =
'https://api.thegraph.com/subgraphs/name/requestnetwork/request-payments-';
Expand All @@ -31,30 +30,33 @@ export type TheGraphClient<TChain extends CurrencyTypes.VMChainName = CurrencyTy
(TChain extends CurrencyTypes.NearChainName
? ReturnType<typeof getNearSdk>
: ReturnType<typeof getSdk>) & {
options?: TheGraphClientOptions;
options?: TheGraphQueryOptions;
};

export type TheGraphQueryOptions = {
blockFilter?: Maybe<Block_Height>;
};

export type TheGraphClientOptions = {
timeout?: number;
/** constraint to select indexers that have at least parsed this block */
minIndexedBlock?: number | undefined;
/** is this client targeting TheGraph decentralized network? */
decentralizedNetwork?: boolean;
};

const extractClientOptions = (options?: TheGraphClientOptions) => {
return pick(options, 'timeout');
};

const populateSdkWithOptions = (
sdk: TheGraphClient<any>,
/** Splits the input options into "client options" to pass to the SDK, and "query options" to use in queries */
const extractClientOptions = (
url: string,
options?: TheGraphClientOptions,
) => {
sdk.options = {
decentralizedNetwork: url.startsWith('https://gateway-arbitrum.network.thegraph.com/'),
...options,
};
): [Pick<TheGraphClientOptions, 'timeout'>, TheGraphQueryOptions] => {
const { minIndexedBlock, timeout } = options ?? {};
const queryOptions: TheGraphQueryOptions = {};
if (minIndexedBlock) {
queryOptions.blockFilter = { number_gte: minIndexedBlock };
} else if (url.match(/https:\/\/gateway-\w.network.thegraph.com/)) {
// the decentralized network expects an empty object, and doesn't support "undefined"
queryOptions.blockFilter = {};
}
return [{ timeout }, queryOptions];
};

export const getTheGraphClient = (network: string, url: string, options?: TheGraphClientOptions) =>
Expand All @@ -63,18 +65,20 @@ export const getTheGraphClient = (network: string, url: string, options?: TheGra
: getTheGraphEvmClient(url, options);

export const getTheGraphEvmClient = (url: string, options?: TheGraphClientOptions) => {
const [clientOptions, queryOptions] = extractClientOptions(url, options);
const sdk: TheGraphClient<CurrencyTypes.EvmChainName> = getSdk(
new GraphQLClient(url, extractClientOptions(options)),
new GraphQLClient(url, clientOptions),
);
populateSdkWithOptions(sdk, url, options);
sdk.options = queryOptions;
return sdk;
};

export const getTheGraphNearClient = (url: string, options?: TheGraphClientOptions) => {
const [clientOptions, queryOptions] = extractClientOptions(url, options);
const sdk: TheGraphClient<CurrencyTypes.NearChainName> = getNearSdk(
new GraphQLClient(url, extractClientOptions(options)),
new GraphQLClient(url, clientOptions),
);
populateSdkWithOptions(sdk, url, options);
sdk.options = queryOptions;
return sdk;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever<Conve
): Promise<PaymentTypes.AllNetworkEvents<PaymentTypes.IERC20FeePaymentEventParameters>> {
const { payments } = params.acceptedTokens
? await this.client.GetAnyToFungiblePayments({
blockFilter: this.client.options?.minIndexedBlock
? { number_gte: this.client.options.minIndexedBlock }
: this.client.options?.decentralizedNetwork
? {}
: undefined,
blockFilter: this.client.options?.blockFilter,
reference: utils.keccak256(`0x${params.paymentReference}`),
to: params.toAddress.toLowerCase(),
currency: params.requestCurrency.hash.toLowerCase(),
acceptedTokens: params.acceptedTokens.map((t) => t.toLowerCase()),
contractAddress: params.contractAddress.toLowerCase(),
})
: await this.client.GetAnyToNativePayments({
blockFilter: this.client.options?.minIndexedBlock
? { number_gte: this.client.options.minIndexedBlock }
: this.client.options?.decentralizedNetwork
? {}
: undefined,
blockFilter: this.client.options?.blockFilter,
reference: utils.keccak256(`0x${params.paymentReference}`),
to: params.toAddress.toLowerCase(),
currency: params.requestCurrency.hash.toLowerCase(),
Expand Down
12 changes: 2 additions & 10 deletions packages/payment-detection/src/thegraph/info-retriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export class TheGraphInfoRetriever<TGraphQuery extends TransferEventsParams = Tr
throw new Error('TheGraphInfoRetriever only supports no or 1 acceptedToken.');
}
const { payments, escrowEvents } = await this.client.GetPaymentsAndEscrowState({
blockFilter: this.client.options?.minIndexedBlock
? { number_gte: this.client.options.minIndexedBlock }
: this.client.options?.decentralizedNetwork
? {}
: undefined,
blockFilter: this.client.options?.blockFilter,
reference: utils.keccak256(`0x${params.paymentReference}`),
to: params.toAddress.toLowerCase(),
tokenAddress: params.acceptedTokens ? params.acceptedTokens[0].toLowerCase() : null,
Expand All @@ -50,11 +46,7 @@ export class TheGraphInfoRetriever<TGraphQuery extends TransferEventsParams = Tr
throw new Error('TheGraphInfoRetriever only supports no or 1 acceptedToken.');
}
const { payments, escrowEvents } = await this.client.GetPaymentsAndEscrowStateForReceivables({
blockFilter: this.client.options?.minIndexedBlock
? { number_gte: this.client.options.minIndexedBlock }
: this.client.options?.decentralizedNetwork
? {}
: undefined,
blockFilter: this.client.options?.blockFilter,
reference: utils.keccak256(`0x${params.paymentReference}`),
tokenAddress: params.acceptedTokens ? params.acceptedTokens[0].toLowerCase() : null,
contractAddress: params.contractAddress.toLowerCase(),
Expand Down

0 comments on commit c1ab567

Please sign in to comment.