Skip to content

Commit

Permalink
Update environment variable usage
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 14, 2024
1 parent 4b07954 commit eca0750
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 20 deletions.
6 changes: 2 additions & 4 deletions aggregators/zrx/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GraphQLClient, gql } from 'graphql-request';
import { getUniqStartOfTodayTimestamp } from '../../helpers/getUniSubgraphVolume';
import { getEnv } from '../../helpers/env';

const CHAINS = [
'Arbitrum',
Expand All @@ -15,10 +16,7 @@ const CHAINS = [

const graphQLClient = new GraphQLClient('https://api.0x.org/data/v0');
const getGQLClient = () => {
graphQLClient.setHeader(
'0x-api-key',
process.env.ZEROx_API_KEY ?? ''
);
graphQLClient.setHeader('0x-api-key', getEnv('ZEROX_API_KEY'));
return graphQLClient;
};

Expand Down
3 changes: 2 additions & 1 deletion dexs/0x/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql, GraphQLClient } from "graphql-request";
import { BreakdownAdapter, Fetch, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getEnv } from "../../helpers/env";

const chains = [CHAIN.ETHEREUM, CHAIN.POLYGON]

Expand Down Expand Up @@ -32,7 +33,7 @@ const getHistoricalDataQuery = (timestamp: number) => {

const graphQLClient = new GraphQLClient("https://api.0x.org/data/v0");
const getGQLClient = () => {
graphQLClient.setHeader("0x-api-key", process.env.ZEROx_API_KEY ?? process.env.ZEROX_API_KEY ?? '')
graphQLClient.setHeader("0x-api-key", getEnv('ZEROX_API_KEY'))
return graphQLClient
}

Expand Down
3 changes: 2 additions & 1 deletion dexs/SmarDex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
} from "../../helpers/getUniSubgraph";
import { CHAIN } from "../../helpers/chains";
import { Chain } from "@defillama/sdk/build/general";
import { getEnv } from "../../helpers/env";

const SMARDEX_SUBGRAPH_API_KEY = process.env.SMARDEX_SUBGRAPH_API_KEY;
const SMARDEX_SUBGRAPH_API_KEY = getEnv('SMARDEX_SUBGRAPH_API_KEY');
const SMARDEX_SUBGRAPH_GATEWAY = "https://subgraph.smardex.io/defillama";

// if (!SMARDEX_SUBGRAPH_API_KEY) {
Expand Down
3 changes: 2 additions & 1 deletion dexs/perennial-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import request, { gql } from 'graphql-request'
import { BreakdownAdapter, Fetch, SimpleAdapter } from '../../adapters/types'
import { CHAIN } from '../../helpers/chains'
import { getUniqStartOfTodayTimestamp } from '../../helpers/getUniSubgraphVolume'
import { getEnv } from '../../helpers/env'

const apiKey = process.env.PERENNIAL_V2_SUBGRAPH_API_KEY
const apiKey = getEnv('PERENNIAL_V2_SUBGRAPH_API_KEY')
const graphUrls: { [key: string]: string } = {
[CHAIN.ARBITRUM]: `https://subgraph.satsuma-prod.com/${apiKey}/equilibria/perennial-v2-arbitrum/api`,
}
Expand Down
3 changes: 2 additions & 1 deletion dexs/serum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DISABLED_ADAPTER_KEY, SimpleAdapter } from "../../adapters/types";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
import { gql, GraphQLClient } from "graphql-request";
import disabledAdapter from "../../helpers/disabledAdapter";
import { getEnv } from "../../helpers/env";

const endpoint = "https://api.vybenetwork.com/v1/graphql";

