Protocol Features

Osito Protocol implements several technical features that support its mathematical lending model and ensure efficient operation.

Event-Driven Updates

Osito uses an event-driven approach to state updates, only recalculating when users interact with the protocol:

function borrow(uint256 positionId, uint256 amount) external nonReentrant {
    // Update states when an interaction occurs
    _updateWberaRate();
    _updateTokenState(token);
    _updatePosition(positionId);
    
    // ... rest of function
}

Benefits of Event-Driven Updates

This approach provides several advantages:

  1. Gas Efficiency: No expensive periodic updates required
  2. Fresh Data: State is updated when decisions need to be made
  3. Consistent State: Every action updates relevant state variables

How It Works

  1. User initiates an action (borrow, deposit, withdraw, repay)
  2. Protocol updates relevant state variables
  3. Protocol performs the action using fresh data
  4. Interest indices are updated for precise accounting

Real-Time Pool Data

The protocol fetches AMM pool data when needed for borrow limit calculations:

function getTokensInPool(address token) external view returns (uint256) {
    address pair = getTokenWberaPair(token);
    if (pair == address(0)) return 0;
    
    // Get reserves from the pair
    (uint112 reserve0, uint112 reserve1, ) = IKodiakPair(pair).getReserves();
    
    // Determine which reserve is the token
    address token0 = IKodiakPair(pair).token0();
    return token0 == token ? uint256(reserve0) : uint256(reserve1);
}

Benefits of Real-Time Data

  1. Current State: Uses fresh pool data for calculations
  2. Direct Access: Contract-to-contract calls for pool data
  3. On-Chain Data: Uses verified AMM pool state

Index-Based Interest Accrual

Osito uses an index-based approach to interest accrual that efficiently tracks interest for all positions:

function _updateWberaRate() internal {
    uint256 timeElapsed = block.timestamp - lastWberaRateUpdate;
    if (timeElapsed > 0 && totalWberaBorrowed > 0) {
        // Calculate interest rate based on utilization
        uint256 rate = calculateInterestRate();
        
        // Calculate interest for the period
        uint256 interest = (totalWberaBorrowed * rate * timeElapsed) / (PRECISION * SECONDS_PER_YEAR);
        
        // Update the global rate index
        uint256 rateIncrease = (interest * PRECISION) / totalWberaBorrowed;
        wberaRate += rateIncrease;
        
        // Update state variables
        totalWberaBorrowed += interest;
        lastWberaRateUpdate = block.timestamp;
    }
}

How Index-Based Interest Works

  1. Global indices track accumulated interest over time
  2. User positions calculate interest using the index
  3. No need to update each position in storage on every block
  4. Interest compounds through the index mechanism

Benefits of the Index Approach

  1. Gas Efficiency: No need to update every position on each interaction
  2. Accurate Compounding: Interest accrues according to the formula
  3. Simplified Implementation: Reduces contract complexity

AMM Integration

Osito Protocol integrates with AMM pools for liquidity data and borrow limit calculations:

// Interface for AMM pools
interface IKodiakPair {
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
    function token0() external view returns (address);
    function token1() external view returns (address);
}

Supported Pool Types

Osito supports two types of liquidity pools:

  1. CPMM (Constant Product Market Maker): Traditional x*y=k formula pools
  2. CLMM (Concentrated Liquidity Market Maker): Pools with concentrated liquidity

Pool-Specific Calculations

For each pool type, Osito implements appropriate borrow limit calculations:

CPMM Pools

max_borrow = pool_BERA - (pool_BERA * pool_tokens) / (pool_tokens + dumpable_tokens)

CLMM Pools

max_borrow = pool_BERA - (dumpable_tokens * pool_BERA/pool_tokens / 2)

These calculations ensure appropriate borrow limits based on pool type.

Gas Optimizations

The protocol includes several 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 Management: Careful handling of decimal precision

Safety Features

The protocol implements several safety features:

  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. Liquidation Mechanism: Handles underwater positions