Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-1m98wce lending docs and fixes #441

Merged
merged 8 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified frame/composable-traits/double_exponents_model_plotter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frame/composable-traits/dynamic_pid_model_plotter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frame/composable-traits/jump_model_plotter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 18 additions & 20 deletions frame/composable-traits/src/lending/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub trait InterestRate {
fn get_borrow_rate(&mut self, utilization: Percent) -> Option<Rate>;
}

/// Parallel interest rate model
// TODO: all these are MayBeModels after SCALE decode, need to map to Models after validation
/// Interest rate models
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, TypeInfo)]
pub enum InterestRateModel {
Expand Down Expand Up @@ -120,6 +121,7 @@ impl InterestRateModel {
}
}

// TODO: Use enum_dispatch crate
impl InterestRate for InterestRateModel {
/// Calculates the current borrow interest rate
fn get_borrow_rate(&mut self, utilization: Percent) -> Option<Rate> {
Expand Down Expand Up @@ -265,7 +267,7 @@ pub struct DynamicPIDControllerModel {
previous_integral_term: FixedI128,
/// `ir_t_1`
previous_interest_rate: FixedU128,
/// uo
/// `uo`
target_utilization: FixedU128,
}

Expand Down Expand Up @@ -293,9 +295,11 @@ impl DynamicPIDControllerModel {
// compute u(t), control value `ut = pt + it + dt`
let ut = pt + it + dt;
// update interest_rate `ir = ir_t_1 + ut`
self.previous_interest_rate = self
.previous_interest_rate
.saturating_add(FixedU128::from_inner(ut.into_inner().try_into().unwrap_or(0u128))).max(FixedU128::zero());


self.previous_interest_rate = FixedU128::from_inner(self
.previous_interest_rate.into_inner().try_into().unwrap_or(0i128)
.saturating_add(&ut).into_inner().try_into().unwrap_or(0u128));
Ok(self.previous_interest_rate)
}

Expand Down Expand Up @@ -333,8 +337,6 @@ const EXPECTED_COEFFICIENTS_SUM: u16 = 100;
/// The double exponent interest rate model
/// Interest based on a polynomial of the utilization of the market.
/// Interest = C_0 + C_1 * U^(2^0) + C_2 * U^(2^1) + C_3 * U^(2^2) ...
/// C_0, C_1, C_2, ... coefficients are passed as packed u128.
/// Each coefficient is of 8 byte. C_0 is at LSB and C_15 at MSB.
/// For reference check https://github.com/dydxprotocol/solo/blob/master/contracts/external/interestsetters/DoubleExponentInterestSetter.sol
/// https://web.archive.org/web/20210518033618/https://help.dydx.exchange/en/articles/2924246-how-do-interest-rates-work
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
Expand All @@ -356,19 +358,15 @@ impl DoubleExponentModel {

impl InterestRate for DoubleExponentModel {
fn get_borrow_rate(&mut self, utilization: Percent) -> Option<Rate> {
let polynomial_0: FixedU128 = FixedU128::one();
let result_0: Rate = Rate::zero();
let res =
self.coefficients
.iter()
.try_fold((result_0, polynomial_0), |(res, poly), coeff| {
let coefficient = FixedU128::from_inner(u128::from(*coeff));
let result = res.checked_add(&coefficient.checked_mul(&poly)?)?;
let polynomial = poly.checked_mul(&utilization.into())?;
Some((result, polynomial))
});
res.map(|(r, _p)| r.checked_div(&FixedU128::from_inner(EXPECTED_COEFFICIENTS_SUM.into())))
.unwrap()
// as per model
vivekvpandya marked this conversation as resolved.
Show resolved Hide resolved
let mut polynomial: FixedU128 = utilization.into();
let mut result = FixedU128::saturating_from_integer(self.coefficients[0]);
for i in 1..self.coefficients.len() {
result = result + FixedU128::saturating_from_integer(self.coefficients[i]) * polynomial;
polynomial = polynomial* polynomial;
}
let maximal = FixedU128::saturating_from_integer(EXPECTED_COEFFICIENTS_SUM);
Some(result / maximal)
}
}

Expand Down
18 changes: 8 additions & 10 deletions frame/composable-traits/src/lending/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn jump_model_plotter() {
let optimal = Percent::from_percent(80);
let mut model = JumpModel::new(base_rate, jump_rate, full_rate, optimal).unwrap();

let area = BitMapBackend::new("./jump_model.png", (1024, 768)).into_drawing_area();
let area = BitMapBackend::new("./jump_model_plotter.png", (1024, 768)).into_drawing_area();
area.fill(&WHITE).unwrap();

let mut chart = ChartBuilder::on(&area)
Expand Down Expand Up @@ -275,9 +275,9 @@ fn curve_model_plotter() {
#[test]
fn dynamic_pid_model_plotter() {
use plotters::prelude::*;
let proportional_parameter = FixedI128::saturating_from_integer(5);
let integral_parameter = FixedI128::saturating_from_integer(0);
let derivative_parameter = FixedI128::saturating_from_integer(0);
let proportional_parameter = FixedI128::saturating_from_integer(1);
let integral_parameter = FixedI128::saturating_from_integer(1);
let derivative_parameter = FixedI128::saturating_from_integer(1);
let target_utilization = FixedU128::saturating_from_rational(80, 100);
let mut model =
DynamicPIDControllerModel::new(proportional_parameter, integral_parameter, derivative_parameter, target_utilization).unwrap();
Expand All @@ -298,12 +298,10 @@ fn dynamic_pid_model_plotter() {
chart
.draw_series(LineSeries::new(
[
50, 55, 51, 57, 60, 66, 66, 66, 66, 77, 78, 50, 78, 88, 88, 90, 78, 79, 74, 74, 80,
80, 62, 59, 58, 59, 58, 60, 61, 62, 62, 62, 63, 80, 85, 99, 80, 81, 82, 60, 60, 40,
30, 31, 32, 40, 50, 51, 51, 40, 50, 60, 66, 69, 60, 80, 70, 70, 77, 70, 60, 56, 52,
50, 45, 44, 40, 30, 10, 30, 40, 50, 60, 70, 71, 71, 71, 70, 80, 80, 90, 91, 90, 91,
99, 55, 51, 57, 60, 66, 66, 66, 66, 77, 78, 50, 78, 88, 88, 90, 78, 79, 74, 74, 80,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 99,90,90,90,90,90,90,
90, 91, 90, 91, 90, 91, 90, 91, 90, 91, 92, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 90, 90, 90, 80, 80, 70, 71, 70, 71, 70, 71, 70, 71, 70, 68, 67,
90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 67,
66, 65, 64, 63, 62, 61, 50, 50, 40, 30,
]
.iter()
Expand All @@ -322,7 +320,7 @@ fn dynamic_pid_model_plotter() {
#[test]
fn double_exponents_model_plotter() {
use plotters::prelude::*;
let coefficients: [u8; 16] = [10, 40, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let coefficients: [u8; 16] = [10, 10, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let mut model = DoubleExponentModel::new(coefficients).unwrap();
let area = BitMapBackend::new("./double_exponents_model_plotter.png", (1024, 768)).into_drawing_area();
area.fill(&WHITE).unwrap();
Expand Down