Account Structure
Credit accounts store critical information for debt and collateral tracking:Key Fields Explained
Debt Components:debt: The principal amount borrowed from the poolcumulativeIndexLastUpdate: Checkpoint for calculating accrued base interestcumulativeQuotaInterest: Total quota interest accumulated over timequotaFees: One-time fees from quota increases
enabledTokensMask: Bitmask indicating which tokens count as collateral- Bit 0 (value 1): Reserved for the underlying token (always enabled)
- Bits 1-255: Available for other collateral tokens
flags: Status flags for special conditions or permissionslastDebtUpdate: Prevents debt manipulation in the same blockborrower: The address that controls the account
Account Lifecycle
Opening an Account
Accounts are opened through the Credit Facade using a multicall pattern:- Account Creation: Credit Manager requests a new credit account from the Account Factory
- Initial Operations: Execute multicall operations (add collateral, increase debt, swaps, etc.)
- Collateral Check: Verify the account meets minimum health factor requirements
- Debt Validation: Ensure debt is within configured limits (
minDebt≤ debt ≤maxDebt)
Accounts can be opened with zero debt initially if collateral is added. However, most users open accounts with both collateral and debt to begin leveraged positions immediately.
- ✅ Adding collateral
- ✅ Increasing debt
- ✅ Withdrawing collateral
- ✅ Updating quotas
- ✅ External calls to adapters
- ❌ Decreasing debt (prevented to ensure proper initialization)
Managing an Account
After opening, users can modify their accounts through multicalls:Increase Debt
Increase Debt
Borrow additional funds from the pool:Validations:
- Debt must not have been updated in the same block
- No forbidden tokens can be enabled on the account
- Resulting debt must not exceed
maxDebt - Total borrowed by credit manager in this block must not exceed limit
- Account must pass collateral check with required health factor
Decrease Debt
Decrease Debt
Repay debt to the pool:Repayment Order:
- Quota fees
- Quota interest + protocol fee
- Base interest + protocol fee
- Debt principal
- If
amountexceeds total debt, performs full repayment - Full repayment (debt = 0) requires all quotas to be disabled
- Partial repayment must leave debt ≥
minDebtor reduce it to 0 - Collateral check skipped if debt reaches zero
Add Collateral
Add Collateral
Transfer tokens from user to the credit account:Behavior:
- Requires token approval from caller to credit manager
- Underlying token always counts as collateral
- Other tokens require quota allocation to count as collateral
- Tokens without quota can be held but won’t protect against liquidation
Withdraw Collateral
Withdraw Collateral
Remove tokens from the credit account:Safety Measures:
- Activates safe pricing (minimum of main and reserve price feeds)
- Reverts if forbidden tokens are enabled on the account
- Account must pass collateral check after withdrawal
- Use
type(uint256).maxforamountto withdraw entire balance
Update Quota
Update Quota
Allocate or deallocate quota for a collateral token:Effects:
- Enables token if quota increases from 0
- Disables token if quota decreases to 0
- Charges one-time quota increase fee
- Accrues quota interest at the token’s rate
- Cannot increase quota for forbidden tokens
- Requires account to have non-zero debt
Closing an Account
Accounts are closed by repaying all debt and withdrawing collateral:- Execute Operations: Perform multicall operations (swaps, debt repayment, etc.)
- Debt Verification: Ensure all debt is fully repaid (debt = 0)
- Quota Verification: Ensure all quotas are disabled
- Collateral Check: Skipped since account has no debt
- Account Return: Credit account returned to the factory for reuse
- ✅ Decreasing debt
- ✅ Adding collateral (to cover debt)
- ✅ Withdrawing collateral
- ✅ Updating quotas (to disable them)
- ✅ External calls to adapters
- ❌ Increasing debt (prevented during closure)
Account Factory
Credit accounts are managed by the Account Factory, which implements object pooling for gas efficiency:- Reuses deployed accounts instead of creating new ones
- Reduces gas costs for account opening
- Maintains pool of available accounts
- Handles account initialization and cleanup
Active Credit Account Pattern
When adapters need to interact with a credit account, they use the active credit account mechanism:- Only the credit facade can set the active account
- Only registered adapters can call
execute()which relies on active account - Active account is cleared after each adapter call
- Prevents unauthorized access to credit account operations
Collateral Checks
After most operations, accounts undergo a collateral check to ensure adequate collateralization:twvUSD: Total weighted value (collateral × liquidation thresholds)totalDebtUSD: Total debt including all interest and fees- Result must be ≥
minHealthFactor(typically 10000 = 100%)
Bot Permissions
Account owners can grant bots permission to manage accounts on their behalf:botMulticall():
- Rebalancing positions
- Taking profit or stop-loss
- Liquidation protection
- Yield optimization
State Transitions
Credit accounts transition through several states:flags field in CreditAccountInfo tracks special states:
- Emergency modes
- Bot permissions
- Temporary restrictions
- Protocol-specific markers
Best Practices
Gas Optimization
- Use collateral hints when possible
- Batch operations in multicalls
- Enable only necessary tokens
- Disable unused quotas
Safety
- Monitor health factor closely
- Avoid forbidden tokens
- Understand repayment priority
- Test operations before execution
Liquidation Prevention
- Maintain health factor buffer
- Use bot permissions for automation
- Monitor price volatility
- Keep emergency collateral ready
Quota Management
- Only enable needed collateral
- Consider quota interest costs
- Disable quotas when closing
- Check token limits before updating