Skip to content

Commit

Permalink
Merge pull request #278 from balancer/subgraph-urls
Browse files Browse the repository at this point in the history
chore: update subgraph urls
  • Loading branch information
gmbronco authored Jun 7, 2024
2 parents 12f760f + 19184ad commit f4b8664
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ DEBUG=balancer
PORT=8090
NETWORKS=1,137
INFURA_PROJECT_ID=
SUBGRAPH_API_KEY=
DOMAIN_NAME=

## E2E Testing
Expand All @@ -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
DECORATE_POOLS_INTERVAL_IN_MINUTES=5
30 changes: 14 additions & 16 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
},
},
],
};
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion .jest/setEnvVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
process.env.ALCHEMY_KEY = 'mock-alchemy';
process.env.SUBGRAPH_API_KEY = 'mock-subgraph-api-key';
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 || '',
Expand Down
2 changes: 1 addition & 1 deletion src/config/arbitrum.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config/avalanche.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config/gnosis-chain.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config/optimism.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config/polygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 24 additions & 11 deletions src/modules/network/network.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { getRpcUrl } from "./network";
import { getRpcUrl, getSubgraphUrl } from './network';

jest.mock('@/config', () => {
return {
1: {
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;
Expand All @@ -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'
);
});
});
});
});
35 changes: 24 additions & 11 deletions src/modules/network/network.ts
Original file line number Diff line number Diff line change
@@ -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]);
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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}`);
}
}

Expand All @@ -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';
}
}

0 comments on commit f4b8664

Please sign in to comment.