harb/onchain/analysis/anchor-width-calculations.md
johba f3047072f6 feat: Dynamic anchorWidth based on staking metrics
Replace hardcoded anchorWidth=100 with dynamic calculation that uses staking data as a decentralized oracle.

Changes:
- Add _calculateAnchorWidth() function to Optimizer.sol
- Base width 40% with adjustments based on staking percentage and average tax rate
- Staking adjustment: -20% to +20% (inverse relationship)
- Tax rate adjustment: -10% to +30% (direct relationship)
- Final range clamped to 10-80% for safety

Rationale:
- High staking % = bullish sentiment → narrower anchor (20-35%) for fee optimization
- Low staking % = bearish/uncertain → wider anchor (60-80%) for defensive positioning
- High tax rates = volatility expected → wider anchor to reduce rebalancing
- Low tax rates = stability expected → narrower anchor for fee collection

The Harberger tax mechanism acts as a prediction market where stakers' self-assessed valuations reveal market expectations.

Tests:
- Add comprehensive unit tests in test/Optimizer.t.sol
- Add mock contracts for testing (MockStake.sol, MockKraiken.sol)
- Manual verification confirms all scenarios calculate correctly

Documentation:
- Add detailed analysis of anchorWidth price ranges
- Add staking-based strategy recommendations
- Add verification of calculation logic

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-19 11:41:02 +02:00

84 lines
No EOL
3.8 KiB
Markdown

# AnchorWidth Price Range Calculations
## Understanding the Formula
From the code:
```solidity
int24 anchorSpacing = TICK_SPACING + (34 * int24(params.anchorWidth) * TICK_SPACING / 100);
```
Where:
- `TICK_SPACING = 200` (for 1% fee tier pools)
- `anchorWidth` ranges from 1 to 100
## Tick to Price Conversion
In Uniswap V3:
- Each tick represents a 0.01% (1 basis point) price change
- Price at tick i = 1.0001^i
- So a tick difference of 100 = ~1.01% price change
- A tick difference of 10,000 = ~2.718x price change
## AnchorWidth to Price Range Mapping
Let's calculate the actual price ranges for different anchorWidth values:
### Formula Breakdown:
- `anchorSpacing = 200 + (34 * anchorWidth * 200 / 100)`
- `anchorSpacing = 200 + (68 * anchorWidth)`
- `anchorSpacing = 200 * (1 + 0.34 * anchorWidth)`
The anchor position extends from:
- Lower bound: `currentTick - anchorSpacing`
- Upper bound: `currentTick + anchorSpacing`
- Total width: `2 * anchorSpacing` ticks
### Price Range Calculations:
| anchorWidth | anchorSpacing (ticks) | Total Width (ticks) | Lower Price Ratio | Upper Price Ratio | Price Range |
|-------------|----------------------|---------------------|-------------------|-------------------|-------------|
| 1% | 268 | 536 | 0.974 | 1.027 | ±2.7% |
| 5% | 540 | 1080 | 0.947 | 1.056 | ±5.5% |
| 10% | 880 | 1760 | 0.916 | 1.092 | ±9.0% |
| 20% | 1560 | 3120 | 0.855 | 1.170 | ±16% |
| 30% | 2240 | 4480 | 0.800 | 1.251 | ±25% |
| 40% | 2920 | 5840 | 0.748 | 1.336 | ±33% |
| 50% | 3600 | 7200 | 0.700 | 1.429 | ±42% |
| 60% | 4280 | 8560 | 0.654 | 1.528 | ±52% |
| 70% | 4960 | 9920 | 0.612 | 1.635 | ±63% |
| 80% | 5640 | 11280 | 0.572 | 1.749 | ±74% |
| 90% | 6320 | 12640 | 0.535 | 1.871 | ±87% |
| 100% | 7000 | 14000 | 0.500 | 2.000 | ±100% |
## Key Insights:
1. **Linear Tick Scaling**: The tick spacing scales linearly with anchorWidth
2. **Non-Linear Price Scaling**: Due to exponential nature of tick-to-price conversion
3. **Asymmetric Percentages**: The percentage move down is smaller than up (e.g., 100% width = -50% to +100%)
## Practical Examples:
### anchorWidth = 50 (Common Default)
- If current price is $1.00:
- Lower bound: $0.70 (-30%)
- Upper bound: $1.43 (+43%)
- Captures moderate price movements in both directions
### anchorWidth = 100 (Maximum)
- If current price is $1.00:
- Lower bound: $0.50 (-50%)
- Upper bound: $2.00 (+100%)
- Price can double or halve while staying in range
### anchorWidth = 10 (Narrow)
- If current price is $1.00:
- Lower bound: $0.92 (-8%)
- Upper bound: $1.09 (+9%)
- Highly concentrated, requires frequent rebalancing
## Important Notes:
1. The anchor position does NOT extend to 2x or 3x the price at maximum width
2. At anchorWidth = 100, the upper bound is exactly 2x the current price
3. The formula `200 + (34 * anchorWidth * 200 / 100)` creates a sensible progression from tight to wide ranges
4. The minimum spacing (anchorWidth = 0) would be 200 ticks (±1% range), but minimum allowed is 1