3.9 KiB
3.9 KiB
Atomic Amortizing Cycle
Overview
The atomic amortizing cycle is the core mechanism of the DBIS system. It guarantees that every execution improves the position (debt↓, collateral↑, LTV↓, HF↑) in a single atomic transaction.
Cycle Phases
Phase 1: Pre-Execution Validation
GovernanceGuard.enforceInvariants()
↓
PolicyEngine.evaluateAll()
↓
ConfigRegistry.getMaxLoops()
↓
Vault.snapshotPosition()
Checks:
- Policy modules approve action
- Max loops not exceeded
- Position snapshot taken
Phase 2: Flash Loan Execution
FlashLoanRouter.flashLoan()
↓
Provider-specific flash loan (Aave/Balancer/Uniswap/DAI)
↓
Kernel.onFlashLoan() callback
Actions:
- Borrow flash loan amount
- Execute callback with borrowed funds
Phase 3: Amortization Operations
onFlashLoan() callback:
1. Harvest yield (if available)
2. Swap to target asset
3. Split funds:
- 50% repay debt
- 50% add collateral
4. Repay flash loan (principal + fee)
Math:
yieldAmount = harvestYield()
totalAmount = flashAmount + yieldAmount
swapAmount = swapToTargetAsset(totalAmount)
debtRepayment = swapAmount / 2
collateralAddition = swapAmount - debtRepayment
Phase 4: Invariant Verification
Vault.verifyPositionImproved(
collateralBefore,
debtBefore,
healthFactorBefore
)
Checks:
debtAfter < debtBefore✓collateralAfter > collateralBefore✓healthFactorAfter > healthFactorBefore✓
Phase 5: Completion
If invariants pass:
- Emit AmortizationExecuted event
- Return success
If invariants fail:
- Emit InvariantFail event
- Revert transaction
Recursive Cycles
The kernel can execute multiple cycles in sequence:
for (uint256 i = 0; i < maxLoops; i++) {
executeSingleStep();
// Early exit if target achieved
if (currentHF >= targetHF) {
break;
}
// Early exit if no improvement
if (noImprovement) {
break;
}
}
Each cycle must improve the position independently.
Example Flow
Input
- Current Position:
- Collateral: $1,000,000
- Debt: $800,000
- HF: 1.05
- LTV: 80%
Cycle Execution
- Flash loan: $100,000 USDC
- Swap: $100,000 → $100,000 USDC (no swap needed)
- Repay debt: $50,000
- Add collateral: $50,000
Output
- New Position:
- Collateral: $1,050,000 (+$50,000)
- Debt: $750,000 (-$50,000)
- HF: 1.12 (+0.07)
- LTV: 71.4% (-8.6%)
Invariant Check
- ✓ Debt decreased
- ✓ Collateral increased
- ✓ HF improved
- ✓ LTV decreased
Result: SUCCESS
Gas Optimization
Batch Operations
- Multiple cycles in one transaction
- Batch collateral toggles
- Batch policy evaluations
Flash Loan Optimization
- Use cheapest provider (DAI flash mint = 0% fee)
- Split across providers for large amounts
- Optimal provider selection
Early Exits
- Exit if target HF achieved
- Exit if no improvement possible
- Exit if max loops reached
Error Handling
Revert Conditions
- Policy check fails
- Flash loan unavailable
- Swap fails (slippage too high)
- Debt repayment fails
- Invariant check fails
Recovery
All state changes are atomic. On revert:
- Flash loan not borrowed (or automatically repaid)
- Position unchanged
- Can retry with different parameters
Monitoring
Events Emitted
AmortizationExecuted(cyclesExecuted, collateralIncrease, debtDecrease, hfImprovement)InvariantFail(reason)SingleStepCompleted(collateralAdded, debtRepaid)
Metrics Tracked
- Cycles executed per transaction
- Average HF improvement per cycle
- Gas cost per cycle
- Success rate
Safety Guarantees
- Atomicity: All-or-nothing execution
- Invariant Preservation: Position always improves
- Reversibility: Can revert at any point
- Policy Compliance: All policies must approve
- Access Control: Only authorized operators