From 2719883ad696b196a6352e707887cfba76df4920 Mon Sep 17 00:00:00 2001 From: Alexandre ABRIOUX Date: Tue, 28 Nov 2023 17:34:32 +0100 Subject: [PATCH] fix(payment-detection): differenciate decentralized network case (#1267) Co-authored-by: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> --- .../payment-detection/src/thegraph/client.ts | 35 ++++++++++++++----- .../src/thegraph/conversion-info-retriever.ts | 4 +-- .../src/thegraph/info-retriever.ts | 4 +-- .../queries/GetPaymentsAndEscrowState.graphql | 8 ++--- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/packages/payment-detection/src/thegraph/client.ts b/packages/payment-detection/src/thegraph/client.ts index e0adae1355..b080dddf7d 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,17 +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; }; -const extractClientOptions = (options?: TheGraphClientOptions) => { - return pick(options, 'timeout'); +/** 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, +): [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) => @@ -50,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), ); - sdk.options = 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), ); - sdk.options = 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 cede1c6335..ab3160a703 100644 --- a/packages/payment-detection/src/thegraph/conversion-info-retriever.ts +++ b/packages/payment-detection/src/thegraph/conversion-info-retriever.ts @@ -22,7 +22,7 @@ export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever> { const { payments } = params.acceptedTokens ? await this.client.GetAnyToFungiblePayments({ - blockFilter: { number_gte: this.client.options?.minIndexedBlock || 0 }, + blockFilter: this.client.options?.blockFilter, reference: utils.keccak256(`0x${params.paymentReference}`), to: params.toAddress.toLowerCase(), currency: params.requestCurrency.hash.toLowerCase(), @@ -30,7 +30,7 @@ export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever