risk-metrics

Crates.iorisk-metrics
lib.rsrisk-metrics
version0.1.0-alpha.2
created_at2026-01-24 12:14:16.916484+00
updated_at2026-01-25 18:01:04.771207+00
descriptionRisk metrics and calculations for DeFi applications
homepage
repositoryhttps://github.com/dijkstra-keystone/keystone
max_upload_size
id2066588
size45,784
Qais Alassa (qasawa)

documentation

README

risk-metrics

Crates.io Documentation License

Risk metrics and calculations for DeFi applications.

Features

  • Health factor calculations
  • Liquidation price and threshold
  • Position sizing and LTV
  • Pool utilization metrics
  • no_std compatible
  • Deterministic results

Installation

[dependencies]
risk-metrics = "0.1"

Quick Start

use risk_metrics::{health_factor, liquidation_price, Decimal};

let collateral = Decimal::from(10000i64);
let debt = Decimal::from(5000i64);
let threshold = Decimal::new(80, 2);  // 80%

// Health factor: (collateral * threshold) / debt
let hf = health_factor(collateral, debt, threshold)?;  // 1.6

// Liquidation price
let liq = liquidation_price(
    Decimal::from(5i64),  // 5 ETH collateral
    debt,
    threshold,
)?;  // $1,250 per ETH

Functions

Health

  • health_factor(collateral, debt, threshold) - Calculate position health
  • is_healthy(collateral, debt, threshold) - Check if position is safe
  • collateral_ratio(collateral, debt) - Raw collateralization ratio

Liquidation

  • liquidation_price(collateral_amount, debt, threshold) - Price at which liquidation occurs
  • liquidation_threshold(collateral, debt, health_factor) - Threshold for given health factor
  • max_borrowable(collateral, threshold, min_health_factor) - Maximum safe debt

Position

  • loan_to_value(debt, collateral) - LTV ratio
  • utilization_rate(borrows, liquidity) - Pool utilization
  • available_liquidity(total_liquidity, borrows) - Remaining liquidity

DeFi Protocol Compatibility

Designed for integration with lending protocols:

  • Aave-style health factor calculations
  • Compound-style collateral ratios
  • MakerDAO-style liquidation thresholds

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Commit count: 10

cargo fmt