import dotenv from 'dotenv';
import { z } from 'zod';
import { type Hex } from 'viem';
// Load environment variables from .env file
dotenv.config();
// Define environment variable schema
const envSchema = z.object({
PRIVATE_KEY: z.string().optional(),
COINGECKO_PRO_API_KEY: z.string().optional(),
SEITRACE_API_KEY: z.string().optional(),
OPENSEA_API_KEY: z.string().optional(),
HIVE_INTELLIGENCE_API_KEY: z.string().optional(),
});
// Parse and validate environment variables
const env = envSchema.safeParse(process.env);
// Format private key with 0x prefix if it exists
const formatPrivateKey = (key?: string): string | undefined => {
if (!key) return undefined;
// Ensure the private key has 0x prefix
return key.startsWith('0x') ? key : `0x${key}`;
};
/**
* Check if a value is a valid API key (not a placeholder)
*/
const isValidApiKey = (key?: string): boolean => {
if (!key) return false;
// Common placeholder values that should be treated as invalid
const placeholders = [
'your_key_here',
'your_api_key_here',
'INSERT_API_KEY_HERE',
'api_key_here',
'replace_with_your_api_key',
'your_coingecko_api_key_here',
'your_seitrace_api_key_here',
'your_opensea_api_key_here',
'your_hive_intelligence_api_key_here',
];
return !placeholders.includes(key.toLowerCase()) && key.length > 10;
};
// Export validated environment variables with formatted private key
export const config = {
privateKey: env.success ? formatPrivateKey(env.data.PRIVATE_KEY) : undefined,
coinGeckoApiKey: env.success && isValidApiKey(env.data.COINGECKO_PRO_API_KEY) ? env.data.COINGECKO_PRO_API_KEY : undefined,
seiTraceApiKey: env.success && isValidApiKey(env.data.SEITRACE_API_KEY) ? env.data.SEITRACE_API_KEY : undefined,
openSeaApiKey: env.success && isValidApiKey(env.data.OPENSEA_API_KEY) ? env.data.OPENSEA_API_KEY : undefined,
hiveIntelligenceApiKey: env.success && isValidApiKey(env.data.HIVE_INTELLIGENCE_API_KEY) ? env.data.HIVE_INTELLIGENCE_API_KEY : undefined,
};
/**
* Get the private key from environment variable as a Hex type for viem.
* Returns undefined if the PRIVATE_KEY environment variable is not set.
* @returns Private key from environment variable as Hex or undefined
*/
export function getPrivateKeyAsHex(): Hex | undefined {
return config.privateKey as Hex | undefined;
}
/**
* Get the CoinGecko Pro API key from environment variable.
* Returns undefined if the COINGECKO_PRO_API_KEY environment variable is not set.
* @returns CoinGecko Pro API key or undefined
*/
export function getCoinGeckoApiKey(): string | undefined {
return config.coinGeckoApiKey;
}
/**
* Get the SeiTrace API key from environment variable.
* Returns undefined if the SEITRACE_API_KEY environment variable is not set.
* @returns SeiTrace API key or undefined
*/
export function getSeiTraceApiKey(): string | undefined {
return config.seiTraceApiKey;
}
/**
* Get the OpenSea API key from environment variable.
* Returns undefined if the OPENSEA_API_KEY environment variable is not set.
* @returns OpenSea API key or undefined
*/
export function getOpenSeaApiKey(): string | undefined {
return config.openSeaApiKey;
}
/**
* Get the Hive Intelligence API key from environment variable.
* Returns undefined if the HIVE_INTELLIGENCE_API_KEY environment variable is not set.
* @returns Hive Intelligence API key or undefined
*/
export function getHiveIntelligenceApiKey(): string | undefined {
return config.hiveIntelligenceApiKey;
}