#!/usr/bin/env tsx /** * Environment Variable Checker * * This script checks that all required environment variables are set * and validates RPC URLs are accessible. * * Usage: * tsx scripts/check-env.ts */ // Load environment variables FIRST import dotenv from 'dotenv'; dotenv.config(); import { createPublicClient, http } from 'viem'; import { mainnet, base, arbitrum, optimism, polygon } from 'viem/chains'; import chalk from 'chalk'; interface EnvCheck { name: string; value: string | undefined; required: boolean; valid: boolean; error?: string; } async function checkRpcUrl(name: string, url: string | undefined, chain: any): Promise { const check: EnvCheck = { name, value: url ? (url.length > 50 ? `${url.substring(0, 30)}...${url.substring(url.length - 10)}` : url) : undefined, required: false, valid: false, }; if (!url) { check.error = 'Not set (using default or will fail)'; return check; } if (url.includes('YOUR_KEY') || url.includes('YOUR_INFURA_KEY')) { check.error = 'Contains placeholder - please set a real RPC URL'; return check; } try { const client = createPublicClient({ chain, transport: http(url, { timeout: 5000 }), }); const blockNumber = await client.getBlockNumber(); check.valid = true; check.error = `✓ Connected (block: ${blockNumber})`; } catch (error: any) { check.error = `Connection failed: ${error.message}`; } return check; } async function main() { console.log(chalk.blue('='.repeat(60))); console.log(chalk.blue('Environment Variable Checker')); console.log(chalk.blue('='.repeat(60))); console.log(''); const checks: EnvCheck[] = []; // Check RPC URLs console.log(chalk.yellow('Checking RPC URLs...')); console.log(''); checks.push(await checkRpcUrl('MAINNET_RPC_URL', process.env.MAINNET_RPC_URL, mainnet)); checks.push(await checkRpcUrl('BASE_RPC_URL', process.env.BASE_RPC_URL, base)); checks.push(await checkRpcUrl('ARBITRUM_RPC_URL', process.env.ARBITRUM_RPC_URL, arbitrum)); checks.push(await checkRpcUrl('OPTIMISM_RPC_URL', process.env.OPTIMISM_RPC_URL, optimism)); checks.push(await checkRpcUrl('POLYGON_RPC_URL', process.env.POLYGON_RPC_URL, polygon)); // Check other variables console.log(chalk.yellow('Checking other environment variables...')); console.log(''); const privateKey = process.env.PRIVATE_KEY; checks.push({ name: 'PRIVATE_KEY', value: privateKey ? '***' + privateKey.slice(-4) : undefined, required: false, valid: !!privateKey, error: privateKey ? '✓ Set (not shown for security)' : 'Not set (optional, only needed for mainnet transactions)', }); // Print results console.log(chalk.blue('='.repeat(60))); console.log(chalk.blue('Results')); console.log(chalk.blue('='.repeat(60))); console.log(''); let hasErrors = false; let hasWarnings = false; for (const check of checks) { const status = check.valid ? chalk.green('✓') : (check.required ? chalk.red('✗') : chalk.yellow('⚠')); const name = chalk.bold(check.name); const value = check.value ? chalk.gray(`(${check.value})`) : ''; const error = check.error ? ` - ${check.error}` : ''; console.log(`${status} ${name} ${value}${error}`); if (!check.valid) { if (check.required) { hasErrors = true; } else { hasWarnings = true; } } // Check for placeholder values if (check.value && (check.value.includes('YOUR_KEY') || check.value.includes('YOUR_INFURA_KEY'))) { hasWarnings = true; console.log(chalk.yellow(` ⚠ Contains placeholder - please set a real value`)); } } console.log(''); console.log(chalk.blue('='.repeat(60))); if (hasErrors) { console.log(chalk.red('✗ Some required checks failed')); console.log(''); console.log('Please:'); console.log(' 1. Copy .env.example to .env'); console.log(' 2. Fill in your RPC URLs'); console.log(' 3. Run this script again to verify'); process.exit(1); } else if (hasWarnings) { console.log(chalk.yellow('⚠ Some checks have warnings')); console.log(''); console.log('Recommendations:'); console.log(' - Set RPC URLs in .env file for better performance'); console.log(' - Replace placeholder values with real RPC URLs'); console.log(' - Check RPC provider settings if connections fail'); console.log(''); console.log('You can still run tests, but they may fail if RPC URLs are not properly configured.'); } else { console.log(chalk.green('✓ All checks passed!')); console.log(''); console.log('You can now run:'); console.log(' - pnpm run strat run scenarios/aave/leveraged-long.yml'); console.log(' - pnpm run strat:test'); } } main().catch(console.error);