Skip to content

Commit

Permalink
fix(payment-detection): differenciate decentralized network case (#1267)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com>
  • Loading branch information
alexandre-abrioux and benjlevesque committed Nov 28, 2023
1 parent 28cf4b6 commit 2719883
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
35 changes: 26 additions & 9 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,17 +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;
};

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<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 @@ -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<CurrencyTypes.EvmChainName> = 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<CurrencyTypes.NearChainName> = getNearSdk(
new GraphQLClient(url, extractClientOptions(options)),
new GraphQLClient(url, clientOptions),
);
sdk.options = options;
sdk.options = queryOptions;
return sdk;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever<Conve
): Promise<PaymentTypes.AllNetworkEvents<PaymentTypes.IERC20FeePaymentEventParameters>> {
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(),
acceptedTokens: params.acceptedTokens.map((t) => t.toLowerCase()),
contractAddress: params.contractAddress.toLowerCase(),
})
: await this.client.GetAnyToNativePayments({
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(),
Expand Down
4 changes: 2 additions & 2 deletions packages/payment-detection/src/thegraph/info-retriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +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: { number_gte: this.client.options?.minIndexedBlock || 0 },
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 @@ -46,7 +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: { number_gte: this.client.options?.minIndexedBlock || 0 },
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fragment EscrowEventResult on EscrowEvent {
}

query GetPaymentsAndEscrowState(
$blockFilter: Block_height!
$blockFilter: Block_height
$reference: Bytes!
$to: Bytes!
$tokenAddress: Bytes
Expand Down Expand Up @@ -60,7 +60,7 @@ query GetPaymentsAndEscrowState(

# AnyToErc20 payments: denominated in request $currency payable with many token addresses
query GetAnyToFungiblePayments(
$blockFilter: Block_height!
$blockFilter: Block_height
$reference: Bytes!
$to: Bytes!
$currency: Bytes!
Expand All @@ -85,7 +85,7 @@ query GetAnyToFungiblePayments(

# AnyToETH payments: denominated in request $currency payable with the EVM native token
query GetAnyToNativePayments(
$blockFilter: Block_height!
$blockFilter: Block_height
$reference: Bytes!
$to: Bytes!
$currency: Bytes!
Expand All @@ -109,7 +109,7 @@ query GetAnyToNativePayments(

# Receivables can be transferred to different owners, so searching by to could drop balance events.
query GetPaymentsAndEscrowStateForReceivables(
$blockFilter: Block_height!
$blockFilter: Block_height
$reference: Bytes!
$tokenAddress: Bytes!
$contractAddress: Bytes!
Expand Down

0 comments on commit 2719883

Please sign in to comment.