credit-facility-rs 0.1.0

A rust library for modeling credit facilities with time-aware calculations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use chrono::{DateTime, Duration, Utc};
use hourglass_rs::SafeTimeProvider;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use uuid::Uuid;

use crate::decimal::{Money, Rate};
use crate::errors::{FacilityError, Result};
use crate::events::Event;
use crate::types::{LtvStatus, LtvThresholds};

/// ltv calculator for facilities
pub struct LtvCalculator {
    facility_id: Uuid,
    thresholds: LtvThresholds,
}

impl LtvCalculator {
    /// create new ltv calculator
    pub fn new(facility_id: Uuid, thresholds: LtvThresholds) -> Self {
        Self {
            facility_id,
            thresholds,
        }
    }
    
    /// calculate current ltv ratio
    pub fn calculate_ltv(
        &self,
        cvl: Money,
        collateral_value: Money,
    ) -> Result<Rate> {
        if collateral_value.is_zero() {
            return Err(FacilityError::InvalidCollateral {
                message: "Collateral value cannot be zero".to_string(),
            });
        }
        
        Ok(Rate::from_decimal(cvl.as_decimal() / collateral_value.as_decimal()))
    }
    
    /// calculate current value of liability
    pub fn calculate_cvl(
        outstanding_principal: Money,
        accrued_interest: Money,
        accrued_fees: Money,
        accrued_penalties: Money,
    ) -> Money {
        outstanding_principal + accrued_interest + accrued_fees + accrued_penalties
    }
    
    /// determine ltv status based on thresholds
    pub fn get_ltv_status(&self, ltv: Rate) -> LtvStatus {
        let ltv_decimal = ltv.as_decimal();
        
        if ltv_decimal > self.thresholds.liquidation_ltv.as_decimal() {
            LtvStatus::Liquidation
        } else if ltv_decimal > self.thresholds.margin_call_ltv.as_decimal() {
            LtvStatus::MarginCall
        } else if ltv_decimal > self.thresholds.warning_ltv.as_decimal() {
            LtvStatus::Warning
        } else {
            LtvStatus::Healthy
        }
    }
    
    /// calculate required collateral to reach target ltv
    pub fn calculate_required_collateral(
        &self,
        cvl: Money,
        current_collateral: Money,
        target_ltv: Rate,
    ) -> Money {
        if target_ltv.as_decimal().is_zero() {
            return Money::from_decimal(Decimal::MAX);
        }
        
        let required_collateral_value = Money::from_decimal(
            cvl.as_decimal() / target_ltv.as_decimal()
        );
        
        if required_collateral_value > current_collateral {
            required_collateral_value - current_collateral
        } else {
            Money::ZERO
        }
    }
    
    /// calculate payment required to reach target ltv
    pub fn calculate_required_payment(
        &self,
        cvl: Money,
        collateral_value: Money,
        target_ltv: Rate,
    ) -> Money {
        let target_cvl = Money::from_decimal(
            collateral_value.as_decimal() * target_ltv.as_decimal()
        );
        
        if cvl > target_cvl {
            cvl - target_cvl
        } else {
            Money::ZERO
        }
    }
}

/// ltv monitor for continuous tracking
pub struct LtvMonitor {
    calculator: LtvCalculator,
    last_check: DateTime<Utc>,
    check_frequency: Duration,
    margin_call_active: bool,
    margin_call_deadline: Option<DateTime<Utc>>,
    events: Vec<Event>,
}

impl LtvMonitor {
    /// create new ltv monitor
    pub fn new(
        facility_id: Uuid,
        thresholds: LtvThresholds,
        check_frequency: Duration,
    ) -> Self {
        Self {
            calculator: LtvCalculator::new(facility_id, thresholds),
            last_check: DateTime::<Utc>::MIN_UTC,  // start with minimum date so first check always runs
            check_frequency,
            margin_call_active: false,
            margin_call_deadline: None,
            events: Vec::new(),
        }
    }
    
