Skip to content

Commit

Permalink
Add networkId logic to superfluid subgraphs (#1896)
Browse files Browse the repository at this point in the history
* add networkId logic to superfluid subgraphs

* remove networkId from api call to superfluid

* fix eslint
  • Loading branch information
CarlosQ96 authored Dec 26, 2024
1 parent b41c543 commit ff91c4d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
52 changes: 43 additions & 9 deletions src/adapters/superFluid/superFluidAdapter.ts
Original file line number Diff line number Diff line change
@@ -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 = `
Expand Down Expand Up @@ -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: {
Expand All @@ -189,12 +204,21 @@ export class SuperFluidAdapter implements SuperFluidAdapterInterface {
sender: string;
flowRate: string;
transactionHash: string;
networkId: number;
}): Promise<FlowUpdatedEvent | undefined> {
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',
},
Expand All @@ -213,14 +237,24 @@ export class SuperFluidAdapter implements SuperFluidAdapterInterface {
sender: string;
flowRate: string;
timestamp_gt: number;
networkId: number;
}): Promise<FlowUpdatedEvent | undefined> {
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',
},
Expand Down
4 changes: 3 additions & 1 deletion src/adapters/superFluid/superFluidAdapterInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ export interface SuperFluidAdapterInterface {
currency: string;
recurringDonationTxHash: string;
}): Promise<any>;
accountBalance(accountId: string): Promise<any>;
accountBalance(accountId: string, networkId: number): Promise<any>;
getFlowByTxHash(params: {
receiver: string;
sender: string;
flowRate: string;
transactionHash: string;
networkId: number;
}): Promise<FlowUpdatedEvent | undefined>;
getFlowByReceiverSenderFlowRate(params: {
receiver: string;
sender: string;
flowRate: string;
timestamp_gt: number;
networkId: number;
}): Promise<FlowUpdatedEvent | undefined>;
}
2 changes: 2 additions & 0 deletions src/adapters/superFluid/superFluidMockAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class SuperFluidMockAdapter implements SuperFluidAdapterInterface {
sender: string;
flowRate: string;
timestamp_gt: number;
networkId: number;
}): Promise<FlowUpdatedEvent | undefined> {
return Promise.resolve(undefined);
}
Expand All @@ -94,6 +95,7 @@ export class SuperFluidMockAdapter implements SuperFluidAdapterInterface {
sender: string;
flowRate: string;
transactionHash: string;
networkId: number;
}): Promise<FlowUpdatedEvent | undefined> {
const { receiver, sender, flowRate, transactionHash } = params;
return Promise.resolve({
Expand Down
1 change: 1 addition & 0 deletions src/services/chains/evm/draftRecurringDonationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function matchDraftRecurringDonations(
timestamp_gt: convertTimeStampToSeconds(
draftRecurringDonation.createdAt.getTime(),
),
networkId: draftRecurringDonation.networkId,
};
const flow =
await superFluidAdapter.getFlowByReceiverSenderFlowRate(getFlowParams);
Expand Down
1 change: 1 addition & 0 deletions src/services/cronJobs/checkUserSuperTokenBalancesQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const validateDonorSuperTokenBalance = async (

const accountBalances = await superFluidAdapter.accountBalance(
user.walletAddress!,
recurringDonation.networkId,
);

logger.debug(
Expand Down
1 change: 1 addition & 0 deletions src/services/recurringDonationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions test/graphqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ff91c4d

Please sign in to comment.