mars-core 1.0.0

Mars is a fully automated, on-chain credit protocol built on Terra and governed by a decentralised community of users and developers
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use cosmwasm_std::{Env, StdResult};

use crate::math::decimal::Decimal;

use super::Market;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum InterestRateModel {
    Dynamic {
        params: DynamicInterestRateModelParams,
        state: DynamicInterestRateModelState,
    },
    Linear {
        params: LinearInterestRateModelParams,
    },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum InterestRateModelParams {
    Dynamic(DynamicInterestRateModelParams),
    Linear(LinearInterestRateModelParams),
}

impl InterestRateModelParams {
    pub fn validate(&self) -> Result<(), InterestRateModelError> {
        match self {
            InterestRateModelParams::Dynamic(dynamic) => dynamic.validate(),
            InterestRateModelParams::Linear(linear) => linear.validate(),
        }
    }
}

#[derive(Error, Debug, PartialEq)]
pub enum InterestRateModelError {
    #[error("max_borrow_rate should be greater than or equal to min_borrow_rate. max_borrow_rate: {max_borrow_rate:?}, min_borrow_rate: {min_borrow_rate:?}")]
    InvalidMinMaxBorrowRate {
        max_borrow_rate: Decimal,
        min_borrow_rate: Decimal,
    },

    #[error("Optimal utilization rate can't be greater than one")]
    InvalidOptimalUtilizationRate {},
}

pub fn init_interest_rate_model(
    params: InterestRateModelParams,
    current_block_time: u64,
) -> Result<InterestRateModel, InterestRateModelError> {
    params.validate()?;

    match params {
        InterestRateModelParams::Dynamic(dynamic_params) => {
            let state = DynamicInterestRateModelState {
                txs_since_last_borrow_rate_update: 0,
                borrow_rate_last_updated: current_block_time,
            };

            Ok(InterestRateModel::Dynamic {
                params: dynamic_params,
                state,
            })
        }
        InterestRateModelParams::Linear(linear_params) => Ok(InterestRateModel::Linear {
            params: linear_params,
        }),
    }
}

/// Updates market with new borrow/liquidity and interest rate model state
pub fn update_market_interest_rates_with_model(
    env: &Env,
    market: &mut Market,
    current_utilization_rate: Decimal,
) -> StdResult<()> {
    // update borrow rate
    match market.interest_rate_model {
        InterestRateModel::Dynamic {
            ref params,
            ref mut state,
        } => {
            let current_block_time = env.block.time.seconds();

            // update tx count and determine if borrow rate should be updated
            state.txs_since_last_borrow_rate_update += 1;
            let seconds_since_last_borrow_rate_update =
                current_block_time - state.borrow_rate_last_updated;

            let threshold_is_met = (state.txs_since_last_borrow_rate_update
                >= params.update_threshold_txs)
                || (seconds_since_last_borrow_rate_update >= params.update_threshold_seconds);

            // don't allow more than one update in the same block
            // this prevents calling the contract multiple times to set interest to min or max
            // on a single block.
            let should_update_borrow_rate =
                threshold_is_met && (seconds_since_last_borrow_rate_update != 0);

            if should_update_borrow_rate {
                market.borrow_rate =
                    dynamic_get_borrow_rate(params, current_utilization_rate, market.borrow_rate)?;
                state.txs_since_last_borrow_rate_update = 0;
                state.borrow_rate_last_updated = current_block_time;
            }
        }

        InterestRateModel::Linear { ref params } => {
            market.borrow_rate = linear_get_borrow_rate(params, current_utilization_rate)?;
        }
    }

    // update liquidity rate
    market.liquidity_rate = get_liquidity_rate(
        market.borrow_rate,
        current_utilization_rate,
        market.reserve_factor,
    )?;

    Ok(())
}

pub fn get_liquidity_rate(
    borrow_rate: Decimal,
    current_utilization_rate: Decimal,
    reserve_factor: Decimal,
) -> StdResult<Decimal> {
    borrow_rate
        .checked_mul(current_utilization_rate)?
        // This operation should not underflow as reserve_factor is checked to be <= 1
        .checked_mul(Decimal::one() - reserve_factor)
}

// DYNAMIC

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DynamicInterestRateModelParams {
    /// Minimum borrow rate
    pub min_borrow_rate: Decimal,
    /// Maximum borrow rate
    pub max_borrow_rate: Decimal,

    /// Optimal utilization rate targeted by the PID controller. Interest rate will decrease when lower and increase when higher
    pub optimal_utilization_rate: Decimal,

    /// Proportional parameter for the PID controller
    pub kp_1: Decimal,
    /// Kp value when error threshold is exceeded
    pub kp_2: Decimal,
    /// Min error that triggers Kp augmentation
    pub kp_augmentation_threshold: Decimal,

    /// Amount of transactions involving the market's interest update
    /// since last borrow rate update that will trigger
    /// the next borrow rate update
    pub update_threshold_txs: u32,
    /// Amount of seconds since last borrow rate update that will trigger
    /// the next borrow rate update when the next transaction involving the market's interest
    /// update happens
    pub update_threshold_seconds: u64,
}

impl DynamicInterestRateModelParams {
    pub fn validate(&self) -> Result<(), InterestRateModelError> {
        if self.min_borrow_rate > self.max_borrow_rate {
            return Err(InterestRateModelError::InvalidMinMaxBorrowRate {
                min_borrow_rate: self.min_borrow_rate,
                max_borrow_rate: self.max_borrow_rate,
            });
        }

        if self.optimal_utilization_rate > Decimal::one() {
            return Err(InterestRateModelError::InvalidOptimalUtilizationRate {});
        }

        Ok(())
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DynamicInterestRateModelState {
    pub txs_since_last_borrow_rate_update: u32,
    pub borrow_rate_last_updated: u64,
}

pub fn dynamic_get_borrow_rate(
    params: &DynamicInterestRateModelParams,
    current_utilization_rate: Decimal,
    current_borrow_rate: Decimal,
) -> StdResult<Decimal> {
    // error_value is unsigned
    // we use a boolean flag to determine the direction of the error
    let (error_value, error_positive) =
        if params.optimal_utilization_rate > current_utilization_rate {
            (
                params.optimal_utilization_rate - current_utilization_rate,
                true,
            )
        } else {
            (
                current_utilization_rate - params.optimal_utilization_rate,
                false,
            )
        };

    let kp = if error_value >= params.kp_augmentation_threshold {
        params.kp_2
    } else {
        params.kp_1
    };

    let p = kp.checked_mul(error_value)?;
    let mut new_borrow_rate = if error_positive {
        // error_positive = true (u_optimal > u) means we want utilization rate to go up
        // we lower interest rate so more people borrow
        if current_borrow_rate > p {
            current_borrow_rate - p
        } else {
            Decimal::zero()
        }
    } else {
        // error_positive = false (u_optimal < u) means we want utilization rate to go down
        // we increase interest rate so less people borrow
        current_borrow_rate + p
    };

    // Check borrow rate conditions
    if new_borrow_rate < params.min_borrow_rate {
        new_borrow_rate = params.min_borrow_rate
    } else if new_borrow_rate > params.max_borrow_rate {
        new_borrow_rate = params.max_borrow_rate;
    };

    Ok(new_borrow_rate)
}

// LINEAR

/// Linear interest rate model
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct LinearInterestRateModelParams {
    /// Optimal utilization rate
    pub optimal_utilization_rate: Decimal,
    /// Base rate
    pub base: Decimal,
    /// Slope parameter for interest rate model function when utilization_rate < optimal_utilization_rate
    pub slope_1: Decimal,
    /// Slope parameter for interest rate model function when utilization_rate >= optimal_utilization_rate
    pub slope_2: Decimal,
}

impl LinearInterestRateModelParams {
    pub fn validate(&self) -> Result<(), InterestRateModelError> {
        if self.optimal_utilization_rate > Decimal::one() {
            return Err(InterestRateModelError::InvalidOptimalUtilizationRate {});
        }

        Ok(())
    }
}

pub fn linear_get_borrow_rate(
    params: &LinearInterestRateModelParams,
    current_utilization_rate: Decimal,
) -> StdResult<Decimal> {
    let new_borrow_rate = if current_utilization_rate <= params.optimal_utilization_rate {
        if current_utilization_rate.is_zero() {
            // prevent division by zero when optimal_utilization_rate is zero
            params.base
        } else {
            // The borrow interest rates increase slowly with utilization
            params.base
                + params.slope_1.checked_mul(
                    current_utilization_rate.checked_div(params.optimal_utilization_rate)?,
                )?
        }
    } else {
        // The borrow interest rates increase sharply with utilization
        params.base
            + params.slope_1
            + params
                .slope_2
                .checked_mul(current_utilization_rate - params.optimal_utilization_rate)?
                .checked_div(Decimal::one() - params.optimal_utilization_rate)?
    };

    Ok(new_borrow_rate)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::math::decimal::Decimal;
    use crate::testing::mock_env_at_block_time;

    #[test]
    fn test_dynamic_model_lifecycle() {
        let optimal_utilization_rate = Decimal::percent(50);
        let kp_1 = Decimal::from_ratio(4u128, 100u128);
        let reserve_factor = Decimal::percent(10);

        let interest_rate_model_params = DynamicInterestRateModelParams {
            min_borrow_rate: Decimal::percent(0),
            max_borrow_rate: Decimal::percent(300),

            kp_1,
            optimal_utilization_rate,
            kp_augmentation_threshold: Decimal::percent(20),
            kp_2: Decimal::from_ratio(3u128, 1u128),

            update_threshold_txs: 2,
            update_threshold_seconds: 1000,
        };

        let time_start = 1_634_710_268;

        let interest_rate_model = init_interest_rate_model(
            InterestRateModelParams::Dynamic(interest_rate_model_params),
            time_start,
        )
        .unwrap();

        let mut market = Market {
            borrow_rate: Decimal::percent(10),
            liquidity_rate: Decimal::percent(10),
            reserve_factor: reserve_factor,
            interest_rate_model,
            ..Default::default()
        };

        // first update after 100 seconds borrow rate does not update
        {
            let diff = Decimal::percent(10);
            let utilization_rate = optimal_utilization_rate - diff;
            let previous_borrow_rate = market.borrow_rate;

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 100),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            assert_eq!(market.borrow_rate, previous_borrow_rate);
            assert_eq!(
                market.liquidity_rate,
                previous_borrow_rate
                    .checked_mul(utilization_rate)
                    .unwrap()
                    .checked_mul(Decimal::one() - reserve_factor)
                    .unwrap()
            );
            if let InterestRateModel::Dynamic { ref state, .. } = market.interest_rate_model {
                assert_eq!(state.borrow_rate_last_updated, time_start);
                assert_eq!(state.txs_since_last_borrow_rate_update, 1);
            } else {
                panic!("Wrong interest rate model type");
            }
        }

        // second update after 200 seconds borrow rate updates (because tx threshold is reached)
        {
            let diff = Decimal::percent(5);
            let utilization_rate = optimal_utilization_rate - diff;
            let previous_borrow_rate = market.borrow_rate;

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 200),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            let expected_borrow_rate = previous_borrow_rate - (kp_1.checked_mul(diff).unwrap());
            assert_eq!(market.borrow_rate, expected_borrow_rate);
            assert_eq!(
                market.liquidity_rate,
                expected_borrow_rate
                    .checked_mul(utilization_rate)
                    .unwrap()
                    .checked_mul(Decimal::one() - reserve_factor)
                    .unwrap()
            );
            if let InterestRateModel::Dynamic { ref state, .. } = market.interest_rate_model {
                assert_eq!(state.borrow_rate_last_updated, time_start + 200);
                assert_eq!(state.txs_since_last_borrow_rate_update, 0);
            } else {
                panic!("Wrong interest rate model type");
            }
        }

        // third update after 1201 seconds borrow rate updates (because seconds threshold is reached)
        {
            let diff = Decimal::percent(15);
            let utilization_rate = optimal_utilization_rate + diff;
            let previous_borrow_rate = market.borrow_rate;

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 1201),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            let expected_borrow_rate = previous_borrow_rate + (kp_1.checked_mul(diff).unwrap());
            assert_eq!(market.borrow_rate, expected_borrow_rate);
            assert_eq!(
                market.liquidity_rate,
                expected_borrow_rate
                    .checked_mul(utilization_rate)
                    .unwrap()
                    .checked_mul(Decimal::one() - reserve_factor)
                    .unwrap()
            );
            if let InterestRateModel::Dynamic { ref state, .. } = market.interest_rate_model {
                assert_eq!(state.borrow_rate_last_updated, time_start + 1201);
                assert_eq!(state.txs_since_last_borrow_rate_update, 0);
            } else {
                panic!("Wrong interest rate model type");
            }
        }

        //  do three after 1201 seconds, borrow rate does not update (because even though txs are
        //  reached, it is on the same block)
        {
            let diff = Decimal::percent(10);
            let utilization_rate = optimal_utilization_rate + diff;
            let previous_borrow_rate = market.borrow_rate;

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 1201),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 1201),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 1201),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            let expected_borrow_rate = previous_borrow_rate;
            assert_eq!(market.borrow_rate, expected_borrow_rate);
            assert_eq!(
                market.liquidity_rate,
                expected_borrow_rate
                    .checked_mul(utilization_rate)
                    .unwrap()
                    .checked_mul(Decimal::one() - reserve_factor)
                    .unwrap()
            );
            if let InterestRateModel::Dynamic { ref state, .. } = market.interest_rate_model {
                assert_eq!(state.borrow_rate_last_updated, time_start + 1201);
                assert_eq!(state.txs_since_last_borrow_rate_update, 3);
            } else {
                panic!("Wrong interest rate model type");
            }
        }

        // Updating after that works
        {
            let diff = Decimal::percent(15);
            let utilization_rate = optimal_utilization_rate + diff;
            let previous_borrow_rate = market.borrow_rate;

            update_market_interest_rates_with_model(
                &mock_env_at_block_time(time_start + 1208),
                &mut market,
                utilization_rate,
            )
            .unwrap();

            let expected_borrow_rate = previous_borrow_rate + (kp_1.checked_mul(diff).unwrap());
            assert_eq!(market.borrow_rate, expected_borrow_rate);
            assert_eq!(
                market.liquidity_rate,
                expected_borrow_rate
                    .checked_mul(utilization_rate)
                    .unwrap()
                    .checked_mul(Decimal::one() - reserve_factor)
                    .unwrap()
            );
            if let InterestRateModel::Dynamic { ref state, .. } = market.interest_rate_model {
                assert_eq!(state.borrow_rate_last_updated, time_start + 1208);
                assert_eq!(state.txs_since_last_borrow_rate_update, 0);
            } else {
                panic!("Wrong interest rate model type");
            }
        }
    }

    #[test]
    fn test_dynamic_borrow_rate_calculation() {
        let borrow_rate = Decimal::percent(5);
        let dynamic_ir_params = DynamicInterestRateModelParams {
            min_borrow_rate: Decimal::percent(1),
            max_borrow_rate: Decimal::percent(90),

            kp_1: Decimal::from_ratio(2u128, 1u128),
            optimal_utilization_rate: Decimal::percent(60),
            kp_augmentation_threshold: Decimal::percent(10),
            kp_2: Decimal::from_ratio(3u128, 1u128),

            update_threshold_txs: 1,
            update_threshold_seconds: 0,
        };

        // current utilization rate > optimal utilization rate
        {
            let current_utilization_rate = Decimal::percent(61);
            let new_borrow_rate =
                dynamic_get_borrow_rate(&dynamic_ir_params, current_utilization_rate, borrow_rate)
                    .unwrap();

            let expected_error =
                current_utilization_rate - dynamic_ir_params.optimal_utilization_rate;
            // we want to increase borrow rate to decrease utilization rate
            let expected_borrow_rate =
                borrow_rate + dynamic_ir_params.kp_1.checked_mul(expected_error).unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate < optimal utilization rate
        {
            let current_utilization_rate = Decimal::percent(59);
            let new_borrow_rate =
                dynamic_get_borrow_rate(&dynamic_ir_params, current_utilization_rate, borrow_rate)
                    .unwrap();

            let expected_error =
                dynamic_ir_params.optimal_utilization_rate - current_utilization_rate;
            // we want to decrease borrow rate to increase utilization rate
            let expected_borrow_rate =
                borrow_rate - Decimal::checked_mul(dynamic_ir_params.kp_1, expected_error).unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate > optimal utilization rate, increment KP by a multiplier if error goes beyond threshold
        {
            let current_utilization_rate = Decimal::percent(72);
            let new_borrow_rate =
                dynamic_get_borrow_rate(&dynamic_ir_params, current_utilization_rate, borrow_rate)
                    .unwrap();

            let expected_error =
                current_utilization_rate - dynamic_ir_params.optimal_utilization_rate;
            // we want to increase borrow rate to decrease utilization rate
            let expected_borrow_rate =
                borrow_rate + dynamic_ir_params.kp_2.checked_mul(expected_error).unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate < optimal utilization rate, borrow rate can't be less than min borrow rate
        {
            let current_utilization_rate = Decimal::percent(10);
            let new_borrow_rate =
                dynamic_get_borrow_rate(&dynamic_ir_params, current_utilization_rate, borrow_rate)
                    .unwrap();

            // we want to decrease borrow rate to increase utilization rate
            let expected_borrow_rate = dynamic_ir_params.min_borrow_rate;

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate > optimal utilization rate, borrow rate can't be less than max borrow rate
        {
            let current_utilization_rate = Decimal::percent(90);
            let new_borrow_rate =
                dynamic_get_borrow_rate(&dynamic_ir_params, current_utilization_rate, borrow_rate)
                    .unwrap();

            // we want to increase borrow rate to decrease utilization rate
            let expected_borrow_rate = dynamic_ir_params.max_borrow_rate;

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }
    }

    #[test]
    fn test_linear_model_lifecycle() {
        let optimal_utilization_rate = Decimal::percent(80);
        let reserve_factor = Decimal::percent(20);

        let interest_rate_model_params = LinearInterestRateModelParams {
            optimal_utilization_rate,
            base: Decimal::from_ratio(0u128, 100u128),
            slope_1: Decimal::from_ratio(7u128, 100u128),
            slope_2: Decimal::from_ratio(45u128, 100u128),
        };

        let interest_rate_model = init_interest_rate_model(
            InterestRateModelParams::Linear(interest_rate_model_params.clone()),
            123,
        )
        .unwrap();

        let mut market = Market {
            borrow_rate: Decimal::percent(10),
            liquidity_rate: Decimal::zero(),
            reserve_factor: reserve_factor,
            interest_rate_model,
            ..Default::default()
        };

        let diff = Decimal::percent(10);
        let utilization_rate = optimal_utilization_rate - diff;

        update_market_interest_rates_with_model(
            &mock_env_at_block_time(1234),
            &mut market,
            utilization_rate,
        )
        .unwrap();

        let expected_borrow_rate = interest_rate_model_params.base
            + interest_rate_model_params
                .slope_1
                .checked_mul(utilization_rate)
                .unwrap()
                .checked_div(interest_rate_model_params.optimal_utilization_rate)
                .unwrap();

        assert_eq!(market.borrow_rate, expected_borrow_rate);
        assert_eq!(
            market.liquidity_rate,
            expected_borrow_rate
                .checked_mul(utilization_rate)
                .unwrap()
                .checked_mul(Decimal::one() - reserve_factor)
                .unwrap()
        );
    }

    #[test]
    fn test_linear_interest_rates_calculation() {
        let linear_ir_params = LinearInterestRateModelParams {
            optimal_utilization_rate: Decimal::percent(80),
            base: Decimal::from_ratio(0u128, 100u128),
            slope_1: Decimal::from_ratio(7u128, 100u128),
            slope_2: Decimal::from_ratio(45u128, 100u128),
        };

        // current utilization rate < optimal utilization rate
        {
            let current_utilization_rate = Decimal::percent(79);
            let new_borrow_rate =
                linear_get_borrow_rate(&linear_ir_params, current_utilization_rate).unwrap();

            let expected_borrow_rate = linear_ir_params.base
                + linear_ir_params
                    .slope_1
                    .checked_mul(current_utilization_rate)
                    .unwrap()
                    .checked_div(linear_ir_params.optimal_utilization_rate)
                    .unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate == optimal utilization rate
        {
            let current_utilization_rate = Decimal::percent(80);
            let new_borrow_rate =
                linear_get_borrow_rate(&linear_ir_params, current_utilization_rate).unwrap();

            let expected_borrow_rate = linear_ir_params.base
                + linear_ir_params
                    .slope_1
                    .checked_mul(current_utilization_rate)
                    .unwrap()
                    .checked_div(linear_ir_params.optimal_utilization_rate)
                    .unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate >= optimal utilization rate
        {
            let current_utilization_rate = Decimal::percent(81);
            let new_borrow_rate =
                linear_get_borrow_rate(&linear_ir_params, current_utilization_rate).unwrap();

            let expected_borrow_rate = linear_ir_params.base
                + linear_ir_params.slope_1
                + linear_ir_params
                    .slope_2
                    .checked_mul(
                        current_utilization_rate - linear_ir_params.optimal_utilization_rate,
                    )
                    .unwrap()
                    .checked_div(Decimal::one() - linear_ir_params.optimal_utilization_rate)
                    .unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate == 100% and optimal utilization rate == 100%
        {
            let linear_ir_params = LinearInterestRateModelParams {
                optimal_utilization_rate: Decimal::percent(100),
                base: Decimal::from_ratio(0u128, 100u128),
                slope_1: Decimal::from_ratio(7u128, 100u128),
                slope_2: Decimal::from_ratio(0u128, 100u128),
            };

            let current_utilization_rate = Decimal::percent(100);
            let new_borrow_rate =
                linear_get_borrow_rate(&linear_ir_params, current_utilization_rate).unwrap();

            let expected_borrow_rate = Decimal::percent(7);

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate == 0% and optimal utilization rate == 0%
        {
            let linear_ir_params = LinearInterestRateModelParams {
                optimal_utilization_rate: Decimal::percent(0),
                base: Decimal::from_ratio(2u128, 100u128),
                slope_1: Decimal::from_ratio(7u128, 100u128),
                slope_2: Decimal::from_ratio(0u128, 100u128),
            };

            let current_utilization_rate = Decimal::percent(0);
            let new_borrow_rate =
                linear_get_borrow_rate(&linear_ir_params, current_utilization_rate).unwrap();

            let expected_borrow_rate = Decimal::percent(2);

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }

        // current utilization rate == 20% and optimal utilization rate == 0%
        {
            let linear_ir_params = LinearInterestRateModelParams {
                optimal_utilization_rate: Decimal::percent(0),
                base: Decimal::from_ratio(2u128, 100u128),
                slope_1: Decimal::from_ratio(1u128, 100u128),
                slope_2: Decimal::from_ratio(5u128, 100u128),
            };

            let current_utilization_rate = Decimal::percent(20);
            let new_borrow_rate =
                linear_get_borrow_rate(&linear_ir_params, current_utilization_rate).unwrap();

            let expected_borrow_rate = linear_ir_params.base
                + linear_ir_params.slope_1
                + linear_ir_params
                    .slope_2
                    .checked_mul(current_utilization_rate)
                    .unwrap();

            assert_eq!(new_borrow_rate, expected_borrow_rate);
        }
    }
}