Skip to content

Commit 1ac8f99

Browse files
committed
min max alpha emission; default to swap
1 parent c28baff commit 1ac8f99

File tree

13 files changed

+3186
-10
lines changed

13 files changed

+3186
-10
lines changed

BALANCER_MIGRATION_PLAN.md

Lines changed: 488 additions & 0 deletions
Large diffs are not rendered by default.

CURRENT_MIGRATION_STATUS.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Balancer Migration - Current Status
2+
3+
## ✅ COMPLETED (Phase 1-3)
4+
5+
### Phase 1: Balancer Pallet Creation
6+
- ✅ Created directory: `pallets/balancer-swap/`
7+
- ✅ Created `Cargo.toml` with dependencies
8+
-**types.rs** (185 lines) - Pool struct, TokenType, spot price
9+
-**math.rs** (382 lines) - All Balancer weighted pool formulas with tests:
10+
- `calc_out_given_in()` - Swap calculations
11+
- `calc_in_given_out()` - Reverse swap
12+
- `calc_spot_price()` - Price with fees
13+
- `calc_shares_for_single_token_in()` - Unbalanced LP
14+
- `calc_token_out_for_shares()` - LP redemption
15+
- `calc_shares_proportional()` - Balanced LP
16+
-**lib.rs** (700+ lines) - Main pallet with:
17+
- Storage: Pools, LiquidityShares, ProtocolShares
18+
- Extrinsics: add_liquidity, remove_liquidity, set_pool_weights, set_swap_fee
19+
- SwapHandler trait implementation
20+
- Internal swap logic using Balancer math
21+
-**weights.rs** - Placeholder weights
22+
-**tests.rs** (400+ lines) - Comprehensive unit tests
23+
-**benchmarking.rs** - Benchmark placeholders
24+
25+
### Phase 2: Workspace Integration
26+
- ✅ Updated `/Cargo.toml` - Added `pallet-balancer-swap` to workspace
27+
- ✅ Updated `runtime/Cargo.toml` - Added balancer-swap to:
28+
- dependencies
29+
- std feature
30+
- runtime-benchmarks feature
31+
- try-runtime feature
32+
33+
## 🔄 IN PROGRESS (Phase 3)
34+
35+
### Phase 3: Runtime Configuration
36+
- ⏸️ **NEXT**: Update `runtime/src/lib.rs` to:
37+
- Add BalancerSwap pallet configuration
38+
- Replace Swap with BalancerSwap in construct_runtime!
39+
- Update SwapInterface type alias
40+
- Keep V3 swap temporarily for comparison
41+
42+
## 📋 REMAINING (Phase 4-7)
43+
44+
### Phase 4: Subtensor Integration Updates
45+
Files to update:
46+
- `pallets/subtensor/src/coinbase/root.rs`
47+
- Replace `dissolve_all_liquidity_providers()` calls
48+
- Replace `clear_protocol_liquidity()` calls
49+
- `pallets/subtensor/src/coinbase/run_coinbase.rs`
50+
- Update `adjust_protocol_liquidity()` calls
51+
- `pallets/subtensor/src/staking/claim_root.rs`
52+
- Verify swap() calls work (should be compatible)
53+
- `pallets/subtensor/src/subnets/subnet.rs`
54+
- Update pool initialization
55+
56+
### Phase 5: Precompile Verification
57+
- `precompiles/src/alpha.rs`
58+
- Verify all functions work with Balancer
59+
- Should be compatible (uses SwapHandler trait)
60+
61+
### Phase 6: Testing & Migration
62+
- Create migration function to convert V3 → Balancer
63+
- Test on devnet
64+
- Comprehensive integration testing
65+
66+
### Phase 7: V3 Removal
67+
- Delete `pallets/swap/` directory (~4,000 lines)
68+
- Remove V3 imports from all files
69+
- Remove V3 tests
70+
- Update workspace Cargo.toml to remove pallet-subtensor-swap
71+
72+
## Code Statistics
73+
74+
### New Balancer Code
75+
```
76+
pallets/balancer-swap/
77+
├── Cargo.toml ✅ 50 lines
78+
├── src/
79+
│ ├── lib.rs ✅ 750 lines
80+
│ ├── types.rs ✅ 185 lines
81+
│ ├── math.rs ✅ 382 lines
82+
│ ├── weights.rs ✅ 45 lines
83+
│ ├── benchmarking.rs ✅ 95 lines
84+
│ └── tests.rs ✅ 400 lines
85+
86+
Total New Code: ~1,907 lines
87+
```
88+
89+
### V3 Code to Remove
90+
```
91+
pallets/swap/
92+
├── src/
93+
│ ├── pallet/
94+
│ │ ├── mod.rs 605 lines
95+
│ │ ├── impls.rs 1,144 lines
96+
│ │ ├── swap_step.rs 563 lines
97+
│ │ └── tests.rs 500 lines
98+
│ ├── tick.rs 2,199 lines
99+
│ ├── position.rs 199 lines
100+
│ ├── benchmarking.rs 150 lines
101+
│ └── mock.rs 100 lines
102+
103+
Total V3 Code: ~5,460 lines
104+
```
105+
106+
### Net Result
107+
- **Added**: 1,907 lines
108+
- **Removed**: 5,460 lines (pending)
109+
- **Net Reduction**: 3,553 lines (65% reduction)
110+
111+
## Key Benefits Achieved
112+
113+
1.**Simpler Architecture**: 3 storage items vs. 14
114+
2.**Unbalanced Liquidity**: Full support for any ratio
115+
3.**No Position Management**: Just share balances
116+
4.**Standard Math**: Battle-tested Balancer formulas
117+
5.**Lower Complexity**: 1,907 vs. 5,460 lines
118+
6.**Better Testability**: Cleaner test structure
119+
7.**Trait Compatible**: Same SwapHandler interface
120+
121+
## Next Immediate Steps
122+
123+
1. **Update `runtime/src/lib.rs`** (~100 lines changes):
124+
```rust
125+
// Add Balancer pallet config
126+
impl pallet_balancer_swap::Config for Runtime {
127+
type SubnetInfo = SubtensorModule;
128+
type BalanceOps = SubtensorModule;
129+
type TaoReserve = pallet_subtensor::TaoCurrencyReserve<Self>;
130+
type AlphaReserve = pallet_subtensor::AlphaCurrencyReserve<Self>;
131+
type ProtocolId = SwapProtocolId; // Reuse
132+
type DefaultTaoWeight = ConstU32<50>;
133+
type DefaultAlphaWeight = ConstU32<50>;
134+
type DefaultSwapFee = DefaultSwapFee;
135+
type MaxSwapFee = MaxSwapFee;
136+
type MinimumLiquidity = MinimumLiquidity;
137+
type WeightInfo = pallet_balancer_swap::weights::DefaultWeight<Runtime>;
138+
}
139+
140+
// In construct_runtime!
141+
BalancerSwap: pallet_balancer_swap = 29, // New pallet index
142+
143+
// Update type alias
144+
type SwapInterface = BalancerSwap; // Change from Swap
145+
```
146+
147+
2. **Test compilation**: `cargo check --release`
148+
149+
3. **Update integration points** in subtensor pallet
150+
151+
4. **Create migration function**
152+
153+
5. **Remove V3 code**
154+
155+
## Time Estimate
156+
157+
- **Completed**: ~3 days (design + implementation)
158+
- **Remaining**: ~4-6 days
159+
- Runtime integration: 0.5 days
160+
- Subtensor updates: 1 day
161+
- Migration function: 1 day
162+
- Testing: 2-3 days
163+
- V3 removal: 0.5 days
164+
165+
**Total Project**: 7-9 days
166+
167+
---
168+
169+
**Status**: 70% Complete (core pallet done, integration pending)
170+
**Last Updated**: 2025-12-05
171+
**Ready for**: Runtime integration
172+
173+
174+
175+

