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
// Copyright 2022 Interlay.
// This file is part of Interlay.

// Copyright 2021 Parallel Finance Developer.
// This file is part of Parallel Finance.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use codec::MaxEncodedLen;
use primitives::{Rate, Ratio};
use scale_info::TypeInfo;
use sp_runtime::traits::{CheckedAdd, CheckedDiv, CheckedSub, Saturating};

use crate::*;

/// Parallel interest rate model
#[derive(
    serde::Deserialize,
    serde::Serialize,
    Encode,
    Decode,
    Eq,
    PartialEq,
    Copy,
    Clone,
    RuntimeDebug,
    TypeInfo,
    MaxEncodedLen,
)]
pub enum InterestRateModel {
    Jump(JumpModel),
    Curve(CurveModel),
}

impl Default for InterestRateModel {
    fn default() -> Self {
        Self::new_jump_model(
            Rate::saturating_from_rational(2, 100),
            Rate::saturating_from_rational(10, 100),
            Rate::saturating_from_rational(32, 100),
            Ratio::from_percent(80),
        )
    }
}

impl InterestRateModel {
    pub fn new_jump_model(base_rate: Rate, jump_rate: Rate, full_rate: Rate, jump_utilization: Ratio) -> Self {
        Self::Jump(JumpModel::new_model(base_rate, jump_rate, full_rate, jump_utilization))
    }

    pub fn new_curve_model(base_rate: Rate) -> Self {
        Self::Curve(CurveModel::new_model(base_rate))
    }

    pub fn check_model(&self) -> bool {
        match self {
            Self::Jump(jump) => jump.check_model(),
            Self::Curve(curve) => curve.check_model(),
        }
    }

    /// Calculates the current borrow interest rate
    pub fn get_borrow_rate(&self, utilization: Ratio) -> Option<Rate> {
        match self {
            Self::Jump(jump) => jump.get_borrow_rate(utilization),
            Self::Curve(curve) => curve.get_borrow_rate(utilization),
        }
    }

    /// Calculates the current supply interest rate
    pub fn get_supply_rate(borrow_rate: Rate, util: Ratio, reserve_factor: Ratio) -> Rate {
        // ((1 - reserve_factor) * borrow_rate) * utilization
        let one_minus_reserve_factor = Ratio::one().saturating_sub(reserve_factor);
        let rate_to_pool = borrow_rate.saturating_mul(one_minus_reserve_factor.into());

        rate_to_pool.saturating_mul(util.into())
    }
}

/// The jump interest rate model
#[derive(
    serde::Deserialize,
    serde::Serialize,
    Encode,
    Decode,
    Eq,
    PartialEq,
    Copy,
    Clone,
    RuntimeDebug,
    TypeInfo,
    MaxEncodedLen,
)]
pub struct JumpModel {
    /// The base interest rate when utilization rate is 0
    pub base_rate: Rate,
    /// The interest rate on jump utilization point
    pub jump_rate: Rate,
    /// The max interest rate when utilization rate is 100%
    pub full_rate: Rate,
    /// The utilization point at which the jump_rate is applied
    pub jump_utilization: Ratio,
}

impl JumpModel {
    pub const MAX_BASE_RATE: Rate = Rate::from_inner(100_000_000_000_000_000); // 10%
    pub const MAX_JUMP_RATE: Rate = Rate::from_inner(1_000_000_000_000_000_000); // 100%
    pub const MAX_FULL_RATE: Rate = Rate::from_inner(5_000_000_000_000_000_000); // 500%

    /// Create a new rate model
    pub fn new_model(base_rate: Rate, jump_rate: Rate, full_rate: Rate, jump_utilization: Ratio) -> JumpModel {
        Self {
            base_rate,
            jump_rate,
            full_rate,
            jump_utilization,
        }
    }

    /// Check the jump model for sanity
    pub fn check_model(&self) -> bool {
        if self.base_rate > Self::MAX_BASE_RATE
            || self.jump_rate > Self::MAX_JUMP_RATE
            || self.full_rate > Self::MAX_FULL_RATE
        {
            return false;
        }
        if self.base_rate > self.jump_rate || self.jump_rate > self.full_rate {
            return false;
        }

        true
    }

