Rate Recalculation

The interest rates are calculated based on the total_deposit, total_debt, and Interest Rate Model. The Interest Rate Model is a vector of function parameters that maps reserve utilization rate into the debt rate.

Reserve utilization rate is a quotient of total_debt by total_deposit.

R(M,U)={100ā‹…M[1]ā‹…U/68Uāˆˆ[0,0.68),M[1]+100(M[2]āˆ’M[1])(Uāˆ’0.68)/16Uāˆˆ[0.68,0.84),M[2]+100(M[3]āˆ’M[2])(Uāˆ’0.84)/8Uāˆˆ[0.84,0.92),M[3]+100(M[4]āˆ’M[3])(Uāˆ’0.92)/4Uāˆˆ[0.92,0.96),M[4]+100(M[5]āˆ’M[4])(Uāˆ’0.96)/2Uāˆˆ[0.96,0.98),M[5]+100(M[6]āˆ’M[5])(Uāˆ’0.98)Uāˆˆ[0.98,0.99),M[6]+100(M[7]āˆ’M[6])(Uāˆ’0.99)Uāˆˆ[0.99,1),M[7]ā‹…UUāˆˆ[1,āˆž).\begin{equation} { R(M,U) = \begin{cases} 100 \cdot M[1] \cdot U/68 & U \in [0, 0.68), \\ M[1] + 100(M[2]-M[1])(U - 0.68) / 16 & U \in [0.68, 0.84), \\ M[2] + 100(M[3]-M[2])(U - 0.84) / 8 & U \in [0.84, 0.92), \\ M[3] + 100(M[4]-M[3])(U - 0.92) / 4 & U \in [0.92, 0.96), \\ M[4] + 100(M[5]-M[4])(U - 0.96) / 2& U \in [0.96, 0.98), \\ M[5] + 100(M[6]-M[5])(U - 0.98) & U \in [0.98, 0.99), \\ M[6] + 100(M[7]-M[6])(U - 0.99) & U \in [0.99, 1), \\ M[7] \cdot U & U \in [1, \infty ). \\ \end{cases}} \end{equation}

The debt_rate is given by the function R(M,U) where U is an utilization rate and M is Interest Rate Model (counting vector entries from 1). The function is a piece-wise linear function that reaches the M[1], M[2], ..., M[7] at the ends of the intervals.

**Inputs:**
- `reserve_data`: mutable reference to `ReserveData` struct
- `interest_rate_model`: array reference of u64 with length 7 
    representing interest rate model

**Pseudocode:**
1. If `reserve_data.total_debt` is 0:
    a. Set `reserve_data.current_debt_rate_e18` to 0.
    b. Set `reserve_data.current_deposit_rate_e18` to 0.
    c. Return.
2. Calculate `utilization_rate_e6` =
    10^6 * `reserve_data.total_debt` / 'reserve_data.total_deposit'.
    round in such way that the result is not lesser than the "precise result".
    
3. Calculate `reserve_data.current_debt_rate_e18` using 
    `utilization_rate_to_interest_rate_e18` function.
4. If `reserve_data.total_deposit` is not 0 then:
    a. Calculate `reserve_data.current_deposit_rate_e18` = 
        reserve_data.total_debt * reserve_data.current_debt_rate_e18 /
        reserve_data.total_deposit.
    round in such way that the result is not greater than the "precise result".
   else, set `self.current_deposit_rate_e18` to 0.

Last updated