From 19184ad1b03b19ca6caccb2d3df07350b4f14e18 Mon Sep 17 00:00:00 2001 From: gmbronco <83549293+gmbronco@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:49:15 +0200 Subject: [PATCH] chore: update subgraph urls --- .env.example | 3 ++- .eslintrc.js | 30 ++++++++++++------------- .github/workflows/deploy.yml | 1 + .jest/setEnvVars.ts | 3 ++- index.ts | 2 ++ src/config/arbitrum.json | 2 +- src/config/avalanche.json | 2 +- src/config/gnosis-chain.json | 2 +- src/config/mainnet.json | 2 +- src/config/optimism.json | 2 +- src/config/polygon.json | 2 +- src/modules/network/network.spec.ts | 35 ++++++++++++++++++++--------- src/modules/network/network.ts | 35 ++++++++++++++++++++--------- 13 files changed, 75 insertions(+), 46 deletions(-) diff --git a/.env.example b/.env.example index f87343c6..927ecf93 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,7 @@ DEBUG=balancer PORT=8090 NETWORKS=1,137 INFURA_PROJECT_ID= +SUBGRAPH_API_KEY= DOMAIN_NAME= ## E2E Testing @@ -15,4 +16,4 @@ DYNAMODB_POOLS_WRITE_CAPACITY=25 DYNAMODB_TOKENS_READ_CAPACITY=10 DYNAMODB_TOKENS_WRITE_CAPACITY=10 UPDATE_POOLS_INTERVAL_IN_MINUTES=5 -DECORATE_POOLS_INTERVAL_IN_MINUTES=5 \ No newline at end of file +DECORATE_POOLS_INTERVAL_IN_MINUTES=5 diff --git a/.eslintrc.js b/.eslintrc.js index c4ff1a72..9f594be2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,29 +2,29 @@ module.exports = { root: true, env: { - node: true + node: true, }, - plugins: [ - "@typescript-eslint" - ], + plugins: ['@typescript-eslint'], globals: { NodeJS: true, withDefaults: true, defineProps: true, - defineEmits: true + defineEmits: true, }, extends: [ 'eslint:recommended', - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended" + 'plugin:@typescript-eslint/eslint-recommended', + 'plugin:@typescript-eslint/recommended', ], + ignorePatterns: ['!.jest'], + parserOptions: { ecmaVersion: 2020, - parser: '@typescript-eslint/parser' + parser: '@typescript-eslint/parser', }, rules: { @@ -37,17 +37,15 @@ module.exports = { '@typescript-eslint/no-var-requires': 'off', '@typescript-eslint/no-use-before-define': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', - '@typescript-eslint/ban-ts-comment': 'off' + '@typescript-eslint/ban-ts-comment': 'off', }, overrides: [ { - files: [ - '**/**/*.spec.{j,t}s?(x)' - ], + files: ['**/**/*.spec.{j,t}s?(x)'], env: { - jest: true - } - } - ] + jest: true, + }, + }, + ], }; diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 627048b2..ac4400a7 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,6 +29,7 @@ jobs: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} INFURA_PROJECT_ID: ${{ secrets.INFURA_PROJECT_ID }} + SUBGRAPH_API_KEY: ${{ secrets.SUBGRAPH_API_KEY }} ALCHEMY_KEY: ${{ secrets.ALCHEMY_KEY }} HYPERNATIVE_EMAIL: ${{ secrets.HYPERNATIVE_EMAIL }} HYPERNATIVE_PASSWORD: ${{ secrets.HYPERNATIVE_PASSWORD }} diff --git a/.jest/setEnvVars.ts b/.jest/setEnvVars.ts index 12b1b59d..d9651b68 100644 --- a/.jest/setEnvVars.ts +++ b/.jest/setEnvVars.ts @@ -2,4 +2,5 @@ process.env.TENDERLY_USER = 'mock-user'; process.env.TENDERLY_PROJECT = 'mock-project'; process.env.TENDERLY_ACCESS_KEY = 'xxxxxx'; process.env.INFURA_PROJECT_ID = 'mock-infura'; -process.env.ALCHEMY_KEY = 'mock-alchemy'; \ No newline at end of file +process.env.ALCHEMY_KEY = 'mock-alchemy'; +process.env.SUBGRAPH_API_KEY = 'mock-subgraph-api-key'; diff --git a/index.ts b/index.ts index fae6b3f7..5cbe9ef7 100644 --- a/index.ts +++ b/index.ts @@ -38,6 +38,7 @@ import { autoScaleSecondaryIndex, Capacities } from './cdk/dynamodb'; const { INFURA_PROJECT_ID, + SUBGRAPH_API_KEY, ALCHEMY_KEY, DYNAMODB_POOLS_READ_CAPACITY, DYNAMODB_POOLS_WRITE_CAPACITY, @@ -256,6 +257,7 @@ export class BalancerPoolsAPI extends Stack { }, environment: { INFURA_PROJECT_ID: INFURA_PROJECT_ID || '', + SUBGRAPH_API_KEY: SUBGRAPH_API_KEY || '', ALCHEMY_KEY: ALCHEMY_KEY || '', SENTRY_DSN: SENTRY_DSN || '', DEBUG: DEBUG || '', diff --git a/src/config/arbitrum.json b/src/config/arbitrum.json index f026fbb4..7c3f0fdc 100644 --- a/src/config/arbitrum.json +++ b/src/config/arbitrum.json @@ -3,7 +3,7 @@ "network": "arbitrum", "GqlChain": "ARBITRUM", "rpc": "https://arbitrum-mainnet.infura.io/v3/{{INFURA_PROJECT_ID}}", - "subgraph": "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-arbitrum-v2-beta", + "subgraph": "https://gateway-arbitrum.network.thegraph.com/api/{{SUBGRAPH_API_KEY}}/subgraphs/id/98cQDy6tufTJtshDCuhh9z2kWXsQWBHVh2bqnLHsGAeS", "addresses": { "nativeAsset": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "wrappedNativeAsset": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", diff --git a/src/config/avalanche.json b/src/config/avalanche.json index a650dc1d..b3dfddd3 100644 --- a/src/config/avalanche.json +++ b/src/config/avalanche.json @@ -3,7 +3,7 @@ "network": "avalanche", "GqlChain": "AVALANCHE", "rpc": "https://avalanche-mainnet.infura.io/v3/{{INFURA_PROJECT_ID}}", - "subgraph": "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-avalanche-v2", + "subgraph": "https://gateway-arbitrum.network.thegraph.com/api/{{SUBGRAPH_API_KEY}}/subgraphs/id/7asfmtQA1KYu6CP7YVm5kv4bGxVyfAHEiptt2HMFgkHu", "addresses": { "nativeAsset": "0x0000000000000000000000000000000000000000", "wrappedNativeAsset": "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7", diff --git a/src/config/gnosis-chain.json b/src/config/gnosis-chain.json index 2d8896b8..8b748a46 100644 --- a/src/config/gnosis-chain.json +++ b/src/config/gnosis-chain.json @@ -3,7 +3,7 @@ "network": "gnosis-chain", "GqlChain": "GNOSIS", "rpc": "https://poa-xdai.gateway.pokt.network/v1/lb/91bc0e12a76e7a84dd76189d", - "subgraph": "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-gnosis-chain-v2", + "subgraph": "https://gateway-arbitrum.network.thegraph.com/api/{{SUBGRAPH_API_KEY}}/subgraphs/id/EJezH1Cp31QkKPaBDerhVPRWsKVZLrDfzjrLqpmv6cGg", "addresses": { "nativeAsset": "", "wrappedNativeAsset": "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d", diff --git a/src/config/mainnet.json b/src/config/mainnet.json index 74dd526f..66563ac5 100644 --- a/src/config/mainnet.json +++ b/src/config/mainnet.json @@ -3,7 +3,7 @@ "network": "mainnet", "GqlChain": "MAINNET", "rpc": "https://mainnet.infura.io/v3/{{INFURA_PROJECT_ID}}", - "subgraph": "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-v2", + "subgraph": "https://gateway-arbitrum.network.thegraph.com/api/{{SUBGRAPH_API_KEY}}/subgraphs/id/C4ayEZP2yTXRAB8vSaTrgN4m9anTe9Mdm2ViyiAuV9TV", "addresses": { "nativeAsset": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "wrappedNativeAsset": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", diff --git a/src/config/optimism.json b/src/config/optimism.json index 5d6a5a61..1c747e8d 100644 --- a/src/config/optimism.json +++ b/src/config/optimism.json @@ -3,7 +3,7 @@ "network": "optimism", "GqlChain": "OPTIMISM", "rpc": "https://optimism-mainnet.infura.io/v3/{{INFURA_PROJECT_ID}}", - "subgraph": "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-optimism-v2", + "subgraph": "https://gateway-arbitrum.network.thegraph.com/api/{{SUBGRAPH_API_KEY}}/subgraphs/id/FsmdxmvBJLGjUQPxKMRtcWKzuCNpomKuMTbSbtRtggZ7", "addresses": { "nativeAsset": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "wrappedNativeAsset": "0x4200000000000000000000000000000000000006", diff --git a/src/config/polygon.json b/src/config/polygon.json index 07ceab96..22633200 100644 --- a/src/config/polygon.json +++ b/src/config/polygon.json @@ -3,7 +3,7 @@ "network": "polygon", "GqlChain": "POLYGON", "rpc": "https://polygon-mainnet.infura.io/v3/{{INFURA_PROJECT_ID}}", - "subgraph": "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-polygon-v2", + "subgraph": "https://gateway-arbitrum.network.thegraph.com/api/{{SUBGRAPH_API_KEY}}/subgraphs/id/H9oPAbXnobBRq1cB3HDmbZ1E8MWQyJYQjT1QDJMrdbNp", "addresses": { "nativeAsset": "0x0000000000000000000000000000000000001010", "wrappedNativeAsset": "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270", diff --git a/src/modules/network/network.spec.ts b/src/modules/network/network.spec.ts index 2782358b..2f153cae 100644 --- a/src/modules/network/network.spec.ts +++ b/src/modules/network/network.spec.ts @@ -1,4 +1,4 @@ -import { getRpcUrl } from "./network"; +import { getRpcUrl, getSubgraphUrl } from './network'; jest.mock('@/config', () => { return { @@ -6,15 +6,22 @@ jest.mock('@/config', () => { rpc: 'https://mainnet.infura.io/v3/{{INFURA_PROJECT_ID}}', }, 100: { - rpc: 'https://poa-xdai.gateway.pokt.network/v1/lb/888c0e12a76e7a84dd76189d' + rpc: 'https://poa-xdai.gateway.pokt.network/v1/lb/888c0e12a76e7a84dd76189d', }, 1101: { - rpc: 'https://polygonzkevm-mainnet.g.alchemy.com/v2/{{ALCHEMY_KEY}}' - } + rpc: 'https://polygonzkevm-mainnet.g.alchemy.com/v2/{{ALCHEMY_KEY}}', + subgraph: 'string-with-{{SUBGRAPH_API_KEY}}', + }, }; }); describe('network module', () => { + describe('getSubgraphUrl', () => { + it('should replace template value with a key', () => { + expect(getSubgraphUrl(1101)).toEqual('string-with-mock-subgraph-api-key'); + }); + }); + describe('getRpcUrl', () => { it('Should throw an error if an invalid network id is passed', () => { const invalidNetworkId = 22; @@ -26,29 +33,35 @@ describe('network module', () => { }); it('Should return the rpcUrl with ALCHEMY_KEY in the template replaced', () => { - expect(getRpcUrl(1101)).toEqual('https://polygonzkevm-mainnet.g.alchemy.com/v2/mock-alchemy'); + expect(getRpcUrl(1101)).toEqual( + 'https://polygonzkevm-mainnet.g.alchemy.com/v2/mock-alchemy' + ); }); it('Should work for networks that dont use infura', () => { - expect(getRpcUrl(100)).toEqual('https://poa-xdai.gateway.pokt.network/v1/lb/888c0e12a76e7a84dd76189d'); + expect(getRpcUrl(100)).toEqual( + 'https://poa-xdai.gateway.pokt.network/v1/lb/888c0e12a76e7a84dd76189d' + ); }); it('Should throw an error if the network requires INFURA_PROJECT_ID but it is not passed', () => { - jest.resetModules() + jest.resetModules(); delete process.env.INFURA_PROJECT_ID; expect(() => require('./network').getRpcUrl(1)).toThrow(); }); it('Should throw an error if the network requires ALCHEMY_KEY but it is not passed', () => { - jest.resetModules() + jest.resetModules(); delete process.env.ALCHEMY_KEY; expect(() => require('./network').getRpcUrl(1101)).toThrow(); }); it('Should work for networks that dont use infura without INFURA_PROJECT_ID set', () => { - jest.resetModules() + jest.resetModules(); delete process.env.INFURA_PROJECT_ID; - expect(getRpcUrl(100)).toEqual('https://poa-xdai.gateway.pokt.network/v1/lb/888c0e12a76e7a84dd76189d'); + expect(getRpcUrl(100)).toEqual( + 'https://poa-xdai.gateway.pokt.network/v1/lb/888c0e12a76e7a84dd76189d' + ); }); }); -}); \ No newline at end of file +}); diff --git a/src/modules/network/network.ts b/src/modules/network/network.ts index de29462b..338c9118 100644 --- a/src/modules/network/network.ts +++ b/src/modules/network/network.ts @@ -1,9 +1,7 @@ -import { - Network, -} from '@/constants'; -import configs from '@/config'; +import { Network } from '@/constants'; +import configs from '@/config'; -const { INFURA_PROJECT_ID, ALCHEMY_KEY } = process.env; +const { INFURA_PROJECT_ID, ALCHEMY_KEY, SUBGRAPH_API_KEY } = process.env; export default function template(templateString, templateVariables) { return templateString.replace(/{{(.*?)}}/g, (_, g) => templateVariables[g]); @@ -16,15 +14,19 @@ export function getRpcUrl(networkId: number): string { const envRpcUrl = process.env[`RPC_URL_${networkId}`]; const templateUrl = envRpcUrl || configs[networkId].rpc; if (templateUrl.match(/INFURA_PROJECT_ID/) && INFURA_PROJECT_ID == null) { - throw new Error(`INFURA_PROJECT_ID env variable must be set for network ${networkId}`) + throw new Error( + `INFURA_PROJECT_ID env variable must be set for network ${networkId}` + ); } if (templateUrl.match(/ALCHEMY_KEY/) && ALCHEMY_KEY == null) { - throw new Error(`ALCHEMY_KEY env variable must be set for network ${networkId}`) + throw new Error( + `ALCHEMY_KEY env variable must be set for network ${networkId}` + ); } const rpcUrl = template(templateUrl, { INFURA_PROJECT_ID, - ALCHEMY_KEY + ALCHEMY_KEY, }); return rpcUrl; @@ -33,7 +35,18 @@ export function getRpcUrl(networkId: number): string { export function getSubgraphUrl(networkId: number): string { requireValidNetworkId(networkId); - return configs[networkId].subgraph; + const templateUrl = configs[networkId].subgraph; + if (templateUrl.match(/SUBGRAPH_API_KEY/) && SUBGRAPH_API_KEY == null) { + throw new Error( + `SUBGRAPH_API_KEY env variable must be set for network ${networkId}` + ); + } + + const subgraphUrl = template(templateUrl, { + SUBGRAPH_API_KEY, + }); + + return subgraphUrl; } export function isValidNetworkId(networkId: number): boolean { @@ -42,7 +55,7 @@ export function isValidNetworkId(networkId: number): boolean { export function requireValidNetworkId(networkId: number): void { if (!isValidNetworkId(networkId)) { - throw new Error(`Invalid network ID ${networkId}`) + throw new Error(`Invalid network ID ${networkId}`); } } @@ -52,4 +65,4 @@ export function getPlatformId(chainId: string | number): string | undefined { export function getNativeAssetPriceSymbol(chainId: string | number): string { return configs[chainId].coingecko.nativeAssetPriceSymbol || 'eth'; -} \ No newline at end of file +}