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:
- Gas Efficiency: No expensive periodic updates required
- Fresh Data: State is updated when decisions need to be made
- Consistent State: Every action updates relevant state variables
How It Works
- User initiates an action (borrow, deposit, withdraw, repay)
- Protocol updates relevant state variables
- Protocol performs the action using fresh data
- 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
- Current State: Uses fresh pool data for calculations
- Direct Access: Contract-to-contract calls for pool data
- 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
- Global indices track accumulated interest over time
- User positions calculate interest using the index
- No need to update each position in storage on every block
- Interest compounds through the index mechanism
Benefits of the Index Approach
- Gas Efficiency: No need to update every position on each interaction
- Accurate Compounding: Interest accrues according to the formula
- 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:
- CPMM (Constant Product Market Maker): Traditional x*y=k formula pools
- 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:
- Unchecked Blocks: Used where safe to reduce gas costs
- Efficient Storage: Optimized storage patterns
- Minimal Updates: State updates only when necessary
- Precision Management: Careful handling of decimal precision
Safety Features
The protocol implements several safety features:
- Reentrancy Protection: Guards against reentrancy attacks
- Zero Amount Checks: Prevents operations with zero amounts
- Borrow Limit Enforcement: Ensures positions stay within limits
- Liquidation Mechanism: Handles underwater positions