    /// check ltv and emit appropriate events
    pub fn check_ltv(
        &mut self,
        cvl: Money,
        collateral_value: Money,
        time_provider: &SafeTimeProvider,
    ) -> Result<LtvStatus> {
        let now = time_provider.now();
        
        // check if it's time to check
        if now - self.last_check < self.check_frequency {
            return Ok(self.get_current_status(cvl, collateral_value)?);
        }
        
        self.last_check = now;
        
        let ltv = self.calculator.calculate_ltv(cvl, collateral_value)?;
        let status = self.calculator.get_ltv_status(ltv);
        
        match status {
            LtvStatus::Healthy => {
                if self.margin_call_active {
                    self.resolve_margin_call(time_provider);
                }
            }
            LtvStatus::Warning => {
                self.emit_warning(ltv, time_provider);
            }
            LtvStatus::MarginCall => {
                if !self.margin_call_active {
                    self.issue_margin_call(ltv, cvl, collateral_value, time_provider)?;
                } else {
                    self.check_margin_call_deadline(time_provider)?;
                }
            }
            LtvStatus::Liquidation => {
                self.trigger_liquidation(ltv, cvl, collateral_value, time_provider);
            }
        }
        
        Ok(status)
    }
    
    /// get current status without checking
    fn get_current_status(&self, cvl: Money, collateral_value: Money) -> Result<LtvStatus> {
        let ltv = self.calculator.calculate_ltv(cvl, collateral_value)?;
        Ok(self.calculator.get_ltv_status(ltv))
    }
    
    /// emit warning event
    fn emit_warning(&mut self, ltv: Rate, time_provider: &SafeTimeProvider) {
        self.events.push(Event::LtvWarningBreached {
            facility_id: self.calculator.facility_id,
            ltv_ratio: ltv,
            threshold: self.calculator.thresholds.warning_ltv,
            timestamp: time_provider.now(),
        });
    }
    
    /// issue margin call
    fn issue_margin_call(
        &mut self,
        ltv: Rate,
        cvl: Money,
        collateral_value: Money,
        time_provider: &SafeTimeProvider,
    ) -> Result<()> {
        self.margin_call_active = true;
        self.margin_call_deadline = Some(time_provider.now() + Duration::hours(24));
        
        let required_collateral = self.calculator.calculate_required_collateral(
            cvl,
            collateral_value,
            self.calculator.thresholds.margin_call_ltv,
        );
        
        let required_payment = self.calculator.calculate_required_payment(
            cvl,
            collateral_value,
            self.calculator.thresholds.margin_call_ltv,
        );
        
        self.events.push(Event::MarginCallRequired {
            facility_id: self.calculator.facility_id,
            current_ltv: ltv,
            required_ltv: self.calculator.thresholds.margin_call_ltv,
            deadline: self.margin_call_deadline.unwrap(),
            options: vec![
                format!("Add collateral: {}", required_collateral),
                format!("Make payment: {}", required_payment),
            ],
        });
        
        Ok(())
    }
    
    /// check margin call deadline
    fn check_margin_call_deadline(&mut self, time_provider: &SafeTimeProvider) -> Result<()> {
        if let Some(deadline) = self.margin_call_deadline {
            if time_provider.now() > deadline {
                return Err(FacilityError::MarginCallExpired {
                    deadline,
                    current_time: time_provider.now(),
                });
            }
        }
        Ok(())
    }
    
    /// resolve margin call
    fn resolve_margin_call(&mut self, time_provider: &SafeTimeProvider) {
        self.margin_call_active = false;
        self.margin_call_deadline = None;
        
        self.events.push(Event::MarginCallResolved {
            facility_id: self.calculator.facility_id,
            new_ltv: Rate::from_decimal(dec!(0)), // would need to pass actual LTV
            timestamp: time_provider.now(),
        });
    }
    
    /// trigger liquidation
    fn trigger_liquidation(
        &mut self,
        ltv: Rate,
        cvl: Money,
        collateral_value: Money,
        time_provider: &SafeTimeProvider,
    ) {
        self.events.push(Event::LiquidationTriggered {
            facility_id: self.calculator.facility_id,
            ltv_ratio: ltv,
            collateral_value,
            debt_amount: cvl,
            timestamp: time_provider.now(),
        });
    }
    
    /// get pending events
    pub fn take_events(&mut self) -> Vec<Event> {
        std::mem::take(&mut self.events)
    }
    
    /// is margin call active
    pub fn is_margin_call_active(&self) -> bool {
        self.margin_call_active
    }
    
