Skip to main content

klend_interface/
fraction.rs

1//! Fixed-point fraction type matching the on-chain representation.
2//!
3//! All on-chain fields ending in `_sf` (e.g. `market_price_sf`, `borrowed_amount_sf`)
4//! store values as [`Fraction`] — a 128-bit unsigned fixed-point number with 68 integer
5//! bits and 60 fractional bits (`U68F60`).
6//!
7//! # Converting from on-chain `_sf` fields
8//!
9//! The `_sf` fields are stored as `u128` (or [`PodU128`](crate::state::PodU128))
10//! on-chain. To interpret them as a [`Fraction`]:
11//!
12//! ```rust
13//! use klend_interface::Fraction;
14//!
15//! let raw_sf: u128 = 1_152_921_504_606_846_976; // 1.0 as _sf
16//! let value = Fraction::from_bits(raw_sf);
17//! assert_eq!(value, Fraction::ONE);
18//!
19//! // Convert to f64
20//! let float_value: f64 = value.to_num();
21//! assert!((float_value - 1.0).abs() < 1e-10);
22//! ```
23
24/// Fixed-point fraction type: 128-bit unsigned, 68 integer bits, 60 fractional bits.
25///
26/// This is the same type used internally by the klend program for all `_sf` fields.
27/// Use [`Fraction::from_bits`] to convert raw on-chain `u128` values.
28pub use fixed::types::U68F60 as Fraction;
29
30/// Scale factor for `_sf` fields: `1.0 == 2^60`.
31///
32/// This is equivalent to `Fraction::ONE.to_bits()`.
33pub const FRACTION_ONE_SCALED: u128 = 1 << 60;