Rebalance Logic
Rebalancing is a core process in Twyne that ensures positions remain optimally sized as interest accrues over time. This document explains the purpose, mechanism, and implementation of rebalancing in Twyne.
Purpose of Rebalancing
As interest accrues in Twyne, two key changes occur:
Borrower collateral decreases (due to the siphoning rate paid to Credit LPs)
Credit LP shares increase (due to receiving interest)
This creates a situation where a Collateral Vault has reserved more credit from the Intermediate Vault than it currently needs based on the borrower’s reduced collateral. This excess credit is inefficient because:
It’s reserved but not being used optimally
It could be made available to other borrowers
It costs the borrower interest without providing additional borrowing power
Rebalancing is the process of calculating and releasing this excess credit back to the Intermediate Vault, ensuring capital efficiency and proper position sizing.
Excess Credit Calculation
Mathematical Model
Recall that the excess credit is calculated as (refer to Non-Negative Excess Credit invariant):
Where:
= Total collateral in the vault
= User’s collateral
= Reserved credit
= Twyne liquidation LTV
= External protocol liquidation LTV
= Safety buffer (typically 0.95 - 1)
The collateral vault then releases this excess credit bringing down and making excess credit 0.
Practically, due to precision issues, we can never bring it down exactly to 0. In that case, we always ensure the excess credit never goes negative.
Python Implementation
Rebalancing Process
Step-by-Step Process
Calculate Excess Credit:
Determine the current user collateral (total assets - reserved credit)
Calculate how much total collateral should be required based on current user collateral
Compute the difference between required total and actual total
Release Excess Credit:
If excess credit is positive, release it back to the Intermediate Vault
Update internal accounting to reflect the new reserved credit amount
Verify Invariants:
Ensure the position remains healthy after rebalancing
Confirm non-negative excess credit invariant is maintained
When Rebalancing Occurs
Rebalancing on Twyne can happen in several ways:
Automatic Rebalancing:
When certain collateral-modifying operations are performed
Examples include deposits, withdrawals, or Twyne liquidation LTV parameter changes.
Manual Rebalancing:
Anyone can explicitly call the
rebalance()functionThis might be done periodically to optimize positions
Security Implications
Rebalancing has several security implications:
Manipulation Resistance: The rebalancing mechanism must be resistant to exploitation through rapid changes in parameters or positions
Safety During Market Volatility: Rebalancing must remain safe even during periods of high market volatility
Position Health: Rebalancing should never compromise position health or violate protocol invariants
Last updated