    /// get margin call deadline
    pub fn margin_call_deadline(&self) -> Option<DateTime<Utc>> {
        self.margin_call_deadline
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;
    use hourglass_rs::TimeSource;
    
    fn create_test_thresholds() -> LtvThresholds {
        LtvThresholds {
            initial_ltv: Rate::from_percentage(50),
            warning_ltv: Rate::from_percentage(65),
            margin_call_ltv: Rate::from_percentage(70),
            liquidation_ltv: Rate::from_percentage(75),
        }
    }
    
    #[test]
    fn test_ltv_calculation() {
        let calc = LtvCalculator::new(Uuid::new_v4(), create_test_thresholds());
        
        let cvl = Money::from_major(50_000);
        let collateral = Money::from_major(100_000);
        
        let ltv = calc.calculate_ltv(cvl, collateral).unwrap();
        assert_eq!(ltv.as_decimal(), dec!(0.5));
    }
    
    #[test]
    fn test_ltv_status_determination() {
        let calc = LtvCalculator::new(Uuid::new_v4(), create_test_thresholds());
        
        assert_eq!(calc.get_ltv_status(Rate::from_percentage(50)), LtvStatus::Healthy);
        assert_eq!(calc.get_ltv_status(Rate::from_percentage(66)), LtvStatus::Warning);
        assert_eq!(calc.get_ltv_status(Rate::from_percentage(71)), LtvStatus::MarginCall);
        assert_eq!(calc.get_ltv_status(Rate::from_percentage(76)), LtvStatus::Liquidation);
    }
    
    #[test]
    fn test_required_collateral_calculation() {
        let calc = LtvCalculator::new(Uuid::new_v4(), create_test_thresholds());
        
        let cvl = Money::from_major(70_000);
        let current_collateral = Money::from_major(90_000);
        let target_ltv = Rate::from_percentage(70);
        
        let required = calc.calculate_required_collateral(cvl, current_collateral, target_ltv);
        assert_eq!(required, Money::from_major(10_000));
    }
    
    #[test]
    fn test_ltv_monitoring() {
        let time = SafeTimeProvider::new(TimeSource::Test(
            Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap()
        ));
        let control = time.test_control().unwrap();
        
        let mut monitor = LtvMonitor::new(
            Uuid::new_v4(),
            create_test_thresholds(),
            Duration::hours(1),
        );
        
        // healthy ltv
        let status = monitor.check_ltv(
            Money::from_major(50_000),
            Money::from_major(100_000),
            &time,
        ).unwrap();
        assert_eq!(status, LtvStatus::Healthy);
        
        // advance time and breach warning
        control.advance(Duration::hours(2));
        let status = monitor.check_ltv(
            Money::from_major(66_000),
            Money::from_major(100_000),
            &time,
        ).unwrap();
        assert_eq!(status, LtvStatus::Warning);
        
        // check events
        let events = monitor.take_events();
        assert_eq!(events.len(), 1);
    }
    
    #[test]
    fn test_margin_call_lifecycle() {
        let time = SafeTimeProvider::new(TimeSource::Test(
            Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap()
        ));
        let control = time.test_control().unwrap();
        
        let mut monitor = LtvMonitor::new(
            Uuid::new_v4(),
            create_test_thresholds(),
            Duration::hours(1),
        );
        
        // trigger margin call
        control.advance(Duration::hours(2));
        let status = monitor.check_ltv(
            Money::from_major(71_000),
            Money::from_major(100_000),
            &time,
        ).unwrap();
        assert_eq!(status, LtvStatus::MarginCall);
        assert!(monitor.is_margin_call_active());
        
        // resolve margin call
        control.advance(Duration::hours(2));
        let status = monitor.check_ltv(
            Money::from_major(50_000),
            Money::from_major(100_000),
            &time,
        ).unwrap();
        assert_eq!(status, LtvStatus::Healthy);
        assert!(!monitor.is_margin_call_active());
        
        let events = monitor.take_events();
        assert!(events.iter().any(|e| matches!(e, Event::MarginCallRequired { .. })));
        assert!(events.iter().any(|e| matches!(e, Event::MarginCallResolved { .. })));
    }
}