use num_bigint::BigUint;
pub(crate) const DENOMINATOR: u32 = 10_000;
pub(crate) fn scale_truncating(amount: &BigUint, rate_bps: u32) -> BigUint {
(amount * BigUint::from(rate_bps)) / BigUint::from(DENOMINATOR)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_scale_truncating_rounds_toward_zero() {
assert_eq!(
scale_truncating(&BigUint::from(99u32), DENOMINATOR + 100),
BigUint::from(99u32)
);
assert_eq!(
scale_truncating(&BigUint::from(1_000_000u32), DENOMINATOR - 300),
BigUint::from(970_000u32)
);
}
#[test]
fn test_scale_truncating_zero_amount() {
assert_eq!(scale_truncating(&BigUint::ZERO, DENOMINATOR + 100), BigUint::ZERO);
}
}