//! Interest rate calculations for Drift and Kamino spot markets.
//!
//! Protocol-specific logic lives in submodules; shared types and helpers
//! are re-exported from the crate root.

pub mod drift;
pub mod kamino;

// ─── Shared error type ──────────────────────────────────────────

/// Errors from rate calculations.
#[derive(Debug, thiserror::Error)]
pub enum RateError {
    #[error("arithmetic overflow in rate calculation")]
    Overflow,

    #[error("SpotMarket decimals {0} exceeds maximum of 19")]
    InvalidDecimals(u32),
}

pub type RateResult<T> = Result<T, RateError>;

// ─── Shared constants ───────────────────────────────────────────

/// Precision for utilization and rate values (1,000,000 = 100%).
pub const RATE_PRECISION: u128 = 1_000_000;

// ─── Shared helpers ─────────────────────────────────────────────

/// Convert a rate value (scaled by [`RATE_PRECISION`]) to a decimal `f64`.
///
/// Example: `500_000` -> `0.5` (50%).
pub fn rate_to_decimal(rate: u128) -> f64 {
    rate as f64 / RATE_PRECISION as f64
}

// ─── Re-exports for backwards compatibility ─────────────────────

pub use drift::{
    calculate_borrow_rate, calculate_deposit_rate, calculate_ltv, calculate_utilization,
    compute_drift_rates, token_borrow_amount, token_deposit_amount,
};
