diff --git a/packages/payment-detection/src/thegraph/client.ts b/packages/payment-detection/src/thegraph/client.ts index 261286d7f..b080dddf7 100644 --- a/packages/payment-detection/src/thegraph/client.ts +++ b/packages/payment-detection/src/thegraph/client.ts @@ -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-'; @@ -31,30 +30,33 @@ export type TheGraphClient : ReturnType) & { - options?: TheGraphClientOptions; + options?: TheGraphQueryOptions; }; +export type TheGraphQueryOptions = { + blockFilter?: Maybe; +}; + 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, +/** 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, 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) => @@ -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 = 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 = getNearSdk( - new GraphQLClient(url, extractClientOptions(options)), + new GraphQLClient(url, clientOptions), ); - populateSdkWithOptions(sdk, url, options); + sdk.options = queryOptions; return sdk; }; diff --git a/packages/payment-detection/src/thegraph/conversion-info-retriever.ts b/packages/payment-detection/src/thegraph/conversion-info-retriever.ts index a9ab6e1fc..ab3160a70 100644 --- a/packages/payment-detection/src/thegraph/conversion-info-retriever.ts +++ b/packages/payment-detection/src/thegraph/conversion-info-retriever.ts @@ -22,11 +22,7 @@ export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever> { 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(), @@ -34,11 +30,7 @@ export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever