Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: superfluid subgraph urls - 2nd attempt #1421

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
schema: https://api.thegraph.com/subgraphs/name/superfluid-finance/protocol-v1-goerli
schema: https://subgraph-endpoints.superfluid.dev/eth-sepolia/protocol-v1
23 changes: 19 additions & 4 deletions packages/payment-detection/src/thegraph/superfluid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import { RequestConfig } from 'graphql-request/src/types';

const BASE_URL = `https://subgraph-endpoints.superfluid.dev`;
const NETWORK_TO_URL: Record<string, string> = {
optimism: 'optimism-mainnet',
'arbitrum-one': 'arbitrum-one',
avalanche: 'avalanche-c',
base: 'base-mainnet',
bsc: 'bsc-mainnet',
celo: 'celo-mainnet',
mainnet: 'eth-mainnet',
matic: 'polygon-mainnet',
optimism: 'optimism-mainnet',
sepolia: 'eth-sepolia',
xdai: 'xdai-mainnet',
};

// NB: the GraphQL client is automatically generated based on files present in ./queries,
Expand All @@ -24,13 +32,20 @@ export const getTheGraphSuperfluidClient = (
options?: TheGraphClientOptions,
): TheGraphSuperfluidClient => {
const { baseUrl: _baseUrl, ...clientOptions } = options ?? {};
const url = buildTheGraphSuperfluidUrl(_baseUrl, network);
return getSdk(new GraphQLClient(url, clientOptions));
};

const baseUrl = _baseUrl || network === 'private' ? 'http://localhost:8000' : BASE_URL;
export const buildTheGraphSuperfluidUrl = (
baseUrl: string | undefined,
network: string,
): string => {
// Note: it is also possible to use the IPFS hash of the subgraph
// eg. /subgraphs/id/QmcCaSkefrmhe4xQj6Y6BBbHiFkbrn6UGDEBUWER7nt399
// which is a better security but would require an update of the
// library each time the subgraph is updated, which isn't ideal
// for early testing.
const url = `${baseUrl}/${NETWORK_TO_URL[network] || network}/protocol-v1`;
return getSdk(new GraphQLClient(url, clientOptions));
return network === 'private'
? 'http://localhost:8000/subgraphs/name/superfluid-finance/protocol-v1-goerli'
: `${baseUrl || BASE_URL}/${NETWORK_TO_URL[network]}/protocol-v1`;
};
56 changes: 56 additions & 0 deletions packages/payment-detection/test/thegraph/superfulid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { buildTheGraphSuperfluidUrl } from '../../src/thegraph/superfluid';

describe('buildTheGraphSuperfluidUrl', () => {
it('should build the correct URL when baseUrl is defined', () => {
const url = buildTheGraphSuperfluidUrl('https://example.com', 'sepolia');
expect(url).toBe('https://example.com/eth-sepolia/protocol-v1');
});
it('should build the correct URL when network is private', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'private');
expect(url).toBe('http://localhost:8000/subgraphs/name/superfluid-finance/protocol-v1-goerli');
});
it('should build the correct URL when baseUrl is undefined and network is private', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'private');
expect(url).toBe('http://localhost:8000/subgraphs/name/superfluid-finance/protocol-v1-goerli');
});
it('should build the correct URL when baseUrl is undefined and network is arbitrum-one', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'arbitrum-one');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/arbitrum-one/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is avalanche', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'avalanche');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/avalanche-c/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is base', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'base');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/base-mainnet/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is bsc', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'bsc');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/bsc-mainnet/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is celo', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'celo');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/celo-mainnet/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is mainnet', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'mainnet');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/eth-mainnet/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is matic', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'matic');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/polygon-mainnet/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is optimism', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'optimism');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/optimism-mainnet/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is sepolia', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'sepolia');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/eth-sepolia/protocol-v1');
});
it('should build the correct URL when baseUrl is undefined and network is xdai', () => {
const url = buildTheGraphSuperfluidUrl(undefined, 'xdai');
expect(url).toBe('https://subgraph-endpoints.superfluid.dev/xdai-mainnet/protocol-v1');
});
});