6.6 KiB
⚙️ 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
cp .env.example .env
2️⃣ Fill in Your RPC URLs
# 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
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)
- 📝 Sign up at infura.io
- ➕ Create a project
- 📋 Copy your project ID
- 🔗 Use:
https://mainnet.infura.io/v3/YOUR_PROJECT_ID
3. Alchemy (Free Tier)
- 📝 Sign up at alchemy.com
- ➕ Create an app
- 📋 Copy your API key
- 🔗 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 |
| Alchemy | Fast, good free tier | alchemy.com |
| QuickNode | Fast, global network | quicknode.com |
| Ankr | Good performance | ankr.com |
✅ Verification
🔍 Check Environment Variables
Run the environment checker:
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
# 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:
- ❌ Invalid API key
- ⏱️ Rate limiting (free tier exceeded)
- 🚫 IP restrictions
- 🔒 Infura project set to "private key only" mode
Solutions:
- ✅ Verify your API key is correct
- ✅ Check your RPC provider dashboard for rate limits
- ✅ Try a different RPC provider
- ✅ For Infura: Enable "Public Requests" in project settings
❌ "Environment variable not set"
Problem: The script can't find the required environment variable.
Solutions:
- ✅ Check that
.envfile exists in project root - ✅ Verify variable name is correct (case-sensitive)
- ✅ Restart your terminal/IDE after creating
.env - ✅ Use
pnpm run check:envto 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:
- ✅ Ensure
.envfile is in the project root - ✅ Check that
dotenvpackage is installed - ✅ Verify scripts load dotenv before other imports
💡 Best Practices
🔐 Security
-
Never commit
.envfiles:- ✅
.envis in.gitignore - ✅ Only commit
.env.example
- ✅
-
Use different keys for different environments:
- 🧪 Development: Free tier or public RPCs
- 🚀 Production: Paid RPC providers
-
Rotate keys regularly:
- 🔄 Especially if keys are exposed
- 📝 Update
.envfile with new keys
🗂️ Organization
-
Use environment-specific files:
- 📁
.env.local- Local development (gitignored) - 📁
.env.production- Production (gitignored) - 📁
.env.example- Template (committed)
- 📁
-
Validate on startup:
- ✅ Use
pnpm run check:envbefore running tests - ✅ Scripts will warn if RPC URLs are not configured
- ✅ Use
🔒 Security Notes
⚠️ IMPORTANT:
- ⛔ Never commit
.envfiles - 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
# 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
pnpm run check:env
2. Run a Test Scenario
pnpm run strat:test
3. Run a Scenario with CLI
pnpm run strat run scenarios/aave/leveraged-long.yml
4. Try Fuzzing
pnpm run strat fuzz scenarios/aave/leveraged-long.yml --iters 10