Security Mechanisms
Osito Protocol implements several security mechanisms to maintain protocol solvency and enforce borrow limits. These mechanisms work together to ensure safe lending operations.
Real-Time State Updates
The protocol updates state variables at each operation to ensure calculations use fresh data:
function borrow(uint256 positionId, uint256 amount) external nonReentrant {
// Update states with fresh data
_updateWberaRate();
_updateTokenState(token);
_updatePosition(positionId);
// Calculate position's max borrow
uint256 maxBorrow = calculatePositionMaxBorrow(positionId);
// ... rest of function
}
Benefits of Real-Time Updates
This approach provides several benefits:
- Fresh Data: Calculations use current pool state
- Market Adaptation: Lending limits adjust to changing conditions
- On-Chain Data: Uses verified AMM pool state
How It Works
- User initiates a transaction
- Protocol updates relevant state variables
- Protocol recalculates borrow limits
- Transaction proceeds if within limits
Liquidation Mechanism
Liquidations maintain protocol solvency by ensuring borrowed amounts stay within calculated limits.
Liquidation Conditions
A position becomes eligible for liquidation when:
position.borrowedAmount > calculatePositionMaxBorrow(positionId)
This can occur due to:
- Increased token utilization in borrowing
- Changes in pool liquidity
- Interest accrual
- Changes in circulating token supply
Liquidation Process
The liquidation process follows these steps:
- Anyone can call the
liquidate(positionId)
function - Protocol updates state variables
- Protocol verifies position is liquidatable
- Liquidator repays the debt
- Liquidator receives collateral
- Position is closed
All-or-Nothing Liquidations
The protocol implements all-or-nothing liquidations:
function liquidate(uint256 positionId) external nonReentrant {
// Update state with fresh data
_updateWberaRate();
_updateTokenState(token);
_updatePosition(positionId);
// Get position details
uint256 requiredWbera = position.borrowedAmount;
uint256 tokenAmount = position.tokenAmount;
// Transfer collateral to liquidator
IERC20(token).safeTransfer(msg.sender, tokenAmount);
// Liquidator repays debt
IERC20(wbera).safeTransferFrom(msg.sender, address(this), requiredWbera);
// Update protocol state
totalWberaBorrowed -= requiredWbera;
tokenData[token].totalBorrowed -= requiredWbera;
tokenData[token].totalDeposited -= tokenAmount;
// Close position
delete positions[positionId];
}
Benefits of All-or-Nothing Liquidations
This approach offers several advantages:
- Simplified Implementation: Reduces contract complexity
- Lower Gas Costs: No partial liquidation tracking
- Clear Position Management: Straightforward position accounting
- Efficient Monitoring: Clear liquidation conditions
Safety Checks
The protocol implements several safety checks:
- Reentrancy Protection: Guards against reentrancy attacks
- Zero Amount Checks: Prevents operations with zero amounts
- Borrow Limit Enforcement: Ensures positions stay within limits
- Precision Management: Careful handling of decimal precision
Gas Optimizations
The protocol includes gas optimizations:
- Unchecked Blocks: Used where safe to reduce gas costs
- Efficient Storage: Optimized storage patterns
- Minimal Updates: State updates only when necessary
- Precision Constants: Careful use of precision values