harb/onchain/analysis/README.md

125 lines
4.3 KiB
Markdown
Raw Normal View History

2025-08-09 18:03:31 +02:00
# KRAIKEN Fuzzing Analysis Tools
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
This directory contains tools for fuzzing the KRAIKEN LiquidityManager to identify potential profitable trading scenarios.
2025-07-06 11:20:35 +02:00
## Quick Start
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
```bash
# Run fuzzing with default settings (50 runs, 20 trades)
./run-fuzzing.sh BullMarketOptimizer
# Run with custom parameters
./run-fuzzing.sh WhaleOptimizer runs=100 trades=50
# Clean up generated files
./clean.sh
```
## Files
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
### Core Scripts
2025-08-23 22:32:41 +02:00
- `StreamlinedFuzzing.s.sol` - Streamlined fuzzing script with staking support and accurate trade recording
2025-08-19 17:35:43 +02:00
- `run-fuzzing.sh` - Shell script to orchestrate multiple fuzzing runs with configurable parameters
2025-08-23 22:32:41 +02:00
- `clean-csvs.sh` - Cleanup script to remove generated CSV files
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
### Helpers
- `helpers/SwapExecutor.sol` - Handles swap execution through Uniswap
- `helpers/CSVManager.sol` - CSV generation utilities
- `helpers/CSVHelper.sol` - CSV formatting helpers
2025-08-09 18:03:31 +02:00
### Visualization
2025-08-23 22:32:41 +02:00
- `run-visualizer.html` - Interactive web visualization for analyzing individual trades from fuzzing runs
- Supports row-by-row navigation through trades with liquidity distribution charts
2025-08-09 18:03:31 +02:00
## Available Optimizers
2025-08-09 18:03:31 +02:00
- `BullMarketOptimizer` - Aggressive parameters for bull market conditions
- `NeutralMarketOptimizer` - Balanced parameters
- `BearMarketOptimizer` - Conservative parameters for bear market conditions
- `WhaleOptimizer` - Simulates large position dominance
- `MockOptimizer` - Standard mock with configurable parameters
- `RandomScenarioOptimizer` - Randomized parameters for each run
2025-08-09 18:03:31 +02:00
## Usage
2025-08-09 18:03:31 +02:00
### Running Fuzzing Campaigns
```bash
2025-08-09 18:03:31 +02:00
# Basic usage
2025-08-19 17:35:43 +02:00
./run-fuzzing.sh <optimizer_class> [runs=N] [staking=on|off] [buybias=N] [trades=N] [stakingbias=N]
2025-08-09 18:03:31 +02:00
# Examples
./run-fuzzing.sh BullMarketOptimizer # Uses defaults
./run-fuzzing.sh WhaleOptimizer runs=100 # 100 runs
./run-fuzzing.sh BearMarketOptimizer trades=50 # 50 trades per run
./run-fuzzing.sh NeutralMarketOptimizer runs=25 trades=30 # Both params
2025-08-19 17:35:43 +02:00
./run-fuzzing.sh BullMarketOptimizer runs=50 staking=on buybias=85 trades=100 stakingbias=95 # Full config
```
2025-08-09 18:03:31 +02:00
Parameters:
- `optimizer_class` - Required. The optimizer class to use
2025-08-19 17:35:43 +02:00
- `runs=N` - Optional. Number of fuzzing runs (default: 20)
- `staking=on|off` - Optional. Enable/disable staking (default: on)
- `buybias=N` - Optional. 0-100% bias towards buying vs selling (default: 50)
- `trades=N` - Optional. Trades per run (default: 15, supports 100+ trades)
- `stakingbias=N` - Optional. 0-100% bias towards staking vs unstaking (default: 80)
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
### Output
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
Each fuzzing campaign creates a timestamped directory with:
- Individual run logs (`run_N.log`)
- Merged CSV of profitable scenarios
- Summary report with statistics
- Configuration file for reproducibility
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
### Visualization
2025-07-06 11:20:35 +02:00
2025-08-09 18:03:31 +02:00
To visualize results:
2025-08-09 18:03:31 +02:00
```bash
2025-08-23 22:32:41 +02:00
# Start local web server from analysis directory
cd analysis && python3 -m http.server 8000
# Then open http://localhost:8000/run-visualizer.html
2025-08-23 22:32:41 +02:00
# Or use debugCSV mode which automatically launches visualizer
./analysis/run-fuzzing.sh BullMarketOptimizer debugCSV
2025-08-09 18:03:31 +02:00
```
### Cleanup
Remove all generated files:
```bash
./clean.sh
```
## Environment Variables
The fuzzing script supports these environment variables:
- `FUZZING_RUNS` - Number of runs (overridden by script parameter)
- `OPTIMIZER_CLASS` - Optimizer to use (overridden by script parameter)
- `TRADES_PER_RUN` - Trades per run (overridden by script parameter)
2025-08-19 17:35:43 +02:00
- `TRACK_POSITIONS` - Enable detailed position tracking (default: true)
- `ENABLE_STAKING` - Enable staking operations (default: true)
- `BUY_BIAS` - Buy bias percentage (default: 50)
- `STAKING_BIAS` - Staking bias percentage (default: 80)
2025-08-09 18:03:31 +02:00
## Development
To add a new optimizer:
1. Create the optimizer contract in `../test/mocks/`
2025-08-19 17:35:43 +02:00
2. Import it in `ImprovedFuzzingAnalysis.s.sol`
2025-08-09 18:03:31 +02:00
3. Add it to the `_getOptimizerByClass` function
4. Update this README
## Notes
- Each run deploys a fresh Uniswap V3 environment
2025-08-23 22:32:41 +02:00
- Gas limit is set to 300M for script execution
2025-08-09 18:03:31 +02:00
- Results are deterministic based on the seed
2025-08-19 17:35:43 +02:00
- The fuzzer tests random buy/sell patterns with periodic recenters
- Supports staking operations with position snatching mechanics
2025-08-23 22:32:41 +02:00
- Only records trades that actually execute (non-zero amounts)
- Records actual traded amounts after liquidity limits are applied
- CSV files are written to the analysis/ directory
- Every trade properly updates the tick value