-
Notifications
You must be signed in to change notification settings - Fork 84
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
Fix/linear pricing #1812
Fix/linear pricing #1812
Changes from 24 commits
3df5900
e004106
7b4ab74
9ad611b
9dc56e5
ca5ab9d
c002271
ec1a96b
2c2edc0
db35635
b1ef424
b4854b1
2aca1dd
c978951
e2dd3ca
9265bbb
967584d
2a691e9
45be927
8c01edc
3001d9d
b831f99
5fee771
fcddabf
b6976c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,12 @@ use sp_runtime::{ | |
traits::{EnsureAdd, EnsureFixedPointNumber, EnsureSub, Zero}, | ||
ArithmeticError, DispatchError, DispatchResult, FixedPointNumber, | ||
}; | ||
use sp_std::collections::btree_map::BTreeMap; | ||
use sp_std::{cmp::min, collections::btree_map::BTreeMap}; | ||
|
||
use crate::{ | ||
entities::interest::ActiveInterestRate, | ||
pallet::{Config, Error}, | ||
PriceOf, | ||
}; | ||
|
||
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)] | ||
|
@@ -76,6 +77,9 @@ pub struct ExternalPricing<T: Config> { | |
/// borrow/repay and the current oracle price. | ||
/// See [`ExternalAmount::settlement_price`]. | ||
pub max_price_variation: T::Rate, | ||
|
||
/// If the pricing is estimated with a linear pricing model. | ||
pub with_linear_pricing: bool, | ||
} | ||
|
||
impl<T: Config> ExternalPricing<T> { | ||
|
@@ -150,23 +154,58 @@ impl<T: Config> ExternalActivePricing<T> { | |
} | ||
} | ||
|
||
fn linear_accrual_price(&self, maturity: Seconds) -> Result<T::Balance, DispatchError> { | ||
Ok(cfg_utils::math::y_coord_in_rect( | ||
(self.settlement_price_updated, self.latest_settlement_price), | ||
(maturity, self.info.notional), | ||
T::Time::now(), | ||
)?) | ||
fn maybe_with_linear_accrual_price( | ||
&self, | ||
maturity: Seconds, | ||
price: T::Balance, | ||
price_last_updated: Seconds, | ||
) -> Result<T::Balance, DispatchError> { | ||
if self.info.with_linear_pricing { | ||
if min(price_last_updated, maturity) == maturity { | ||
// We can not have 2 'xs' with different 'y' in a rect. | ||
// That only happens at maturity | ||
return Ok(self.info.notional); | ||
} | ||
|
||
Ok(cfg_utils::math::y_coord_in_rect( | ||
(min(price_last_updated, maturity), price), | ||
(maturity, self.info.notional), | ||
min(T::Time::now(), maturity), | ||
)?) | ||
} else { | ||
Ok(price) | ||
} | ||
} | ||
|
||
pub fn current_price( | ||
&self, | ||
pool_id: T::PoolId, | ||
maturity: Seconds, | ||
) -> Result<T::Balance, DispatchError> { | ||
Ok(match T::PriceRegistry::get(&self.info.price_id, &pool_id) { | ||
Ok(data) => data.0, | ||
Err(_) => self.linear_accrual_price(maturity)?, | ||
}) | ||
self.current_price_inner( | ||
maturity, | ||
T::PriceRegistry::get(&self.info.price_id, &pool_id).ok(), | ||
) | ||
} | ||
|
||
fn current_price_inner( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current on-chain behavior is:
That behavior can not be modeled right now. We need to choose between:
Do we want this? Don't we still want to model the current behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we want to change that too. Get the price:
Use the price:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference between "Get the price" and "Use the price"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get is either orale or settlement. Use is just for using whatever came out of the get. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add new 2 test cases to check a loan configured with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, after the changes of this PR all loans comes with |
||
&self, | ||
maturity: Seconds, | ||
oracle: Option<PriceOf<T>>, | ||
) -> Result<T::Balance, DispatchError> { | ||
if let Some((oracle_price, oracle_provided_at)) = oracle { | ||
self.maybe_with_linear_accrual_price( | ||
maturity, | ||
oracle_price, | ||
oracle_provided_at.into_seconds(), | ||
) | ||
} else { | ||
self.maybe_with_linear_accrual_price( | ||
maturity, | ||
self.latest_settlement_price, | ||
self.settlement_price_updated, | ||
) | ||
} | ||
} | ||
|
||
pub fn outstanding_principal( | ||
|
@@ -197,13 +236,10 @@ impl<T: Config> ExternalActivePricing<T> { | |
|
||
pub fn present_value_cached( | ||
&self, | ||
cache: &BTreeMap<T::PriceId, T::Balance>, | ||
cache: &BTreeMap<T::PriceId, PriceOf<T>>, | ||
maturity: Seconds, | ||
) -> Result<T::Balance, DispatchError> { | ||
let price = match cache.get(&self.info.price_id) { | ||
Some(data) => *data, | ||
None => self.linear_accrual_price(maturity)?, | ||
}; | ||
let price = self.current_price_inner(maturity, cache.get(&self.info.price_id).copied())?; | ||
Ok(self.outstanding_quantity.ensure_mul_int(price)?) | ||
} | ||
|
||
|
@@ -288,3 +324,95 @@ impl<T: Config> ExternalActivePricing<T> { | |
Ok(()) | ||
} | ||
} | ||
|
||
/// Adds `with_linear_pricing` to ExternalPricing struct for migration to v4 | ||
pub mod v3 { | ||
use cfg_traits::Seconds; | ||
use parity_scale_codec::{Decode, Encode}; | ||
|
||
use crate::{ | ||
entities::{ | ||
interest::ActiveInterestRate, | ||
pricing::{external::MaxBorrowAmount, internal, internal::InternalActivePricing}, | ||
}, | ||
Config, | ||
}; | ||
|
||
#[derive(Encode, Decode)] | ||
pub enum Pricing<T: Config> { | ||
Internal(internal::InternalPricing<T>), | ||
External(ExternalPricing<T>), | ||
} | ||
|
||
impl<T: Config> Pricing<T> { | ||
pub fn migrate(self, with_linear_pricing: bool) -> crate::entities::pricing::Pricing<T> { | ||
match self { | ||
Pricing::Internal(i) => crate::entities::pricing::Pricing::Internal(i), | ||
Pricing::External(e) => { | ||
crate::entities::pricing::Pricing::External(e.migrate(with_linear_pricing)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Encode, Decode)] | ||
pub struct ExternalPricing<T: Config> { | ||
pub price_id: T::PriceId, | ||
pub max_borrow_amount: MaxBorrowAmount<T::Quantity>, | ||
pub notional: T::Balance, | ||
pub max_price_variation: T::Rate, | ||
} | ||
|
||
#[derive(Encode, Decode)] | ||
pub enum ActivePricing<T: Config> { | ||
Internal(InternalActivePricing<T>), | ||
External(ExternalActivePricing<T>), | ||
} | ||
|
||
impl<T: Config> ActivePricing<T> { | ||
pub fn migrate( | ||
self, | ||
with_linear_pricing: bool, | ||
) -> crate::entities::pricing::ActivePricing<T> { | ||
match self { | ||
ActivePricing::Internal(i) => crate::entities::pricing::ActivePricing::Internal(i), | ||
ActivePricing::External(e) => crate::entities::pricing::ActivePricing::External( | ||
e.migrate(with_linear_pricing), | ||
), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Encode, Decode)] | ||
pub struct ExternalActivePricing<T: Config> { | ||
info: ExternalPricing<T>, | ||
outstanding_quantity: T::Quantity, | ||
pub interest: ActiveInterestRate<T>, | ||
latest_settlement_price: T::Balance, | ||
settlement_price_updated: Seconds, | ||
} | ||
|
||
impl<T: Config> ExternalActivePricing<T> { | ||
pub fn migrate(self, with_linear_pricing: bool) -> super::ExternalActivePricing<T> { | ||
super::ExternalActivePricing { | ||
info: self.info.migrate(with_linear_pricing), | ||
outstanding_quantity: self.outstanding_quantity, | ||
interest: self.interest, | ||
latest_settlement_price: self.latest_settlement_price, | ||
settlement_price_updated: self.settlement_price_updated, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Config> ExternalPricing<T> { | ||
pub fn migrate(self, with_linear_pricing: bool) -> super::ExternalPricing<T> { | ||
super::ExternalPricing { | ||
price_id: self.price_id, | ||
max_borrow_amount: self.max_borrow_amount, | ||
notional: self.notional, | ||
max_price_variation: self.max_price_variation, | ||
with_linear_pricing, | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this, I think now is much more understandable what's happening 🎉