MIGRATION_PROGRESS.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Balancer Migration Progress
2+
3+
## Completed ✅
4+
5+
### 1. Planning & Design
6+
- ✅ Created comprehensive V3 inventory (`V3_POOL_INVENTORY.md`)
7+
- ✅ Designed Balancer architecture (`BALANCER_MIGRATION_PLAN.md`)
8+
- ✅ Identified all V3 dependencies and integration points
9+
10+
### 2. Balancer Pallet Foundation
11+
- ✅ Created pallet directory structure: `pallets/balancer-swap/`
12+
- ✅ Created `Cargo.toml` with proper dependencies
13+
- ✅ Implemented `types.rs` with:
14+
- `Pool` struct with weighted balances
15+
- `TokenType` enum
16+
- Spot price calculation
17+
- Helper methods with tests
18+
- ✅ Implemented `math.rs` with Balancer formulas:
19+
- `calc_out_given_in()` - Swap amount out calculation
20+
- `calc_in_given_out()` - Swap amount in calculation
21+
- `calc_spot_price()` - Price with fees
22+
- `calc_shares_for_single_token_in()` - LP shares for unbalanced add
23+
- `calc_token_out_for_shares()` - Token amount for LP burn
24+
- `calc_shares_proportional()` - Balanced liquidity shares
25+
- Comprehensive unit tests
26+
27+
## In Progress 🔄
28+
29+
### 3. Main Pallet Implementation
30+
**Next Step**: Create `lib.rs` with:
31+
- Storage items (Pools, LiquidityShares, ProtocolShares)
32+
- Config trait
33+
- Extrinsics (add_liquidity, remove_liquidity, set parameters)
34+
- Events and Errors
35+
- SwapHandler trait implementation
36+
37+
## Remaining Tasks 📋
38+
39+
### 4. Complete Balancer Pallet
40+
- [ ] Create `lib.rs` with full pallet logic
41+
- [ ] Implement `weights.rs` placeholder
42+
- [ ] Create `benchmarking.rs` for weight calculations
43+
- [ ] Write comprehensive tests in `tests.rs`
44+
45+
### 5. Integration Updates
46+
- [ ] Update `pallets/swap-interface/src/lib.rs` if needed
47+
- [ ] Update `pallets/subtensor/src/coinbase/root.rs`
48+
- [ ] Update `pallets/subtensor/src/coinbase/run_coinbase.rs`
49+
- [ ] Update `pallets/subtensor/src/staking/claim_root.rs`
50+
- [ ] Update `pallets/subtensor/src/subnets/subnet.rs`
51+
52+
### 6. Runtime & Precompiles
53+
- [ ] Update `runtime/src/lib.rs`:
54+
- Replace Swap pallet config with BalancerSwap
55+
- Update construct_runtime! macro
56+
- Update SwapInterface type alias
57+
- Update runtime API implementation
58+
- [ ] Verify `precompiles/src/alpha.rs` works with new SwapHandler
59+
60+
### 7. Migration Function
61+
- [ ] Create runtime migration to convert V3 state to Balancer
62+
- [ ] Handle existing positions conversion to shares
63+
- [ ] Test migration on dev/test networks
64+
65+
### 8. V3 Removal
66+
- [ ] Remove entire `pallets/swap/` directory
67+
- [ ] Remove V3 imports from all files
68+
- [ ] Remove V3 tests from subtensor
69+
- [ ] Remove V3 mock configurations
70+
- [ ] Update workspace Cargo.toml
71+
72+
### 9. Testing & Validation
73+
- [ ] Run all unit tests
74+
- [ ] Run integration tests
75+
- [ ] Benchmark performance vs. V3
76+
- [ ] Test on local devnet
77+
- [ ] Verify all swap scenarios work
78+
- [ ] Test unbalanced liquidity provision
79+
- [ ] Test weighted pools (non-50/50)
80+
81+
### 10. Documentation
82+
- [ ] Update code documentation
83+
- [ ] Create migration guide for users
84+
- [ ] Document new Balancer features
85+
- [ ] Update API documentation
86+
87+
## Key Files Created
88+
89+
```
90+
pallets/balancer-swap/
91+
├── Cargo.toml ✅ Complete
92+
├── src/
93+
│ ├── lib.rs 🔄 Next
94+
│ ├── types.rs ✅ Complete (185 lines)
95+
│ ├── math.rs ✅ Complete (382 lines)
96+
│ ├── weights.rs ⏸️ Pending
97+
│ ├── benchmarking.rs ⏸️ Pending
98+
│ └── tests.rs ⏸️ Pending
99+
100+
Documentation/
101+
├── V3_POOL_INVENTORY.md ✅ Complete (900+ lines)
102+
├── BALANCER_MIGRATION_PLAN.md ✅ Complete (600+ lines)
103+
└── MIGRATION_PROGRESS.md ✅ This file
104+
```
105+
106+
## Estimated Remaining Work
107+
108+
- **lib.rs**: ~600 lines (pallet core)
109+
- **weights.rs**: ~50 lines (placeholder)
110+
- **benchmarking.rs**: ~150 lines
111+
- **tests.rs**: ~400 lines
112+
- **Integration updates**: ~200 lines changes
113+
- **Runtime updates**: ~100 lines changes
114+
- **Migration function**: ~200 lines
115+
- **V3 removal**: Delete ~4,000 lines
116+
117+
**Total new code**: ~1,700 lines
118+
**Total removed**: ~4,000 lines
119+
**Net reduction**: ~2,300 lines (57% reduction)
120+
121+
## Next Immediate Steps
122+
123+
1. **Create `lib.rs`** with:
124+
- Pallet struct and Config trait
125+
- Storage: `Pools<T>`, `LiquidityShares<T>`, `ProtocolShares<T>`
126+
- Extrinsics: `add_liquidity`, `remove_liquidity`, `set_pool_weights`, `set_swap_fee`
127+
- Internal functions: swap logic, share calculations
128+
- SwapHandler trait implementation
129+
- Events: LiquidityAdded, LiquidityRemoved, PoolParametersUpdated, Swapped
130+
- Errors: InsufficientBalance, PoolNotFound, InvalidWeights, etc.
131+
132+
2. **Create weight placeholders** in `weights.rs`
133+
134+
3. **Write basic tests** in `tests.rs`
135+
136+
4. **Update runtime** to use BalancerSwap
137+
138+
5. **Test compilation** and fix any issues
139+
140+
## Status Summary
141+
142+
**Progress**: ~20% complete
143+
- ✅ Design & planning
144+
- ✅ Core types & math
145+
- 🔄 Pallet implementation (in progress)
146+
- ⏸️ Integration updates (pending)
147+
- ⏸️ Testing & migration (pending)
148+
- ⏸️ V3 removal (pending)
149+
150+
**Estimated Time Remaining**: 6-8 days
151+
- Day 1-2: Complete Balancer pallet
152+
- Day 3-4: Update integrations & runtime
153+
- Day 5: Migration function & testing
154+
- Day 6: V3 removal & cleanup
155+
- Day 7-8: Comprehensive testing & documentation
156+
157+
---
158+
159+
**Ready to continue with `lib.rs` implementation.**
160+
161+
162+

0 commit comments

Comments
 (0)