From b93e4236478f693f0693cb40f3cc49884ab87e20 Mon Sep 17 00:00:00 2001 From: Cypher Pepe <125112044+cypherpepe@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:10:34 +0300 Subject: [PATCH] refactor: optimize cache-config.ts structure --- lib/cron/cache-config.ts | 427 +++++++++------------------------------ 1 file changed, 92 insertions(+), 335 deletions(-) diff --git a/lib/cron/cache-config.ts b/lib/cron/cache-config.ts index 98e026e993..a5202e8936 100644 --- a/lib/cron/cache-config.ts +++ b/lib/cron/cache-config.ts @@ -1,348 +1,105 @@ -import { Protocol } from '@uniswap/router-sdk' -import { V2SubgraphProvider, V3SubgraphProvider, V4SubgraphProvider } from '@uniswap/smart-order-router' -import { ChainId } from '@uniswap/sdk-core' +import { Protocol } from '@uniswap/router-sdk'; +import { V2SubgraphProvider, V3SubgraphProvider, V4SubgraphProvider } from '@uniswap/smart-order-router'; +import { ChainId } from '@uniswap/sdk-core'; // during local cdk stack update, the env vars are not populated // make sure to fill in the env vars below // process.env.ALCHEMY_QUERY_KEY = '' -export const v4SubgraphUrlOverride = (chainId: ChainId) => { - switch (chainId) { - case ChainId.SEPOLIA: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v4-sepolia-test/api` - default: - return undefined - } -} +const SUBGRAPH_CONFIG = { + v4: { trackedEthThreshold: 0.01, untrackedUsdThreshold: 0 }, + v3: { trackedEthThreshold: 0.01, untrackedUsdThreshold: 25000 }, + v2: { trackedEthThreshold: 0.025, untrackedUsdThreshold: Number.MAX_VALUE, pageSize: 1000 }, +}; -export const v3SubgraphUrlOverride = (chainId: ChainId) => { - switch (chainId) { - case ChainId.MAINNET: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-mainnet/api` - case ChainId.ARBITRUM_ONE: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-arbitrum-ii/api` - case ChainId.POLYGON: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-polygon/api` - case ChainId.OPTIMISM: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-optimism-ii/api` - case ChainId.AVALANCHE: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-avalanche/api` - case ChainId.BNB: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-bsc-ii/api` - case ChainId.BLAST: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-blast/api` - case ChainId.BASE: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-base/api` - case ChainId.CELO: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-celo/api` - case ChainId.WORLDCHAIN: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-worldchain/api` - case ChainId.ASTROCHAIN_SEPOLIA: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v3-astrochain-sepolia/api` - default: - return undefined - } -} +const TIMEOUTS = { + v2: { + MAINNET: 840000, + DEFAULT: 90000, + }, + v3: 90000, + v4: 90000, +}; -export const v2SubgraphUrlOverride = (chainId: ChainId) => { - switch (chainId) { - case ChainId.MAINNET: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-mainnet/api` - case ChainId.ARBITRUM_ONE: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-arbitrum/api` - case ChainId.POLYGON: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-polygon/api` - case ChainId.OPTIMISM: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-optimism/api` - case ChainId.AVALANCHE: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-avalanche/api` - case ChainId.BNB: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-bsc/api` - case ChainId.BLAST: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-blast/api` - case ChainId.BASE: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-base/api` - case ChainId.WORLDCHAIN: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-worldchain/api` - case ChainId.ASTROCHAIN_SEPOLIA: - return `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-v2-astrochain-sepolia/api` - default: - return undefined - } -} +const CHAIN_MAP: Record = { + [ChainId.MAINNET]: { v2: 'mainnet', v3: 'mainnet', timeout: TIMEOUTS.v2.MAINNET }, + [ChainId.ARBITRUM_ONE]: { v2: 'arbitrum', v3: 'arbitrum-ii' }, + [ChainId.POLYGON]: { v2: 'polygon', v3: 'polygon' }, + [ChainId.OPTIMISM]: { v2: 'optimism', v3: 'optimism-ii' }, + [ChainId.AVALANCHE]: { v2: 'avalanche', v3: 'avalanche' }, + [ChainId.BNB]: { v2: 'bsc', v3: 'bsc-ii' }, + [ChainId.BLAST]: { v2: 'blast', v3: 'blast' }, + [ChainId.BASE]: { v2: 'base', v3: 'base', timeout: TIMEOUTS.v2.MAINNET }, + [ChainId.CELO]: { v2: 'celo', v3: 'celo' }, + [ChainId.WORLDCHAIN]: { v2: 'worldchain', v3: 'worldchain' }, + [ChainId.ASTROCHAIN_SEPOLIA]: { v2: 'astrochain-sepolia', v3: 'astrochain-sepolia' }, +}; -const v4TrackedEthThreshold = 0.01 // Pools need at least 0.01 of trackedEth to be selected -const v4UntrackedUsdThreshold = 0 // v4 subgraph totalValueLockedUSDUntracked returns 0, even with the pools that have appropriate liqudities and correct pool pricing +const createSubgraphUrl = (chain: string, version: string) => + `https://subgraph.satsuma-prod.com/${process.env.ALCHEMY_QUERY_KEY}/uniswap/uniswap-${version}-${chain}/api`; -export const v3TrackedEthThreshold = 0.01 // Pools need at least 0.01 of trackedEth to be selected -const v3UntrackedUsdThreshold = 25000 // Pools need at least 25K USD (untracked) to be selected (for metrics only) +const getSubgraphUrl = (version: 'v2' | 'v3', chainId: ChainId) => { + const chainInfo = CHAIN_MAP[chainId]; + return chainInfo ? createSubgraphUrl(chainInfo[version], version) : undefined; +}; -export const v2TrackedEthThreshold = 0.025 // Pairs need at least 0.025 of trackedEth to be selected -const v2UntrackedUsdThreshold = Number.MAX_VALUE // Pairs need at least 1K USD (untracked) to be selected (for metrics only) +const v4SubgraphUrlOverride = (chainId: ChainId) => + chainId === ChainId.SEPOLIA ? createSubgraphUrl('sepolia-test', 'v4') : undefined; + +const createProviderConfig = ( + protocol: Protocol, + chainId: ChainId, + version: 'v2' | 'v3' | 'v4', + thresholds: { trackedEthThreshold: number; untrackedUsdThreshold: number }, + timeout: number, + pageSize?: number +) => ({ + protocol, + chainId, + timeout, + provider: new (version === 'v4' ? V4SubgraphProvider : version === 'v3' ? V3SubgraphProvider : V2SubgraphProvider)( + chainId, + version === 'v2' ? (pageSize || 3) : 3, // V2 pageSize = 1000, others = 3 + timeout, + true, + thresholds.trackedEthThreshold, + thresholds.untrackedUsdThreshold, + version === 'v4' ? v4SubgraphUrlOverride(chainId) : getSubgraphUrl(version, chainId) + ), +}); export const chainProtocols = [ - // V3. - { - protocol: Protocol.V3, - chainId: ChainId.MAINNET, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.MAINNET, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.MAINNET) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.ARBITRUM_ONE, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.ARBITRUM_ONE, - 5, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.ARBITRUM_ONE) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.POLYGON, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.POLYGON, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.POLYGON) - ), - }, - // Waiting for Alchemy subgraph - { - protocol: Protocol.V3, - chainId: ChainId.OPTIMISM, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.OPTIMISM, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.OPTIMISM) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.CELO, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.CELO, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.CELO) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.BNB, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.BNB, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.BNB) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.AVALANCHE, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.AVALANCHE, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.AVALANCHE) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.BASE, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.BASE, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.BASE) - ), - }, - { - protocol: Protocol.V3, - chainId: ChainId.BLAST, - timeout: 90000, - provider: new V3SubgraphProvider( - ChainId.BLAST, - 3, - 90000, - true, - v3TrackedEthThreshold, - v3UntrackedUsdThreshold, - v3SubgraphUrlOverride(ChainId.BLAST) - ), - }, + // V3 Protocols + ...Object.keys(CHAIN_MAP).map((chainId) => + createProviderConfig( + Protocol.V3, + parseInt(chainId, 10), + 'v3', + SUBGRAPH_CONFIG.v3, + TIMEOUTS.v3 + ) + ), - // V2. - { - protocol: Protocol.V2, - chainId: ChainId.MAINNET, - timeout: 840000, - provider: new V2SubgraphProvider( - ChainId.MAINNET, - 5, - 900000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.MAINNET) - ), // 1000 is the largest page size supported by thegraph - }, - { - protocol: Protocol.V2, - chainId: ChainId.ARBITRUM_ONE, - timeout: 90000, - provider: new V2SubgraphProvider( - ChainId.ARBITRUM_ONE, - 3, - 90000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.ARBITRUM_ONE) - ), - }, - { - protocol: Protocol.V2, - chainId: ChainId.POLYGON, - timeout: 90000, - provider: new V2SubgraphProvider( - ChainId.POLYGON, - 3, - 90000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.POLYGON) - ), - }, - { - protocol: Protocol.V2, - chainId: ChainId.OPTIMISM, - timeout: 90000, - provider: new V2SubgraphProvider( - ChainId.OPTIMISM, - 3, - 90000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.OPTIMISM) - ), - }, - { - protocol: Protocol.V2, - chainId: ChainId.BNB, - timeout: 90000, - provider: new V2SubgraphProvider( - ChainId.BNB, - 3, - 90000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.BNB) - ), - }, - { - protocol: Protocol.V2, - chainId: ChainId.AVALANCHE, - timeout: 90000, - provider: new V2SubgraphProvider( - ChainId.AVALANCHE, - 3, - 90000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.AVALANCHE) - ), - }, - { - protocol: Protocol.V2, - chainId: ChainId.BASE, - timeout: 840000, - provider: new V2SubgraphProvider( - ChainId.BASE, - 5, - 900000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.BASE) - ), - }, - { - protocol: Protocol.V2, - chainId: ChainId.BLAST, - timeout: 90000, - provider: new V2SubgraphProvider( - ChainId.BLAST, - 3, - 90000, - true, - 1000, - v2TrackedEthThreshold, - v2UntrackedUsdThreshold, - v2SubgraphUrlOverride(ChainId.BLAST) - ), - }, - { - protocol: Protocol.V4, - chainId: ChainId.SEPOLIA, - timeout: 90000, - provider: new V4SubgraphProvider( - ChainId.SEPOLIA, - 3, - 90000, - true, - v4TrackedEthThreshold, - v4UntrackedUsdThreshold, - v4SubgraphUrlOverride(ChainId.SEPOLIA) - ), - }, -] + // V2 Protocols + ...Object.keys(CHAIN_MAP).map((chainId) => { + const parsedChainId = parseInt(chainId, 10); + const timeout = CHAIN_MAP[parsedChainId]?.timeout || TIMEOUTS.v2.DEFAULT; + return createProviderConfig( + Protocol.V2, + parsedChainId, + 'v2', + SUBGRAPH_CONFIG.v2, + timeout, + SUBGRAPH_CONFIG.v2.pageSize + ); + }), + + // V4 Protocol (Sepolia only) + createProviderConfig( + Protocol.V4, + ChainId.SEPOLIA, + 'v4', + SUBGRAPH_CONFIG.v4, + TIMEOUTS.v4 + ), +];