Skip to content

Commit

Permalink
fixing build after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Jan 4, 2022
1 parent b96ecaa commit 1e82327
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 216 deletions.
314 changes: 157 additions & 157 deletions Cargo.lock

Large diffs are not rendered by default.

Binary file removed frame/composable-traits/curve_model_plotter.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed frame/composable-traits/jump_model.png
Binary file not shown.
Binary file removed frame/composable-traits/jump_model_plotter.png
Binary file not shown.
26 changes: 17 additions & 9 deletions frame/composable-traits/src/lending/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ impl InterestRateModel {
proportional_parameter: FixedI128,
integral_parameter: FixedI128,
derivative_parameter: FixedI128,
initial_interest_rate: FixedU128,
target_utilization: FixedU128,
) -> Option<Self> {
DynamicPIDControllerModel::new(
proportional_parameter,
integral_parameter,
derivative_parameter,
target_utilization,
initial_interest_rate,
target_utilization,
)
.map(Self::DynamicPIDController)
}
Expand Down Expand Up @@ -285,11 +287,16 @@ impl DynamicPIDControllerModel {
//compute integral term `it = it_1 + ki * et`
let it = self
.previous_integral_term
.checked_add(&self.integral_parameter.checked_mul(&et).ok_or(ArithmeticError::Overflow)?)
.checked_add(
&self.integral_parameter.checked_mul(&et).ok_or(ArithmeticError::Overflow)?,
)
.ok_or(ArithmeticError::Overflow)?;
self.previous_integral_term = it;
// compute derivative term `dt = kd * (et - et_1)`
let dt = self.derivative_parameter.checked_mul(&(et - self.previous_error_value)).ok_or(ArithmeticError::Overflow)?;
let dt = self
.derivative_parameter
.checked_mul(&(et - self.previous_error_value))
.ok_or(ArithmeticError::Overflow)?;
self.previous_error_value = et;

// compute u(t), control value `ut = pt + it + dt`
Expand All @@ -313,15 +320,16 @@ impl DynamicPIDControllerModel {
proportional_parameter: FixedI128,
integral_parameter: FixedI128,
derivative_parameter: FixedI128,
initial_interest_rate: FixedU128,
target_utilization: FixedU128,
) -> Option<DynamicPIDControllerModel> {
Some(DynamicPIDControllerModel {
proportional_parameter,
integral_parameter,
derivative_parameter,
previous_error_value : <_>::zero(),
previous_integral_term : <_>::zero(),
previous_interest_rate : <_>::zero(),
previous_error_value: <_>::zero(),
previous_integral_term: <_>::zero(),
previous_interest_rate: initial_interest_rate,
target_utilization,
})
}
Expand Down Expand Up @@ -364,12 +372,12 @@ impl DoubleExponentModel {

impl InterestRate for DoubleExponentModel {
fn get_borrow_rate(&mut self, utilization: Percent) -> Option<Rate> {
// as per model
// as per model
let mut polynomial: FixedU128 = utilization.into();
let mut result = FixedU128::saturating_from_integer(self.coefficients[0]);
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;
polynomial = polynomial * polynomial;
}
let maximal = FixedU128::saturating_from_integer(EXPECTED_COEFFICIENTS_SUM);
Some(result / maximal)
Expand Down
80 changes: 41 additions & 39 deletions frame/composable-traits/src/lending/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

use crate::defi::Rate;

use super::*;
use proptest::{prop_assert, strategy::Strategy, test_runner::TestRunner};
use sp_runtime::{
traits::{One, Saturating, Zero},
FixedI128, FixedPointNumber, FixedU128,
FixedPointNumber, FixedU128,
};

// Test jump model
Expand Down Expand Up @@ -60,7 +59,6 @@ fn get_borrow_rate_works() {
);
}


#[test]
fn get_supply_rate_works() {
let borrow_rate = Rate::saturating_from_rational(2, 100);
Expand Down Expand Up @@ -223,10 +221,11 @@ fn jump_model_plotter() {
.build_cartesian_2d(0.0..100.0, 0.0..100.0)
.unwrap();
chart
.configure_mesh()
.x_desc("Utilization ratio %")
.y_desc("Borrow rate %")
.draw().unwrap();
.configure_mesh()
.x_desc("Utilization ratio %")
.y_desc("Borrow rate %")
.draw()
.unwrap();
chart
.draw_series(LineSeries::new(
(0..=100).map(|x| {
Expand Down Expand Up @@ -255,10 +254,11 @@ fn curve_model_plotter() {
.build_cartesian_2d(0.0..100.0, 0.0..100.0)
.unwrap();
chart
.configure_mesh()
.x_desc("Utilization ratio %")
.y_desc("Borrow rate %")
.draw().unwrap();
.configure_mesh()
.x_desc("Utilization ratio %")
.y_desc("Borrow rate %")
.draw()
.unwrap();
chart
.draw_series(LineSeries::new(
(0..=100).map(|x| {
Expand All @@ -271,18 +271,27 @@ fn curve_model_plotter() {
.unwrap();
}

// ISSUE: given ideal real interest rate, borrow rate growth infinitely, which is wrong
#[cfg(feature = "visualization")]
#[test]
fn dynamic_pid_model_plotter() {
use plotters::prelude::*;
let proportional_parameter = FixedI128::saturating_from_integer(1);
let integral_parameter = FixedI128::saturating_from_integer(3);
let derivative_parameter = FixedI128::saturating_from_integer(2);
let proportional_parameter = FixedI128::saturating_from_rational(40, 100);
let integral_parameter = FixedI128::saturating_from_rational(50, 100);
let derivative_parameter = FixedI128::saturating_from_rational(30, 100);
let target_utilization = FixedU128::saturating_from_rational(80, 100);
let mut model =
DynamicPIDControllerModel::new(proportional_parameter, integral_parameter, derivative_parameter, target_utilization).unwrap();
let initial_interest_rate = FixedU128::saturating_from_rational(13, 100);
let mut model = DynamicPIDControllerModel::new(
proportional_parameter,
integral_parameter,
derivative_parameter,
initial_interest_rate,
target_utilization,
)
.unwrap();

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

let mut chart = ChartBuilder::on(&area)
Expand All @@ -291,53 +300,45 @@ fn dynamic_pid_model_plotter() {
.set_label_area_size(LabelAreaPosition::Right, 100)
.build_cartesian_2d(0.0..100.0, 0.0..150.0)
.unwrap();
chart
.configure_mesh()
.x_desc("Time")
.y_desc("%")
.draw().unwrap();

chart.configure_mesh().x_desc("Time").y_desc("%").draw().unwrap();

chart
.draw_series(LineSeries::new(
[
0, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
0, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
]
.iter()
.enumerate()
.map(|(i, x)| {
let utilization = Percent::from_percent(*x);
let rate = model.get_borrow_rate(utilization).unwrap();
(i as f64, rate.to_float() )
(i as f64, rate.to_float())
}),
&RED,
))
.unwrap()
.label("Interest rate %");


chart
.draw_series(LineSeries::new(
[
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
]
.iter()
.enumerate()
.map(|(i, x)| {
(i as f64, *x as f64)
}),
.map(|(i, x)| (i as f64, *x as f64)),
&BLUE,
))
.unwrap()
.label("Target Utilization ratio %");

chart.configure_series_labels().border_style(&BLACK).draw().unwrap();
chart.configure_series_labels().border_style(&BLACK).draw().unwrap();
}

#[cfg(feature = "visualization")]
Expand All @@ -346,11 +347,12 @@ fn double_exponents_model_plotter() {
use plotters::prelude::*;
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();
let area =
BitMapBackend::new("./double_exponents_model_plotter.png", (1024, 768)).into_drawing_area();
area.fill(&WHITE).unwrap();

let mut chart = ChartBuilder::on(&area)
.set_label_area_size(LabelAreaPosition::Left, 50)
.set_label_area_size(LabelAreaPosition::Left, 50)
.set_label_area_size(LabelAreaPosition::Bottom, 50)
.build_cartesian_2d(0.0..100.0, 0.0..100.0)
.unwrap();
Expand Down
7 changes: 3 additions & 4 deletions frame/lending/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!
//! Lending pallet

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(
Expand Down Expand Up @@ -33,7 +33,6 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub mod math;

mod models;

Expand All @@ -45,7 +44,7 @@ pub mod pallet {
use codec::{Codec, FullCodec};
use composable_traits::{
currency::{CurrencyFactory, PriceableAsset},
lending::{BorrowAmountOf, CollateralLpAmountOf, Lending, MarketConfig, MarketConfigInput},
lending::{BorrowAmountOf, CollateralLpAmountOf, Lending, MarketConfig, MarketConfigInput, math::*},
liquidation::Liquidation,
loans::{DurationSeconds, PriceStructure, Timestamp},
math::{LiftedFixedBalance, SafeArithmetic},
Expand Down Expand Up @@ -1288,7 +1287,7 @@ pub mod pallet {
cash: &Self::Balance,
borrows: &Self::Balance,
) -> Result<Percent, DispatchError> {
Ok(composable_traits::rate_model::calc_utilization_ratio(
Ok(composable_traits::lending::math::calc_utilization_ratio(
(*cash).into(),
(*borrows).into(),
)?)
Expand Down
3 changes: 2 additions & 1 deletion frame/lending/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use composable_traits::{
math::{LiftedFixedBalance, SafeArithmetic},
rate_model::{NormalizedCollateralFactor, Rate},
lending::math::NormalizedCollateralFactor,
defi::Rate,
};
use sp_runtime::{traits::Saturating, ArithmeticError, Percent};

Expand Down
7 changes: 3 additions & 4 deletions frame/lending/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use crate::{
use composable_tests_helpers::{prop_assert_acceptable_computation_error, prop_assert_ok};
use composable_traits::{
currency::PriceableAsset,
lending::MarketConfigInput,
lending::{MarketConfigInput, math::*},
math::LiftedFixedBalance,
rate_model::*,
vault::{Deposit, VaultConfig},
vault::{Deposit, VaultConfig}, defi::Rate,
};
use frame_support::{
assert_noop, assert_ok,
Expand Down Expand Up @@ -324,7 +323,7 @@ fn new_jump_model() -> (Percent, InterestRateModel) {
let full_rate = Rate::saturating_from_rational(32, 100);
let optimal = Percent::from_percent(80);
let interest_rate_model = InterestRateModel::Jump(
JumpModel::new_model(base_rate, jump_rate, full_rate, optimal).unwrap(),
JumpModel::new(base_rate, jump_rate, full_rate, optimal).unwrap(),
);
(optimal, interest_rate_model)
}
Expand Down
3 changes: 1 addition & 2 deletions frame/vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ pub mod pallet {
};
use codec::{Codec, FullCodec};
use composable_traits::{
rate_model::Rate,
vault::{
CapabilityVault, Deposit, FundsAvailability, ReportableStrategicVault, Vault,
VaultConfig,
},
}, defi::Rate,
};
use frame_support::{
dispatch::DispatchResultWithPostInfo,
Expand Down

0 comments on commit 1e82327

Please sign in to comment.