From d8b5439f084d357adfcf951df3653aaa570c14c1 Mon Sep 17 00:00:00 2001 From: Christophe Deveaux Date: Tue, 11 Jun 2024 09:52:48 +0200 Subject: [PATCH] refactor: move subgraph endpoints to .env (#147) Co-authored-by: spsjvc --- .env.sample | 2 ++ .github/workflows/ci.yml | 4 +++ .github/workflows/generate-token-lists.yml | 2 ++ src/init.ts | 6 +++++ src/lib/graph.ts | 31 +++++++++------------- src/main.ts | 8 ++---- 6 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 src/init.ts diff --git a/.env.sample b/.env.sample index 72a000a..e4ebb5a 100644 --- a/.env.sample +++ b/.env.sample @@ -3,5 +3,7 @@ ARB_ONE_RPC="https://arbitrum.infura.io/v3/${INFURA_KEY}" ARB_SEPOLIA_RPC="https://arbitrum-sepolia.infura.io/v3/${INFURA_KEY}" MAINNET_RPC="https://mainnet.infura.io/v3/${INFURA_KEY}" SEPOLIA_RPC="https://sepolia.infura.io/v3/${INFURA_KEY}" +L2_GATEWAY_SUBGRAPH_URL= +L2_GATEWAY_SEPOLIA_SUBGRAPH_URL= l2NetworkID=42161 PORT=3000 \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d44fe3e..aef867e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,6 +54,8 @@ jobs: INFURA_KEY: '${{ secrets.INFURA_KEY }}' MAINNET_RPC: 'https://mainnet.infura.io/v3/${{ secrets.INFURA_KEY }}' SEPOLIA_RPC: 'https://sepolia.infura.io/v3/${{ secrets.INFURA_KEY }}' + L2_GATEWAY_SUBGRAPH_URL: '${{ secrets.L2_GATEWAY_SUBGRAPH_URL }}' + L2_GATEWAY_SEPOLIA_SUBGRAPH_URL: '${{ secrets.L2_GATEWAY_SEPOLIA_SUBGRAPH_URL }}' steps: - uses: actions/checkout@v3 @@ -72,6 +74,8 @@ jobs: INFURA_KEY: '${{ secrets.INFURA_KEY }}' MAINNET_RPC: 'https://mainnet.infura.io/v3/${{ secrets.INFURA_KEY }}' SEPOLIA_RPC: 'https://sepolia.infura.io/v3/${{ secrets.INFURA_KEY }}' + L2_GATEWAY_SUBGRAPH_URL: '${{ secrets.L2_GATEWAY_SUBGRAPH_URL }}' + L2_GATEWAY_SEPOLIA_SUBGRAPH_URL: '${{ secrets.L2_GATEWAY_SEPOLIA_SUBGRAPH_URL }}' steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/generate-token-lists.yml b/.github/workflows/generate-token-lists.yml index f85c771..33dc7cb 100644 --- a/.github/workflows/generate-token-lists.yml +++ b/.github/workflows/generate-token-lists.yml @@ -188,6 +188,8 @@ jobs: SEPOLIA_RPC: 'https://sepolia.infura.io/v3/${{ secrets.INFURA_KEY }}' ARB_ONE_RPC: 'https://arbitrum-mainnet.infura.io/v3/${{ secrets.INFURA_KEY }}' ARB_SEPOLIA_RPC: 'https://arbitrum-sepolia.infura.io/v3/${{ secrets.INFURA_KEY }}' + L2_GATEWAY_SUBGRAPH_URL: '${{ secrets.L2_GATEWAY_SUBGRAPH_URL }}' + L2_GATEWAY_SEPOLIA_SUBGRAPH_URL: '${{ secrets.L2_GATEWAY_SEPOLIA_SUBGRAPH_URL }}' steps: - uses: actions/checkout@v3 diff --git a/src/init.ts b/src/init.ts new file mode 100644 index 0000000..f4a1d45 --- /dev/null +++ b/src/init.ts @@ -0,0 +1,6 @@ +import * as dotenv from 'dotenv'; +import dotenvExpand from 'dotenv-expand'; +import './customNetworks'; + +const myEnv = dotenv.config(); +dotenvExpand.expand(myEnv); diff --git a/src/lib/graph.ts b/src/lib/graph.ts index 428b4dc..369a5f9 100644 --- a/src/lib/graph.ts +++ b/src/lib/graph.ts @@ -3,19 +3,21 @@ import { isNetwork } from './utils'; import { GraphTokenResult, GraphTokensResult } from './types'; import { excludeList } from './constants'; -const apolloL2GatewaysRinkebyClient = - 'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway-rinkeby'; -const apolloL2GatewaysClient = - 'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway'; +if (!process.env.L2_GATEWAY_SUBGRAPH_URL) { + throw new Error('process.env.L2_GATEWAY_SUBGRAPH_URL is not defined'); +} +const apolloL2GatewaysClient = process.env.L2_GATEWAY_SUBGRAPH_URL; + +if (!process.env.L2_GATEWAY_SEPOLIA_SUBGRAPH_URL) { + throw new Error('process.env.L2_GATEWAY_SEPOLIA_SUBGRAPH_URL is not defined'); +} const apolloL2GatewaysSepoliaClient = - 'https://api.thegraph.com/subgraphs/name/fionnachan/layer2-token-gateway-sepolia'; + process.env.L2_GATEWAY_SEPOLIA_SUBGRAPH_URL; const chainIdToGraphClientUrl = (chainID: string) => { switch (chainID) { case '42161': return apolloL2GatewaysClient; - case '421611': - return apolloL2GatewaysRinkebyClient; case '421614': return apolloL2GatewaysSepoliaClient; default: @@ -42,11 +44,6 @@ const isGraphTokenResult = (obj: GraphTokenResult) => { } }; -/** 421613 subgraph uses a different field name */ -const graphGatewayBlockNumField = (networkID: string | number) => { - return +networkID === 421613 ? 'l2BlockNum' : 'blockNum'; -}; - export const getTokens = async ( tokenList: { addr: string; logo: string | undefined }[], _networkID: string | number, @@ -74,7 +71,6 @@ export const getTokens = async ( const formattedAddresses = tokenList .map((token) => `"${token.addr}"`.toLowerCase()) .join(','); - const blockNumber = graphGatewayBlockNumField(_networkID); const query = gql` { tokens(first: 500, skip: 0, where:{ @@ -83,11 +79,11 @@ export const getTokens = async ( l1TokenAddr: id joinTableEntry: gateway( first: 1 - orderBy: ${blockNumber} + orderBy: l2BlockNum orderDirection: desc ) { id - ${blockNumber} + l2BlockNum token { tokenAddr: id } @@ -113,18 +109,17 @@ export const getAllTokens = async ( const networkID = typeof _networkID === 'number' ? _networkID.toString() : _networkID; const clientUrl = chainIdToGraphClientUrl(networkID); - const blockNumber = graphGatewayBlockNumField(_networkID); const query = gql` { tokens(first: 500, skip: 0) { l1TokenAddr: id joinTableEntry: gateway( first: 1 - orderBy: ${blockNumber} + orderBy: l2BlockNum orderDirection: desc ) { id - ${blockNumber} + l2BlockNum token { tokenAddr: id } diff --git a/src/main.ts b/src/main.ts index 5387847..368c84c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import { yargsInstance } from './lib/options'; -import * as dotenv from 'dotenv'; -import dotenvExpand from 'dotenv-expand'; + +import './init'; import { command as commandUpdate, describe as describeUpdate, @@ -22,10 +22,6 @@ import { describe as describeAllTokensList, handler as handlerAllTokensList, } from './commands/allTokensList'; -import './customNetworks'; - -const myEnv = dotenv.config(); -dotenvExpand.expand(myEnv); const update = yargsInstance.command( commandUpdate,