Overview
TheCreditLogic library implements core functions for debt management, interest calculations, and liquidation logic in Gearbox Protocol V3. It handles debt accrual, repayment waterfall mechanics, and liquidation payment distributions.
Key Features
- Linear interest growth calculations
- Debt increase and decrease with interest index management
- Liquidation payment calculations with premium and fee handling
- Dynamic liquidation threshold ramping
- Multi-component debt repayment (quota fees → quota interest → base interest → principal)
Constants
Debt and Interest Functions
calcLinearGrowth
value- The yearly growth valuetimestampLastUpdate- Timestamp of the last update
calcAccruedInterest
amount- Principal amountcumulativeIndexLastUpdate- Previous cumulative indexcumulativeIndexNow- Current cumulative index
If the amount is zero, this function returns zero to avoid unnecessary calculations.
calcTotalDebt
collateralDebtData- Struct containing debt data (must have debt fields filled)
Debt Management
calcIncrease
amount- Amount to increase debt bydebt- Current debt principalcumulativeIndexNow- Current interest indexcumulativeIndexLastUpdate- Credit account’s last interest index
newDebt- Updated debt principal (debt + amount)newCumulativeIndex- New credit account interest index
The new index is calculated to maintain the same accrued interest amount after the debt increase:
debt * (indexNow / indexLastUpdate - 1) = (debt + amount) * (indexNow / indexNew - 1)calcDecrease
amount- Amount to repaydebt- Current debt principalcumulativeIndexNow- Current interest indexcumulativeIndexLastUpdate- Credit account’s last interest indexcumulativeQuotaInterest- Current quota interestquotaFees- Accrued quota feesfeeInterest- DAO fee on interest (in basis points)
newDebt- Remaining debt principalnewCumulativeIndex- Updated interest indexprofit- DAO fees collectednewCumulativeQuotaInterest- Remaining quota interestnewQuotaFees- Remaining quota fees
Liquidation Functions
calcLiquidationPayments
collateralDebtData- Collateral and debt data (both must be filled)feeLiquidation- Liquidation fee charged by DAO (in basis points)liquidationDiscount- Discount applied to collateral (= 1 - liquidation premium)amountWithFeeFn- Function to calculate amount with fee addedamountMinusFeeFn- Function to calculate amount with fee deducted
amountToPool- Underlying tokens to send to the poolremainingFunds- Underlying tokens to send to account ownerprofit- DAO fees collectedloss- Unpaid debt (bad debt)
Liquidation process:
- Apply liquidation premium and fee to total collateral value
- Use resulting funds to repay debt to the pool
- Return any excess to the account owner
- If funds insufficient, reduce protocol profits before reporting bad debt
getLiquidationThreshold
ltInitial- Initial liquidation thresholdltFinal- Final liquidation thresholdtimestampRampStart- Timestamp when ramping beginsrampDuration- Duration of the ramp in seconds
Gearbox V3 supports liquidation threshold ramping, allowing LT values to change linearly over time from
ltInitial to ltFinal. To set a static LT, write the value to ltInitial with a ramp start far in the future.Usage Example
Related Libraries
- QuotasLogic - Quota interest calculations
- CollateralLogic - Collateral valuation
- Constants - Protocol constants