From 6501855231de146cf09984295ac743bef7d02f4d Mon Sep 17 00:00:00 2001 From: Vincent Taglia Date: Fri, 25 Oct 2024 15:39:13 -0500 Subject: [PATCH] feat: set API key via single env variable --- .env.example | 10 ++-- docker-entrypoint.sh | 22 ++++---- indexer-tools-config.json.example | 10 ++-- src/plugins/defaultsConfig.js | 63 +++++++++-------------- src/plugins/graphNetworkSubgraphClient.js | 10 ++-- src/store/allocations.js | 5 ++ src/store/network.js | 5 ++ src/store/subgraphs.js | 5 ++ 8 files changed, 68 insertions(+), 62 deletions(-) diff --git a/.env.example b/.env.example index e74a957..ed1f06f 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,9 @@ VITE_DEFAULT_RPC_MAINNET="https://mainnet.infura.io/v3/659344f230804542a4e653f87 VITE_DEFAULT_RPC_ARBITRUM="https://arb-mainnet.g.alchemy.com/v2/er8LBcXpoFwlV8xJee-WXFbFG_M8L4JK" VITE_DEFAULT_RPC_SEPOLIA="https://eth-sepolia.g.alchemy.com/v2/eKJ8_13LMaVi2bSITYWTMqskgsGiey8K" VITE_DEFAULT_RPC_ARBITRUM_SEPOLIA="https://arbitrum-sepolia.infura.io/v3/db92de7c459f4d83a2c2c21931a6bdf0" -VITE_DEFAULT_SUBGRAPH_MAINNET="https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8" -VITE_DEFAULT_SUBGRAPH_ARBITRUM="https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp" -VITE_DEFAULT_SUBGRAPH_SEPOLIA="https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC" -VITE_DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA="https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV" \ No newline at end of file +VITE_DEFAULT_SUBGRAPH_MAINNET="https://gateway.thegraph.com/api/[api-key]/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8" +VITE_DEFAULT_SUBGRAPH_ARBITRUM="https://gateway.thegraph.com/api/[api-key]/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp" +VITE_DEFAULT_SUBGRAPH_SEPOLIA="https://gateway.thegraph.com/api/[api-key]/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC" +VITE_DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA="https://gateway.thegraph.com/api/[api-key]/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV" +VITE_DEFAULT_QOS_SUBGRAPH="https://gateway.thegraph.com/api/[api-key]/subgraphs/id/Dtr9rETvwokot4BSXaD5tECanXfqfJKcvHuaaEgPDD2D" +VITE_GRAPH_API_KEY="3bab348a7c385b1870039eb890fd0a5f" \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 0add2f0..0b5049b 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,17 +1,17 @@ #!/bin/sh -NO_ENV_ACCOUNTS='[ { "address": "0x1b92e4cba0f82c85c1298af861247849988c788c", "name": "vincenttaglia-indexer.eth", "active": true, "chain": "arbitrum-one", "agentConnect": false, "agentEndpoint": "", "poiQuery": false, "poiQueryEndpoint": "" } ]' - JSON_STRING="{ - \"DEFAULT_ACCOUNTS\": ${DEFAULT_ACCOUNTS:-$NO_ENV_ACCOUNTS}, - \"DEFAULT_RPC_MAINNET\": \"${DEFAULT_RPC_MAINNET:-https://mainnet.infura.io/v3/659344f230804542a4e653f875172105}\", - \"DEFAULT_RPC_ARBITRUM\": \"${DEFAULT_RPC_ARBITRUM:-https://arb-mainnet.g.alchemy.com/v2/er8LBcXpoFwlV8xJee-WXFbFG_M8L4JK}\", - \"DEFAULT_RPC_SEPOLIA\": \"${DEFAULT_RPC_SEPOLIA:-https://eth-sepolia.g.alchemy.com/v2/eKJ8_13LMaVi2bSITYWTMqskgsGiey8K}\", - \"DEFAULT_RPC_ARBITRUM_SEPOLIA\": \"${DEFAULT_RPC_ARBITRUM_SEPOLIA:-https://arbitrum-sepolia.infura.io/v3/db92de7c459f4d83a2c2c21931a6bdf0}\", - \"DEFAULT_SUBGRAPH_MAINNET\": \"${DEFAULT_SUBGRAPH_MAINNET:-https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8}\", - \"DEFAULT_SUBGRAPH_ARBITRUM\": \"${DEFAULT_SUBGRAPH_ARBITRUM:-https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp}\", - \"DEFAULT_SUBGRAPH_SEPOLIA\": \"${DEFAULT_SUBGRAPH_SEPOLIA:-https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC}\", - \"DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA\": \"${DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA:-https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV}\" + \"DEFAULT_ACCOUNTS\": ${DEFAULT_ACCOUNTS:-\"\"}, + \"DEFAULT_RPC_MAINNET\": \"${DEFAULT_RPC_MAINNET}\", + \"DEFAULT_RPC_ARBITRUM\": \"${DEFAULT_RPC_ARBITRUM}\", + \"DEFAULT_RPC_SEPOLIA\": \"${DEFAULT_RPC_SEPOLIA}\", + \"DEFAULT_RPC_ARBITRUM_SEPOLIA\": \"${DEFAULT_RPC_ARBITRUM_SEPOLIA}\", + \"DEFAULT_SUBGRAPH_MAINNET\": \"${DEFAULT_SUBGRAPH_MAINNET}\", + \"DEFAULT_SUBGRAPH_ARBITRUM\": \"${DEFAULT_SUBGRAPH_ARBITRUM}\", + \"DEFAULT_SUBGRAPH_SEPOLIA\": \"${DEFAULT_SUBGRAPH_SEPOLIA}\", + \"DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA\": \"${DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA}\", + \"DEFAULT_QOS_SUBGRAPH\": \"${DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA}\", + \"GRAPH_API_KEY\": \"${GRAPH_API_KEY}\" }" echo $JSON_STRING > /app/public/indexer-tools-config.json diff --git a/indexer-tools-config.json.example b/indexer-tools-config.json.example index 7a8716f..668864d 100644 --- a/indexer-tools-config.json.example +++ b/indexer-tools-config.json.example @@ -4,8 +4,10 @@ "DEFAULT_RPC_ARBITRUM": "https://arb-mainnet.g.alchemy.com/v2/er8LBcXpoFwlV8xJee-WXFbFG_M8L4JK", "DEFAULT_RPC_SEPOLIA": "https://eth-sepolia.g.alchemy.com/v2/eKJ8_13LMaVi2bSITYWTMqskgsGiey8K" "DEFAULT_RPC_ARBITRUM_SEPOLIA": "https://arbitrum-sepolia.infura.io/v3/db92de7c459f4d83a2c2c21931a6bdf0" - "DEFAULT_SUBGRAPH_MAINNET": "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8" - "DEFAULT_SUBGRAPH_ARBITRUM": "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp" - "DEFAULT_SUBGRAPH_SEPOLIA": "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC" - "DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA": "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV" + "DEFAULT_SUBGRAPH_MAINNET": "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8", + "DEFAULT_SUBGRAPH_ARBITRUM": "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp", + "DEFAULT_SUBGRAPH_SEPOLIA": "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC", + "DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA": "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV", + "DEFAULT_QOS_SUBGRAPH": "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/Dtr9rETvwokot4BSXaD5tECanXfqfJKcvHuaaEgPDD2D", + "DEFAULT_GRAPH_API_KEY": "3bab348a7c385b1870039eb890fd0a5f" } \ No newline at end of file diff --git a/src/plugins/defaultsConfig.js b/src/plugins/defaultsConfig.js index 60f2429..f89a63f 100644 --- a/src/plugins/defaultsConfig.js +++ b/src/plugins/defaultsConfig.js @@ -15,52 +15,39 @@ export const defaultsConfig = { */ export const loadDefaultsConfig = async () => { const resp = await fetch('/indexer-tools-config.json'); + let value; + let json; if(resp.ok){ - const value = await resp.json() - - return { - variables: { - accounts: value.DEFAULT_ACCOUNTS, - rpcMainnet: value.DEFAULT_RPC_MAINNET, - rpcArbitrum: value.DEFAULT_RPC_ARBITRUM, - rpcSepolia: value.DEFAULT_RPC_SEPOLIA, - rpcArbitrumSepolia: value.DEFAULT_RPC_ARBITRUM_SEPOLIA, - subgraphMainnet: value.DEFAULT_SUBGRAPH_MAINNET, - subgraphArbitrum: value.DEFAULT_SUBGRAPH_ARBITRUM, - subgraphSepolia: value.DEFAULT_SUBGRAPH_SEPOLIA, - subgraphArbitrumSepolia: value.DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA, - } - } + value = await resp.json(); + }else{ + value = {}; + } - }else if(import.meta.env.VITE_DEFAULT_ACCOUNTS != undefined){ - return { - variables: { - accounts: JSON.parse(import.meta.env.VITE_DEFAULT_ACCOUNTS), - rpcMainnet: import.meta.env.VITE_DEFAULT_RPC_MAINNET, - rpcArbitrum: import.meta.env.VITE_DEFAULT_RPC_ARBITRUM, - rpcSepolia: import.meta.env.VITE_DEFAULT_RPC_SEPOLIA, - rpcArbitrumSepolia: import.meta.env.VITE_DEFAULT_RPC_ARBITRUM_SEPOLIA, - subgraphMainnet: import.meta.env.VITE_DEFAULT_SUBGRAPH_MAINNET, - subgraphArbitrum: import.meta.env.VITE_DEFAULT_SUBGRAPH_ARBITRUM, - subgraphSepolia: import.meta.env.VITE_DEFAULT_SUBGRAPH_SEPOLIA, - subgraphArbitrumSepolia: import.meta.env.VITE_DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA, - } - } + if(import.meta.env.VITE_DEFAULT_ACCOUNTS){ + json = JSON.parse(import.meta.env.VITE_DEFAULT_ACCOUNTS); + }else{ + json = ""; } return { variables: { - accounts: [ { "address": "0x1b92e4cba0f82c85c1298af861247849988c788c", "name": "vincenttaglia-indexer.eth", "active": true, "chain": "arbitrum-one", "agentConnect": false, "agentEndpoint": "", "poiQuery": false, "poiQueryEndpoint": "" } ], - rpcMainnet: "https://mainnet.infura.io/v3/659344f230804542a4e653f875172105", - rpcArbitrum: "https://arb-mainnet.g.alchemy.com/v2/er8LBcXpoFwlV8xJee-WXFbFG_M8L4JK", - rpcSepolia: "https://eth-sepolia.g.alchemy.com/v2/eKJ8_13LMaVi2bSITYWTMqskgsGiey8K", - rpcArbitrumSepolia: "https://arbitrum-sepolia.infura.io/v3/db92de7c459f4d83a2c2c21931a6bdf0", - subgraphMainnet: "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8", - subgraphArbitrum: "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp", - subgraphSepolia: "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC", - subgraphArbitrumSepolia: "https://gateway-arbitrum.network.thegraph.com/api/146d8cd439901e24257f3c19d82359da/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV", + accounts: value.DEFAULT_ACCOUNTS || json || [ { "address": "0x1b92e4cba0f82c85c1298af861247849988c788c", "name": "vincenttaglia-indexer.eth", "active": true, "chain": "arbitrum-one", "agentConnect": false, "agentEndpoint": "", "poiQuery": false, "poiQueryEndpoint": "" } ], + rpcMainnet: value.DEFAULT_RPC_MAINNET || import.meta.env.VITE_DEFAULT_RPC_MAINNET || "https://mainnet.infura.io/v3/659344f230804542a4e653f875172105", + rpcArbitrum: value.DEFAULT_RPC_ARBITRUM || import.meta.env.VITE_DEFAULT_RPC_ARBITRUM || "https://arb-mainnet.g.alchemy.com/v2/er8LBcXpoFwlV8xJee-WXFbFG_M8L4JK", + rpcSepolia: value.DEFAULT_RPC_SEPOLIA || import.meta.env.VITE_DEFAULT_RPC_SEPOLIA || "https://eth-sepolia.g.alchemy.com/v2/eKJ8_13LMaVi2bSITYWTMqskgsGiey8K", + rpcArbitrumSepolia: value.DEFAULT_RPC_ARBITRUM_SEPOLIA || import.meta.env.VITE_DEFAULT_RPC_ARBITRUM_SEPOLIA || "https://arbitrum-sepolia.infura.io/v3/db92de7c459f4d83a2c2c21931a6bdf0", + subgraphMainnet: value.DEFAULT_SUBGRAPH_MAINNET || import.meta.env.VITE_DEFAULT_SUBGRAPH_MAINNET || "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/9Co7EQe5PgW3ugCUJrJgRv4u9zdEuDJf8NvMWftNsBH8", + subgraphArbitrum: value.DEFAULT_SUBGRAPH_ARBITRUM || import.meta.env.VITE_DEFAULT_SUBGRAPH_ARBITRUM || "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp", + subgraphSepolia: value.DEFAULT_SUBGRAPH_SEPOLIA || import.meta.env.VITE_DEFAULT_SUBGRAPH_SEPOLIA || "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/8pVKDwHniAz87CHEQsiz2wgFXGZXrbMDkrxgauVVfMJC", + subgraphArbitrumSepolia: value.DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA || import.meta.env.VITE_DEFAULT_SUBGRAPH_ARBITRUM_SEPOLIA || "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV", + qosSubgraph: value.DEFAULT_QOS_SUBGRAPH || import.meta.env.VITE_DEFAULT_QOS_SUBGRAPH || "https://gateway.thegraph.com/api/[api-key]/subgraphs/id/Dtr9rETvwokot4BSXaD5tECanXfqfJKcvHuaaEgPDD2D", + apiKey: value.GRAPH_API_KEY || import.meta.env.VITE_GRAPH_API_KEY || "3bab348a7c385b1870039eb890fd0a5f", } } +} + +export function replaceAPI(string, apiKey){ + return string.replace("[api-key]", apiKey).replace("{api-key}", apiKey); } \ No newline at end of file diff --git a/src/plugins/graphNetworkSubgraphClient.js b/src/plugins/graphNetworkSubgraphClient.js index 0b93927..373c052 100644 --- a/src/plugins/graphNetworkSubgraphClient.js +++ b/src/plugins/graphNetworkSubgraphClient.js @@ -1,4 +1,4 @@ -import { loadDefaultsConfig } from "./defaultsConfig"; +import { loadDefaultsConfig, replaceAPI } from "./defaultsConfig"; import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core' const defaultsConfigVariables = await loadDefaultsConfig(); @@ -8,22 +8,22 @@ console.log(defaultsConfig.subgraphArbitrum); // HTTP connection to the API const httpLink = createHttpLink({ // You should use an absolute URL here - uri: defaultsConfig.subgraphMainnet, + uri: replaceAPI(defaultsConfig.subgraphMainnet, defaultsConfig.apiKey), }); const arbitrumHttpLink = createHttpLink({ // You should use an absolute URL here - uri: defaultsConfig.subgraphArbitrum, + uri: replaceAPI(defaultsConfig.subgraphArbitrum, defaultsConfig.apiKey), }); const sepoliaHttpLink = createHttpLink({ // You should use an absolute URL here - uri: defaultsConfig.subgraphSepolia, + uri: replaceAPI(defaultsConfig.subgraphSepolia, defaultsConfig.apiKey), }); const arbitrumSepoliaHttpLink = createHttpLink({ // You should use an absolute URL here - uri: defaultsConfig.subgraphArbitrumSepolia, + uri: replaceAPI(defaultsConfig.subgraphArbitrumSepolia, defaultsConfig.apiKey), }); // Cache implementation diff --git a/src/store/allocations.js b/src/store/allocations.js index e0f8123..986b875 100644 --- a/src/store/allocations.js +++ b/src/store/allocations.js @@ -483,6 +483,11 @@ export const useAllocationStore = defineStore('allocationStore', { }) } return data; + }).catch((err) => { + if(err.graphQLErrors){ + this.loading = false; + alert(`API Error: ${err.graphQLErrors[0].message}`); + } }); } } diff --git a/src/store/network.js b/src/store/network.js index 8c6586c..7da2aa2 100644 --- a/src/store/network.js +++ b/src/store/network.js @@ -74,6 +74,11 @@ export const useNetworkStore = defineStore('network', { this.networks[chain.id].currentEpoch = data.data.graphNetwork.currentEpoch; this.networks[chain.id].issuancePerYear = data.data.graphNetwork.networkGRTIssuancePerBlock * chainStore.getBlocksPerYear; this.networks[chain.id].totalTokensAllocated = data.data.graphNetwork.totalTokensAllocated; + }).catch((err) => { + if(err.graphQLErrors){ + this.loading = false; + alert(`API Error: ${err.graphQLErrors[0].message}`); + } })); } return queries; diff --git a/src/store/subgraphs.js b/src/store/subgraphs.js index 59127eb..7f1ad78 100644 --- a/src/store/subgraphs.js +++ b/src/store/subgraphs.js @@ -430,6 +430,11 @@ export const useSubgraphsStore = defineStore({ } return data; + }).catch((err) => { + if(err.graphQLErrors){ + this.loading = false; + alert(`API Error: ${err.graphQLErrors[0].message}`); + } }); }, async fetchData(){