-
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 8 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ use sp_std::collections::btree_map::BTreeMap; | |
use crate::{ | ||
entities::interest::ActiveInterestRate, | ||
pallet::{Config, Error}, | ||
PriceOf, | ||
}; | ||
|
||
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)] | ||
|
@@ -150,9 +151,14 @@ impl<T: Config> ExternalActivePricing<T> { | |
} | ||
} | ||
|
||
fn linear_accrual_price(&self, maturity: Seconds) -> Result<T::Balance, DispatchError> { | ||
fn linear_accrual_price( | ||
&self, | ||
maturity: Seconds, | ||
price: T::Balance, | ||
price_last_updated: Seconds, | ||
) -> Result<T::Balance, DispatchError> { | ||
Ok(cfg_utils::math::y_coord_in_rect( | ||
(self.settlement_price_updated, self.latest_settlement_price), | ||
(price_last_updated, price), | ||
lemunozm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(maturity, self.info.notional), | ||
T::Time::now(), | ||
)?) | ||
|
@@ -163,10 +169,26 @@ impl<T: Config> ExternalActivePricing<T> { | |
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.linear_accrual_price(maturity, oracle_price, oracle_provided_at.into_seconds()) | ||
mustermeiszer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
self.linear_accrual_price( | ||
maturity, | ||
self.latest_settlement_price, | ||
self.settlement_price_updated, | ||
) | ||
} | ||
} | ||
|
||
pub fn outstanding_principal( | ||
|
@@ -197,13 +219,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)?) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,14 +198,16 @@ fn with_unregister_price_id_and_oracle_not_required() { | |
expected_portfolio(QUANTITY.saturating_mul_int(price_value_after_half_year)); | ||
|
||
// Suddenty, the oracle set a value | ||
const MARKET_PRICE_VALUE: Balance = 999; | ||
MockPrices::mock_collection(|_| { | ||
Ok(MockDataCollection::new(|_| { | ||
Ok((PRICE_VALUE * 8, BLOCK_TIME_MS)) | ||
Ok((MARKET_PRICE_VALUE, BLOCK_TIME_MS)) | ||
})) | ||
}); | ||
let price_value_after_half_year = MARKET_PRICE_VALUE + (NOTIONAL - MARKET_PRICE_VALUE) / 2; | ||
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 like more how you have tested this! |
||
|
||
update_portfolio(); | ||
expected_portfolio(QUANTITY.saturating_mul_int(PRICE_VALUE * 8)); | ||
expected_portfolio(QUANTITY.saturating_mul_int(price_value_after_half_year)); | ||
}); | ||
} | ||
|
||
|
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.
To fix the clippy issue