-
Notifications
You must be signed in to change notification settings - Fork 72
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
Loans: Add Runtime API #1457
Loans: Add Runtime API #1457
Changes from 11 commits
ea4baff
07f970f
6c025c3
b840047
abba2d7
0172343
23d90e9
a9d7506
c8960e3
c2fb6e4
c705b89
a68041a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,8 @@ impl<T: Config> ClosedLoan<T> { | |
/// Data containing an active loan. | ||
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] | ||
#[scale_info(skip_type_params(T))] | ||
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] | ||
#[cfg_attr(feature = "std", serde(bound = ""))] | ||
pub struct ActiveLoan<T: Config> { | ||
/// Specify the repayments schedule of the loan | ||
schedule: RepaymentSchedule, | ||
|
@@ -489,3 +491,47 @@ impl<T: Config> ActiveLoan<T> { | |
self.schedule.maturity = crate::types::Maturity::fixed(duration); | ||
} | ||
} | ||
|
||
/// Data containing an active loan with extra computed. | ||
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] | ||
#[scale_info(skip_type_params(T))] | ||
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] | ||
#[cfg_attr(feature = "std", serde(bound = ""))] | ||
pub struct ActiveLoanInfo<T: Config> { | ||
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. This is a bit tricky and probably useful for futures runtime APIs. The line 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. Thanks for clarifying this. So this is the 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. Yeah! More or less. Also, if there were other bounds, and you do not want to "reset them" you should add |
||
/// Related active loan | ||
active_loan: ActiveLoan<T>, | ||
|
||
/// Interest accrued for this loan | ||
interest_accrued: T::Balance, | ||
|
||
/// Present value of the loan | ||
present_value: T::Balance, | ||
} | ||
|
||
impl<T: Config> TryFrom<ActiveLoan<T>> for ActiveLoanInfo<T> { | ||
type Error = DispatchError; | ||
|
||
fn try_from(active_loan: ActiveLoan<T>) -> Result<Self, Self::Error> { | ||
let (interest_accrued, present_value) = match &active_loan.pricing { | ||
ActivePricing::Internal(inner) => { | ||
let principal = active_loan | ||
.total_borrowed | ||
.ensure_sub(active_loan.total_repaid.principal)?; | ||
|
||
let maturity_date = active_loan.schedule.maturity.date(); | ||
|
||
( | ||
inner.current_interest(principal)?, | ||
inner.present_value(active_loan.origination_date, maturity_date)?, | ||
) | ||
} | ||
ActivePricing::External(inner) => (inner.current_interest()?, inner.present_value()?), | ||
}; | ||
|
||
Ok(Self { | ||
active_loan, | ||
interest_accrued, | ||
present_value, | ||
}) | ||
} | ||
} |
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.
Since it's optional, shouldn't we apply
?
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.
Wondering why adding this broke tests?
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.
I think:
serde?/std
in caseserde
is enable it addsserde/std
serde/srd
enablesserde
and it addsserde/std
(what we want here)