CreditEscrow
The CreditEscrow contract manages credit draws, repayments, and liquidations. It tracks outstanding balances and enforces collateral requirements.
Overview
- Pattern: UUPS Upgradeable Proxy
- Solidity Version: 0.8.24
- Dependencies: OpenZeppelin Contracts v5
Key Functions
draw
function draw(uint256 amount) external
Draws from the caller's credit line. The draw will revert if:
- The amount exceeds available credit
- The resulting health factor would drop below 1.0
- The caller's credit line is frozen
Parameters:
amount-- USD-denominated amount to draw (18 decimals)
Events: CreditDrawn(address indexed user, uint256 amount)
repay
function repay(address token, uint256 amount) external
Repays outstanding credit balance using the specified token. The repayment value is calculated using the OracleReader price feed.
Parameters:
token-- ERC-20 token used for repaymentamount-- Amount of token to use for repayment
Events: CreditRepaid(address indexed user, address token, uint256 amount, uint256 creditReduced)
liquidate
function liquidate(address user) external onlyOperator
Performs partial liquidation on an account whose health factor has dropped below 1.0. Liquidation sells enough collateral to restore the health factor to 1.5.
Parameters:
user-- Address of the account to liquidate
Events: Liquidated(address indexed user, uint256 collateralSold, uint256 debtRepaid)
getOutstandingBalance
function getOutstandingBalance(address user) external view returns (uint256)
Returns the user's total outstanding credit balance including accrued interest.
getAvailableCredit
function getAvailableCredit(address user) external view returns (uint256)
Returns the amount of credit the user can still draw, based on their collateral value and outstanding balance.
Interest Accrual
Interest accrues continuously using a per-second rate model:
balance_after = balance_before * (1 + rate_per_second) ^ seconds_elapsed
The interest rate is determined by the user's graduation tier (see Interest Rates).
Liquidation Process
- Health factor drops below 1.0
- Operator calls
liquidate() - Contract calculates collateral to sell to restore health factor to 1.5
- Collateral is sold at market price (via OracleReader) with a 5% liquidation penalty
- Proceeds repay the user's outstanding balance
- Remaining collateral stays in the user's DepositVault
Security Considerations
- Draws are atomic -- the entire transaction reverts if any check fails
- Liquidation includes a 5% penalty to discourage under-collateralization
- Interest rate parameters are set by governance and cannot be changed unilaterally