Expand All @@ -18,7 +19,7 @@ const query = gql`

const graphQLClient = new GraphQLClient(endpoint);
const getGQLClient = () => {
graphQLClient.setHeader("authorization", process.env.PROD_VYBE_API_KEY ?? '')
graphQLClient.setHeader("authorization", getEnv('PROD_VYBE_API_KEY'))
return graphQLClient
}

Expand Down
3 changes: 2 additions & 1 deletion fees/SmarDex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { request, gql } from "graphql-request";
import BigNumber from "bignumber.js";

import { getTimestampAtStartOfDayUTC } from "../../utils/date";
import { getEnv } from "../../helpers/env";

const SMARDEX_SUBGRAPH_API_KEY = process.env.SMARDEX_SUBGRAPH_API_KEY || "";
const SMARDEX_SUBGRAPH_API_KEY = getEnv('SMARDEX_SUBGRAPH_API_KEY')
const SMARDEX_SUBGRAPH_GATEWAY = "https://subgraph.smardex.io/defillama";

// Headers for GraphQL requests that require an API key
Expand Down
3 changes: 2 additions & 1 deletion fees/cardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IDate } from "../helpers/bitqueryFees";
import { CHAIN } from "../helpers/chains";
import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfNextDayUTC } from "../utils/date";
import { httpPost } from "../utils/fetchURL";
import { getEnv } from "../helpers/env";


interface ITxAda {
Expand All @@ -28,7 +29,7 @@ const adapterQuery = async (form: string, till: string, network: string): Promis
variables: value
});

const headers = {"X-API-KEY": process.env.BIT_QUERY_API_KEY || '', "Content-Type": "application/json"};
const headers = {"X-API-KEY": getEnv('BIT_QUERY_API_KEY'), "Content-Type": "application/json"};
const result: ITxAda[] = (await httpPost("https://graphql.bitquery.io", body, { headers: headers }))?.data.cardano.transactions;

return result;
Expand Down
3 changes: 2 additions & 1 deletion helpers/allium.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import retry from "async-retry";
import { IJSON } from "../adapters/types";
import { httpGet, httpPost } from "../utils/fetchURL";
import { getEnv } from "./env";

const token = {} as IJSON<string>

const HEADERS = {
"Content-Type": "application/json",
"X-API-KEY": process.env.ALLIUM_API_KEY!,
"X-API-KEY": getEnv('ALLIUM_API_KEY'),
};

export async function startAlliumQuery(sqlQuery: string) {
Expand Down
3 changes: 2 additions & 1 deletion helpers/bitqueryFees.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { httpPost } from "../utils/fetchURL";
import { getEnv } from "./env";

interface IDate {
date: string; // ex. 2022-01-01
Expand Down Expand Up @@ -29,7 +30,7 @@ const adapterBitqueryFeesEthereumNetwork = async (form: string, till: string, ne
variables: value
});

const headers = {"X-API-KEY": process.env.BIT_QUERY_API_KEY || '', "Content-Type": "application/json"};
const headers = {"X-API-KEY": getEnv('BIT_QUERY_API_KEY'), "Content-Type": "application/json"};
const result: ITx[] = (await httpPost("https://graphql.bitquery.io", body, { headers: headers }))?.data.ethereum.transactions;

return result;
Expand Down
4 changes: 2 additions & 2 deletions helpers/dune.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import retry from "async-retry";
import { IJSON } from "../adapters/types";
import { httpGet, httpPost } from "../utils/fetchURL";
import { getEnv } from "./env";

const token = {} as IJSON<string>
const API_KEYS = process.env.DUNE_API_KEYS?.split(',') ?? ["L0URsn5vwgyrWbBpQo9yS1E3C1DBJpZh"]
const API_KEYS =getEnv('DUNE_API_KEYS').split(',') ?? ["L0URsn5vwgyrWbBpQo9yS1E3C1DBJpZh"]
let API_KEY_INDEX = 0;
const MAX_RETRIES = 20;

export async function queryDune(queryId: string, query_parameters = {}) {
/* const error = new Error("Dune: queryId is required")
Expand Down
3 changes: 2 additions & 1 deletion helpers/duneRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import retry from "async-retry";
import fetchURL from "../utils/fetchURL";
import { getEnv } from "./env";

const API_KEYS = process.env.DUNE_API_KEYS?.split(",") ?? [];
const API_KEYS = getEnv('DUNE_API_KEYS').split(",") ?? [];
type IRequest = {
[key: string]: Promise<any>;
}
Expand Down
39 changes: 39 additions & 0 deletions helpers/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const BOOL_KEYS = [
'LLAMA_DEBUG_MODE',
]

const DEFAULTS: any = {
ANKR_API_KEY: '79258ce7f7ee046decc3b5292a24eb4bf7c910d7e39b691384c7ce0cfb839a01',
ZETA_RPC: "https://zetachain-evm.blockpi.network/v1/rpc/public,https://zetachain-mainnet-archive.allthatnode.com:8545"
}

export const ENV_KEYS = new Set([
...BOOL_KEYS,
...Object.keys(DEFAULTS),
'PANCAKESWAP_OPBNB_SUBGRAPH',
'INDEXA_DB',
'FLIPSIDE_API_KEY',
'DUNE_API_KEYS',
'ALLIUM_API_KEY',
'BIT_QUERY_API_KEY',
'SMARDEX_SUBGRAPH_API_KEY',
'PROD_VYBE_API_KEY',
'PERENNIAL_V2_SUBGRAPH_API_KEY',
'ZEROx_API_KEY',
'ZEROX_API_KEY'
])

// This is done to support both ZEROx_API_KEY and ZEROX_API_KEY
if (!process.env.ZEROX_API_KEY) process.env.ZEROX_API_KEY = process.env.ZEROx_API_KEY

Object.keys(DEFAULTS).forEach(i => {
if (!process.env[i]) process.env[i] = DEFAULTS[i] // this is done to set the chain RPC details in @defillama/sdk
})


export function getEnv(key: string): any {
if (!ENV_KEYS.has(key)) throw new Error(`Unknown env key: ${key}`)
const value = process.env[key] ?? DEFAULTS[key]
return BOOL_KEYS.includes(key) ? !!value : value
}

3 changes: 2 additions & 1 deletion helpers/flipsidecrypto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import retry from "async-retry";
import { IJSON } from "../adapters/types";
import { httpPost } from "../utils/fetchURL";
import { getEnv } from "./env";

const token = {} as IJSON<string>
const FLIPSIDE_API_KEYS = process.env.FLIPSIDE_API_KEY?.split(',') ?? ["f3b65679-a179-4983-b794-e41cf40103ed"]
const FLIPSIDE_API_KEYS = getEnv('FLIPSIDE_API_KEY').split(',') ?? ["f3b65679-a179-4983-b794-e41cf40103ed"]
let API_KEY_INDEX = 0;
const MAX_RETRIES = 20;

Expand Down
3 changes: 2 additions & 1 deletion helpers/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import { Sequelize } from 'sequelize';
import { FetchOptions } from '../adapters/types';
const dbString = process.env.INDEXA_DB!;
import { getEnv } from './env';
const dbString = getEnv('INDEXA_DB')

let connection: Sequelize;

Expand Down
3 changes: 2 additions & 1 deletion helpers/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import axios from 'axios'
import { getCache, setCache } from "./cache";
import { ethers } from "ethers";
import { getUniqueAddresses } from '@defillama/sdk/build/generalUtil';
import { getEnv } from './env';

export const nullAddress = ADDRESSES.null

Expand Down Expand Up @@ -136,7 +137,7 @@ async function ankrGetTokens(address: string, { onlyWhitelisted = true }: {

const options = {
method: 'POST',
url: `https://rpc.ankr.com/multichain/${process.env.ANKR_API_KEY}`,
url: `https://rpc.ankr.com/multichain/${getEnv('ANKR_API_KEY')}`,
headers: { accept: 'application/json', 'content-type': 'application/json' },
data: {
jsonrpc: '2.0',
Expand Down
5 changes: 3 additions & 2 deletions protocols/pancakeswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import disabledAdapter from "../../helpers/disabledAdapter";
import { getGraphDimensions } from "../../helpers/getUniSubgraph"
import * as sdk from "@defillama/sdk";
import { httpGet } from "../../utils/fetchURL";
import { getEnv } from "../../helpers/env";

const endpoints = {
[CHAIN.BSC]: "https://proxy-worker.pancake-swap.workers.dev/bsc-exchange",
Expand All @@ -15,7 +16,7 @@ const endpoints = {
[CHAIN.ARBITRUM]: "https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v2-arb",
[CHAIN.LINEA]: "https://graph-query.linea.build/subgraphs/name/pancakeswap/exhange-v2",
[CHAIN.BASE]: "https://api.studio.thegraph.com/query/45376/exchange-v2-base/version/latest",
[CHAIN.OP_BNB]: `${process.env.PANCAKESWAP_OPBNB_SUBGRAPH}/subgraphs/name/pancakeswap/exchange-v2`
[CHAIN.OP_BNB]: `${getEnv('PANCAKESWAP_OPBNB_SUBGRAPH')}/subgraphs/name/pancakeswap/exchange-v2`
};

const stablesSwapEndpoints = {
Expand All @@ -30,7 +31,7 @@ const v3Endpoint = {
[CHAIN.ARBITRUM]: "https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-arb",
[CHAIN.LINEA]: "https://graph-query.linea.build/subgraphs/name/pancakeswap/exchange-v3-linea",
[CHAIN.BASE]: "https://api.studio.thegraph.com/query/45376/exchange-v3-base/version/latest",
[CHAIN.OP_BNB]: `${process.env.PANCAKESWAP_OPBNB_SUBGRAPH}/subgraphs/name/pancakeswap/exchange-v3`
[CHAIN.OP_BNB]: `${getEnv('PANCAKESWAP_OPBNB_SUBGRAPH')}/subgraphs/name/pancakeswap/exchange-v3`
}

const VOLUME_USD = "volumeUSD";
Expand Down

0 comments on commit eca0750

Please sign in to comment.