# โš™๏ธ Environment Setup Guide This guide explains how to set up environment variables for the DeFi Strategy Testing Framework. --- ## ๐Ÿš€ Quick Start ### 1๏ธโƒฃ Copy the Example Environment File ```bash cp .env.example .env ``` ### 2๏ธโƒฃ Fill in Your RPC URLs ```bash # Edit .env file MAINNET_RPC_URL=https://mainnet.infura.io/v3/YOUR_INFURA_KEY BASE_RPC_URL=https://base-mainnet.infura.io/v3/YOUR_INFURA_KEY # ... etc ``` ### 3๏ธโƒฃ Verify Your Setup ```bash pnpm run check:env ``` --- ## ๐Ÿ“‹ Required Environment Variables ### ๐Ÿ”— RPC URLs These are used to connect to blockchain networks for forking and testing: | Variable | Description | Required | |----------|-------------|----------| | `MAINNET_RPC_URL` | Ethereum mainnet RPC endpoint | โœ… Yes | | `BASE_RPC_URL` | Base network RPC endpoint | โš ๏ธ Optional | | `ARBITRUM_RPC_URL` | Arbitrum One RPC endpoint | โš ๏ธ Optional | | `OPTIMISM_RPC_URL` | Optimism network RPC endpoint | โš ๏ธ Optional | | `POLYGON_RPC_URL` | Polygon network RPC endpoint | โš ๏ธ Optional | ### ๐Ÿ” Optional Environment Variables | Variable | Description | When Needed | |----------|-------------|-------------| | `PRIVATE_KEY` | Private key for executing transactions | Mainnet/testnet execution only | | `TEST_SCENARIO` | Override default test scenario path | Custom test scenarios | | `TEST_NETWORK` | Override default test network | Multi-chain testing | --- ## ๐Ÿ”— Getting RPC URLs ### ๐Ÿ†“ Free Options #### 1. Public RPCs (Rate-Limited) | Network | Public RPC URL | |---------|----------------| | Ethereum | `https://eth.llamarpc.com` | | Base | `https://mainnet.base.org` | | Arbitrum | `https://arb1.arbitrum.io/rpc` | | Optimism | `https://mainnet.optimism.io` | | Polygon | `https://polygon-rpc.com` | #### 2. Infura (Free Tier) 1. ๐Ÿ“ Sign up at [infura.io](https://infura.io) 2. โž• Create a project 3. ๐Ÿ“‹ Copy your project ID 4. ๐Ÿ”— Use: `https://mainnet.infura.io/v3/YOUR_PROJECT_ID` #### 3. Alchemy (Free Tier) 1. ๐Ÿ“ Sign up at [alchemy.com](https://alchemy.com) 2. โž• Create an app 3. ๐Ÿ“‹ Copy your API key 4. ๐Ÿ”— Use: `https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY` ### ๐Ÿ’ฐ Paid Options (Recommended for Production) | Provider | Best For | Link | |----------|----------|------| | **Infura** | Reliable, well-known | [infura.io](https://infura.io) | | **Alchemy** | Fast, good free tier | [alchemy.com](https://alchemy.com) | | **QuickNode** | Fast, global network | [quicknode.com](https://quicknode.com) | | **Ankr** | Good performance | [ankr.com](https://ankr.com) | --- ## โœ… Verification ### ๐Ÿ” Check Environment Variables Run the environment checker: ```bash pnpm run check:env ``` This will: - โœ… Check that all RPC URLs are set - โœ… Verify connections to each network - โœ… Show current block numbers - โœ… Report any issues ### ๐Ÿงช Test with a Scenario ```bash # Set your RPC URL export MAINNET_RPC_URL=https://your-rpc-url-here # Run a test pnpm run strat:test ``` --- ## ๐Ÿ”ง Troubleshooting ### โŒ "RPC URL contains placeholder" **Problem:** Your `.env` file still has placeholder values like `YOUR_KEY` or `YOUR_INFURA_KEY`. **Solution:** Replace placeholders with actual RPC URLs in your `.env` file. ### โŒ "Connection failed" or "403 Forbidden" **Problem:** Your RPC endpoint is rejecting requests. **Possible Causes:** 1. โŒ Invalid API key 2. โฑ๏ธ Rate limiting (free tier exceeded) 3. ๐Ÿšซ IP restrictions 4. ๐Ÿ”’ Infura project set to "private key only" mode **Solutions:** 1. โœ… Verify your API key is correct 2. โœ… Check your RPC provider dashboard for rate limits 3. โœ… Try a different RPC provider 4. โœ… For Infura: Enable "Public Requests" in project settings ### โŒ "Environment variable not set" **Problem:** The script can't find the required environment variable. **Solutions:** 1. โœ… Check that `.env` file exists in project root 2. โœ… Verify variable name is correct (case-sensitive) 3. โœ… Restart your terminal/IDE after creating `.env` 4. โœ… Use `pnpm run check:env` to verify ### โŒ Module Load Order Issues **Problem:** Environment variables not being loaded before modules that use them. **Solution:** The framework now loads `dotenv` FIRST in all entry points. If you still have issues: 1. โœ… Ensure `.env` file is in the project root 2. โœ… Check that `dotenv` package is installed 3. โœ… Verify scripts load dotenv before other imports --- ## ๐Ÿ’ก Best Practices ### ๐Ÿ” Security 1. **Never commit `.env` files:** - โœ… `.env` is in `.gitignore` - โœ… Only commit `.env.example` 2. **Use different keys for different environments:** - ๐Ÿงช Development: Free tier or public RPCs - ๐Ÿš€ Production: Paid RPC providers 3. **Rotate keys regularly:** - ๐Ÿ”„ Especially if keys are exposed - ๐Ÿ“ Update `.env` file with new keys ### ๐Ÿ—‚๏ธ Organization 4. **Use environment-specific files:** - ๐Ÿ“ `.env.local` - Local development (gitignored) - ๐Ÿ“ `.env.production` - Production (gitignored) - ๐Ÿ“ `.env.example` - Template (committed) 5. **Validate on startup:** - โœ… Use `pnpm run check:env` before running tests - โœ… Scripts will warn if RPC URLs are not configured --- ## ๐Ÿ”’ Security Notes > โš ๏ธ **IMPORTANT**: > - โ›” **Never commit `.env` files** - They may contain private keys > - ๐Ÿ”‘ **Don't share RPC keys** - They may have rate limits or costs > - ๐Ÿ”„ **Use separate keys** for development and production > - ๐Ÿ” **Rotate keys** if they're exposed or compromised --- ## ๐Ÿ“ Example .env File ```bash # RPC Endpoints MAINNET_RPC_URL=https://mainnet.infura.io/v3/your-infura-project-id BASE_RPC_URL=https://base-mainnet.infura.io/v3/your-infura-project-id ARBITRUM_RPC_URL=https://arbitrum-mainnet.infura.io/v3/your-infura-project-id OPTIMISM_RPC_URL=https://optimism-mainnet.infura.io/v3/your-infura-project-id POLYGON_RPC_URL=https://polygon-mainnet.infura.io/v3/your-infura-project-id # Private Keys (only for mainnet execution, not fork testing) # PRIVATE_KEY=0x... # Test Configuration (optional) # TEST_SCENARIO=scenarios/aave/leveraged-long.yml # TEST_NETWORK=mainnet ``` --- ## ๐ŸŽฏ Next Steps After setting up your environment: ### 1. Verify Setup ```bash pnpm run check:env ``` ### 2. Run a Test Scenario ```bash pnpm run strat:test ``` ### 3. Run a Scenario with CLI ```bash pnpm run strat run scenarios/aave/leveraged-long.yml ``` ### 4. Try Fuzzing ```bash pnpm run strat fuzz scenarios/aave/leveraged-long.yml --iters 10 ``` --- ## ๐Ÿ“š Related Documentation - ๐Ÿ“– [Strategy Testing Guide](./STRATEGY_TESTING.md) - ๐Ÿ”— [Chain Configuration](./CHAIN_CONFIG.md) - ๐Ÿ” [Security Best Practices](./SECURITY.md)