    /// Calculates the borrow interest rate of jump model
    pub fn get_borrow_rate(&self, utilization: Ratio) -> Option<Rate> {
        if utilization <= self.jump_utilization {
            // utilization * (jump_rate - zero_rate) / jump_utilization + zero_rate
            let result = self
                .jump_rate
                .checked_sub(&self.base_rate)?
                .saturating_mul(utilization.into())
                .checked_div(&self.jump_utilization.into())?
                .checked_add(&self.base_rate)?;

            Some(result)
        } else {
            // (utilization - jump_utilization)*(full_rate - jump_rate) / ( 1 - jump_utilization) + jump_rate
            let excess_util = utilization.saturating_sub(self.jump_utilization);
            let result = self
                .full_rate
                .checked_sub(&self.jump_rate)?
                .saturating_mul(excess_util.into())
                .checked_div(&(Ratio::one().saturating_sub(self.jump_utilization).into()))?
                .checked_add(&self.jump_rate)?;

            Some(result)
        }
    }
}

/// The curve interest rate model
#[derive(
    serde::Deserialize,
    serde::Serialize,
    Encode,
    Decode,
    Eq,
    PartialEq,
    Copy,
    Clone,
    RuntimeDebug,
    TypeInfo,
    MaxEncodedLen,
)]
pub struct CurveModel {
    pub base_rate: Rate,
}

impl CurveModel {
    pub const MAX_BASE_RATE: Rate = Rate::from_inner(100_000_000_000_000_000); // 10%

    /// Create a new curve model
    pub fn new_model(base_rate: Rate) -> CurveModel {
        Self { base_rate }
    }

    /// Check the curve model for sanity
    pub fn check_model(&self) -> bool {
        self.base_rate <= Self::MAX_BASE_RATE
    }

    /// Calculates the borrow interest rate of curve model
    pub fn get_borrow_rate(&self, utilization: Ratio) -> Option<Rate> {
        const NINE: usize = 9;
        let utilization_rate: Rate = utilization.into();
        utilization_rate.saturating_pow(NINE).checked_add(&self.base_rate)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sp_runtime::FixedU128;

    // Test jump model
    #[test]
    fn init_jump_model_works() {
        let base_rate = Rate::saturating_from_rational(2, 100);
        let jump_rate = Rate::saturating_from_rational(10, 100);
        let full_rate = Rate::saturating_from_rational(32, 100);
        let jump_utilization = Ratio::from_percent(80);

        assert_eq!(
            JumpModel::new_model(base_rate, jump_rate, full_rate, jump_utilization),
            JumpModel {
                base_rate: Rate::from_inner(20_000_000_000_000_000).into(),
                jump_rate: Rate::from_inner(100_000_000_000_000_000).into(),
                full_rate: Rate::from_inner(320_000_000_000_000_000).into(),
                jump_utilization: Ratio::from_percent(80),
            }
        );
    }

    #[test]
    fn get_borrow_rate_works() {
        // init
        let base_rate = Rate::saturating_from_rational(2, 100);
        let jump_rate = Rate::saturating_from_rational(10, 100);
        let full_rate = Rate::saturating_from_rational(32, 100);
        let jump_utilization = Ratio::from_percent(80);
        let jump_model = JumpModel::new_model(base_rate, jump_rate, full_rate, jump_utilization);
        assert!(jump_model.check_model());

        // normal rate
        let mut cash: u128 = 500;
        let borrows: u128 = 1000;
        let util = Ratio::from_rational(borrows, cash + borrows);
        let borrow_rate = jump_model.get_borrow_rate(util).unwrap();
        assert_eq!(
            borrow_rate,
            jump_model.jump_rate.saturating_mul(util.into()) + jump_model.base_rate,
        );

        // jump rate
        cash = 100;
        let util = Ratio::from_rational(borrows, cash + borrows);
        let borrow_rate = jump_model.get_borrow_rate(util).unwrap();
        let normal_rate = jump_model.jump_rate.saturating_mul(jump_utilization.into()) + jump_model.base_rate;
        let excess_util = util.saturating_sub(jump_utilization);
        assert_eq!(
            borrow_rate,
            (jump_model.full_rate - jump_model.jump_rate).saturating_mul(excess_util.into())
                / FixedU128::saturating_from_rational(20, 100)
                + normal_rate,
        );
    }

    // Test curve model
    // TODO: Add test cases for curve model

    #[test]
    fn get_supply_rate_works() {
        let borrow_rate = Rate::saturating_from_rational(2, 100);
        let util = Ratio::from_percent(50);
        let reserve_factor = Ratio::zero();
        let supply_rate = InterestRateModel::get_supply_rate(borrow_rate, util, reserve_factor);
        assert_eq!(
            supply_rate,
            borrow_rate.saturating_mul(((Ratio::one().saturating_sub(reserve_factor)) * util).into()),
        );
    }

    #[test]
    fn curve_model_correctly_calculates_borrow_rate() {
        let model = CurveModel::new_model(Rate::saturating_from_rational(2, 100));
        assert_eq!(
            model.get_borrow_rate(Ratio::from_percent(80)).unwrap(),
            Rate::from_inner(154217728000000000)
        );
    }
}