From a8f429a453d0c839532ea1bc883d7a7a283d184e Mon Sep 17 00:00:00 2001 From: vindard <17693119+vindard@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:37:07 -0400 Subject: [PATCH] fix: handle zero disbursed amount for check_disbursal function --- lana/app/src/credit_facility/entity.rs | 22 +++++++++++++++++++++- lana/app/src/terms/value.rs | 4 ++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lana/app/src/credit_facility/entity.rs b/lana/app/src/credit_facility/entity.rs index 69a463048..3c2a777a1 100644 --- a/lana/app/src/credit_facility/entity.rs +++ b/lana/app/src/credit_facility/entity.rs @@ -254,7 +254,13 @@ impl FacilityCVL { } fn check_disbursal_allowed(&self, terms: TermValues) -> Result<(), CreditFacilityError> { - if self.disbursed < terms.margin_call_cvl { + let cvl = if self.disbursed.is_zero() { + self.total + } else { + self.disbursed + }; + + if cvl < terms.margin_call_cvl { return Err(CreditFacilityError::BelowMarginLimit); } Ok(()) @@ -1745,6 +1751,20 @@ mod test { )); } + #[test] + fn cvl_check_disbursal_allowed_for_zero_amount() { + let terms = default_terms(); + + let facility_cvl = FacilityCVL { + total: terms.margin_call_cvl, + disbursed: CVLPct::ZERO, + }; + assert!(matches!( + facility_cvl.check_disbursal_allowed(terms), + Ok(()) + )); + } + #[test] fn check_activated_at() { let mut credit_facility = facility_from(initial_events()); diff --git a/lana/app/src/terms/value.rs b/lana/app/src/terms/value.rs index 2bd2ddd09..676f905a9 100644 --- a/lana/app/src/terms/value.rs +++ b/lana/app/src/terms/value.rs @@ -131,6 +131,10 @@ impl CVLPct { Self(Decimal::from(value)) } + pub fn is_zero(&self) -> bool { + *self == Self::ZERO + } + pub fn scale(&self, value: UsdCents) -> UsdCents { let cents = value.to_usd() * dec!(100) * (self.0 / dec!(100)); UsdCents::from(