From ff91c4d7325bd82edcccc1984279b552271de66d Mon Sep 17 00:00:00 2001 From: CarlosQ96 <92376054+CarlosQ96@users.noreply.github.com> Date: Thu, 26 Dec 2024 12:46:45 -0500 Subject: [PATCH] Add networkId logic to superfluid subgraphs (#1896) * add networkId logic to superfluid subgraphs * remove networkId from api call to superfluid * fix eslint --- src/adapters/superFluid/superFluidAdapter.ts | 52 +++++++++++++++---- .../superFluid/superFluidAdapterInterface.ts | 4 +- .../superFluid/superFluidMockAdapter.ts | 2 + .../evm/draftRecurringDonationService.ts | 1 + .../checkUserSuperTokenBalancesQueue.ts | 1 + src/services/recurringDonationService.ts | 1 + test/graphqlQueries.ts | 18 +++++++ 7 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/adapters/superFluid/superFluidAdapter.ts b/src/adapters/superFluid/superFluidAdapter.ts index 39a684a7f..b488642cc 100644 --- a/src/adapters/superFluid/superFluidAdapter.ts +++ b/src/adapters/superFluid/superFluidAdapter.ts @@ -1,19 +1,33 @@ import axios from 'axios'; import { logger } from '../../utils/logger'; -import { isProduction } from '../../utils/utils'; import { FlowUpdatedEvent, SuperFluidAdapterInterface, } from './superFluidAdapterInterface'; +import { NETWORK_IDS } from '../../provider'; -const superFluidGraphqlUrl = +const superFluidGraphqlPolygonUrl = 'https://subgraph-endpoints.superfluid.dev/optimism-mainnet/protocol-v1'; -const superFluidGraphqlStagingUrl = +const superFluidGraphqlPolygonStagingUrl = 'https://optimism-sepolia.subgraph.x.superfluid.dev'; +const superFluidGraphBaseUrl = + 'https://subgraph-endpoints.superfluid.dev/base-mainnet/protocol-v1'; -const subgraphUrl = isProduction - ? superFluidGraphqlUrl - : superFluidGraphqlStagingUrl; +const subgraphUrlMap = { + [NETWORK_IDS.OPTIMISTIC]: superFluidGraphqlPolygonUrl, + [NETWORK_IDS.OPTIMISM_SEPOLIA]: superFluidGraphqlPolygonStagingUrl, + [NETWORK_IDS.BASE_MAINNET]: superFluidGraphBaseUrl, + [NETWORK_IDS.BASE_SEPOLIA]: superFluidGraphBaseUrl, +}; + +// Function to retrieve the subgraph URL for a given network ID +const getSubgraphUrl = (networkId: number) => { + const url = subgraphUrlMap[networkId]; + if (!url) { + throw new Error(`No subgraph URL found for network ID: ${networkId}`); + } + return url; +}; // Define your GraphQL query as a string and prepare your variables const accountQuery = ` @@ -169,8 +183,9 @@ export class SuperFluidAdapter implements SuperFluidAdapterInterface { */ // Optimism works - async accountBalance(accountId: string) { + async accountBalance(accountId: string, networkId: number) { try { + const subgraphUrl = getSubgraphUrl(networkId); const response = await axios.post(subgraphUrl, { query: accountQuery, variables: { @@ -189,12 +204,21 @@ export class SuperFluidAdapter implements SuperFluidAdapterInterface { sender: string; flowRate: string; transactionHash: string; + networkId: number; }): Promise { try { + const subgraphUrl = getSubgraphUrl(params.networkId); + const whereParams = { + receiver: params.receiver, + sender: params.sender, + flowRate: params.flowRate, + transactionHash: params.transactionHash, + }; + const response = await axios.post(subgraphUrl, { query: getFlowsQuery, variables: { - where: params, + where: whereParams, orderBy: 'timestamp', orderDirection: 'asc', }, @@ -213,14 +237,24 @@ export class SuperFluidAdapter implements SuperFluidAdapterInterface { sender: string; flowRate: string; timestamp_gt: number; + networkId: number; }): Promise { try { + const subgraphUrl = getSubgraphUrl(params.networkId); + logger.debug('getFlowByReceiverSenderFlowRate has been called', params); + const whereParams = { + receiver: params.receiver, + sender: params.sender, + flowRate: params.flowRate, + timestamp_gt: params.timestamp_gt, + }; + const response = await axios.post(subgraphUrl, { query: getFlowsQuery, variables: { - where: params, + where: whereParams, orderBy: 'timestamp', orderDirection: 'asc', }, diff --git a/src/adapters/superFluid/superFluidAdapterInterface.ts b/src/adapters/superFluid/superFluidAdapterInterface.ts index 569059bcd..461452579 100644 --- a/src/adapters/superFluid/superFluidAdapterInterface.ts +++ b/src/adapters/superFluid/superFluidAdapterInterface.ts @@ -20,17 +20,19 @@ export interface SuperFluidAdapterInterface { currency: string; recurringDonationTxHash: string; }): Promise; - accountBalance(accountId: string): Promise; + accountBalance(accountId: string, networkId: number): Promise; getFlowByTxHash(params: { receiver: string; sender: string; flowRate: string; transactionHash: string; + networkId: number; }): Promise; getFlowByReceiverSenderFlowRate(params: { receiver: string; sender: string; flowRate: string; timestamp_gt: number; + networkId: number; }): Promise; } diff --git a/src/adapters/superFluid/superFluidMockAdapter.ts b/src/adapters/superFluid/superFluidMockAdapter.ts index 288f6ff52..b52716de6 100644 --- a/src/adapters/superFluid/superFluidMockAdapter.ts +++ b/src/adapters/superFluid/superFluidMockAdapter.ts @@ -85,6 +85,7 @@ export class SuperFluidMockAdapter implements SuperFluidAdapterInterface { sender: string; flowRate: string; timestamp_gt: number; + networkId: number; }): Promise { return Promise.resolve(undefined); } @@ -94,6 +95,7 @@ export class SuperFluidMockAdapter implements SuperFluidAdapterInterface { sender: string; flowRate: string; transactionHash: string; + networkId: number; }): Promise { const { receiver, sender, flowRate, transactionHash } = params; return Promise.resolve({ diff --git a/src/services/chains/evm/draftRecurringDonationService.ts b/src/services/chains/evm/draftRecurringDonationService.ts index cd79dc283..427adceb2 100644 --- a/src/services/chains/evm/draftRecurringDonationService.ts +++ b/src/services/chains/evm/draftRecurringDonationService.ts @@ -47,6 +47,7 @@ export async function matchDraftRecurringDonations( timestamp_gt: convertTimeStampToSeconds( draftRecurringDonation.createdAt.getTime(), ), + networkId: draftRecurringDonation.networkId, }; const flow = await superFluidAdapter.getFlowByReceiverSenderFlowRate(getFlowParams); diff --git a/src/services/cronJobs/checkUserSuperTokenBalancesQueue.ts b/src/services/cronJobs/checkUserSuperTokenBalancesQueue.ts index c034bf91d..c3014ce75 100644 --- a/src/services/cronJobs/checkUserSuperTokenBalancesQueue.ts +++ b/src/services/cronJobs/checkUserSuperTokenBalancesQueue.ts @@ -108,6 +108,7 @@ export const validateDonorSuperTokenBalance = async ( const accountBalances = await superFluidAdapter.accountBalance( user.walletAddress!, + recurringDonation.networkId, ); logger.debug( diff --git a/src/services/recurringDonationService.ts b/src/services/recurringDonationService.ts index 3a4c3ff06..c527b4be8 100644 --- a/src/services/recurringDonationService.ts +++ b/src/services/recurringDonationService.ts @@ -393,6 +393,7 @@ export const updateRecurringDonationStatusWithNetwork = async (params: { flowRate: recurringDonation.flowRate, sender: recurringDonation?.donor?.walletAddress?.toLowerCase() as string, transactionHash: recurringDonation.txHash, + networkId: recurringDonation.networkId, }); if (!txData) { throw new Error( diff --git a/test/graphqlQueries.ts b/test/graphqlQueries.ts index 2cb780bfb..359fdc908 100644 --- a/test/graphqlQueries.ts +++ b/test/graphqlQueries.ts @@ -2072,6 +2072,24 @@ export const removeSocialProfileMutation = ` } `; +export const recurringDonationEligibleProjectsQuery = ` + query ( + networkId: Int + page: Int + limit: Int + ) { + recurringDonationEligibleProjects { + id + slug + title + anchorContracts { + address + networkId + } + } + } +`; + export const getAllowedCountries = ` query { getAllowedCountries {