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:

  1. Fresh Data: Calculations use current pool state
  2. Market Adaptation: Lending limits adjust to changing conditions
  3. On-Chain Data: Uses verified AMM pool state

How It Works

  1. User initiates a transaction
  2. Protocol updates relevant state variables
  3. Protocol recalculates borrow limits
  4. 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:

  1. Increased token utilization in borrowing
  2. Changes in pool liquidity
  3. Interest accrual
  4. Changes in circulating token supply

Liquidation Process

The liquidation process follows these steps:

  1. Anyone can call the liquidate(positionId) function
  2. Protocol updates state variables
  3. Protocol verifies position is liquidatable
  4. Liquidator repays the debt
  5. Liquidator receives collateral
  6. 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:

  1. Simplified Implementation: Reduces contract complexity
  2. Lower Gas Costs: No partial liquidation tracking
  3. Clear Position Management: Straightforward position accounting
  4. Efficient Monitoring: Clear liquidation conditions

Safety Checks

The protocol implements several safety checks:

  1. Reentrancy Protection: Guards against reentrancy attacks
  2. Zero Amount Checks: Prevents operations with zero amounts
  3. Borrow Limit Enforcement: Ensures positions stay within limits
  4. Precision Management: Careful handling of decimal precision

Gas Optimizations

The protocol includes gas optimizations:

  1. Unchecked Blocks: Used where safe to reduce gas costs
  2. Efficient Storage: Optimized storage patterns
  3. Minimal Updates: State updates only when necessary
  4. Precision Constants: Careful use of precision values