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

fix: Enable Runtime-Apis to update nav with estimates #1791

Merged
merged 10 commits into from
Apr 3, 2024
12 changes: 10 additions & 2 deletions pallets/pool-system/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,20 @@ impl<T: Config> ChangeGuard for Pallet<T> {
}
}

allowed
let change = allowed
.then(|| {
NotedChange::<T>::remove(pool_id, change_id);
change
})
.ok_or(Error::<T>::ChangeNotReady.into())
.ok_or(Error::<T>::ChangeNotReady.into())?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already notified in the consumer side of released(): https://github.com/centrifuge/centrifuge-chain/blob/main/pallets/oracle-collection/src/lib.rs#L244

Not sure if we want another event here.

Copy link
Contributor

@wischli wischli Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS, we are not emitting such an event for pool fees or for loans. IMO, it makes sense to add this here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the Events are quite confusing without showing that this came from a change that was released:

And if we are noting a change we are also emitting an event. I would keep it.


Self::deposit_event(Event::ReleasedChange {
pool_id,
change_id,
change: change.clone(),
});

Ok(change)
}
}

Expand Down
6 changes: 6 additions & 0 deletions pallets/pool-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ pub mod pallet {
change_id: T::Hash,
change: T::RuntimeChange,
},
/// A change was released
ReleasedChange {
pool_id: T::PoolId,
change_id: T::Hash,
change: T::RuntimeCange,
},
/// The PoolFeesNAV exceeds the sum of the AUM and the total reserve of
/// the pool
NegativeBalanceSheet {
Expand Down
6 changes: 3 additions & 3 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,7 @@ impl_runtime_apis! {
fn tranche_token_price(pool_id: PoolId, tranche: TrancheLoc<TrancheId>) -> Option<Quantity>{
let now = <Timestamp as UnixTime>::now().as_secs();
let mut pool = PoolSystem::pool(pool_id)?;
let nav = Loans::update_nav(pool_id).ok()?;
let nav = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let total_assets = pool.reserve.total.saturating_add(nav);
let index: usize = pool.tranches.tranche_index(&tranche)?.try_into().ok()?;
let prices = pool
Expand All @@ -2308,7 +2308,7 @@ impl_runtime_apis! {
fn tranche_token_prices(pool_id: PoolId) -> Option<Vec<Quantity>>{
let now = <Timestamp as UnixTime>::now().as_secs();
let mut pool = PoolSystem::pool(pool_id)?;
let nav = Loans::update_nav(pool_id).ok()?;
let nav = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let total_assets = pool.reserve.total.saturating_add(nav);
pool
.tranches
Expand All @@ -2334,7 +2334,7 @@ impl_runtime_apis! {

fn nav(pool_id: PoolId) -> Option<PoolNav<Balance>> {
let pool = pallet_pool_system::Pool::<Runtime>::get(pool_id)?;
let nav_loans = Loans::update_nav(pool_id).ok()?;
let nav_loans = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let nav_fees = PoolFees::update_nav(pool_id).ok()?;
let nav = pallet_pool_system::Nav::new(nav_loans, nav_fees);
let total = nav.total(pool.reserve.total).unwrap_or(Balance::default());
Expand Down
6 changes: 3 additions & 3 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ impl_runtime_apis! {
fn tranche_token_price(pool_id: PoolId, tranche: TrancheLoc<TrancheId>) -> Option<Quantity>{
let now = <Timestamp as UnixTime>::now().as_secs();
let mut pool = PoolSystem::pool(pool_id)?;
let nav = Loans::update_nav(pool_id).ok()?;
let nav = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let total_assets = pool.reserve.total.saturating_add(nav);
let index: usize = pool.tranches.tranche_index(&tranche)?.try_into().ok()?;
let prices = pool
Expand All @@ -2358,7 +2358,7 @@ impl_runtime_apis! {
fn tranche_token_prices(pool_id: PoolId) -> Option<Vec<Quantity>>{
let now = <Timestamp as UnixTime>::now().as_secs();
let mut pool = PoolSystem::pool(pool_id)?;
let nav = Loans::update_nav(pool_id).ok()?;
let nav = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let total_assets = pool.reserve.total.saturating_add(nav);
pool
.tranches
Expand All @@ -2384,7 +2384,7 @@ impl_runtime_apis! {

fn nav(pool_id: PoolId) -> Option<PoolNav<Balance>> {
let pool = pallet_pool_system::Pool::<Runtime>::get(pool_id)?;
let nav_loans = Loans::update_nav(pool_id).ok()?;
let nav_loans = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let nav_fees = PoolFees::update_nav(pool_id).ok()?;
let nav = pallet_pool_system::Nav::new(nav_loans, nav_fees);
let total = nav.total(pool.reserve.total).unwrap_or(Balance::default());
Expand Down
16 changes: 16 additions & 0 deletions runtime/common/src/apis/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_traits::data::DataRegistry;
use pallet_loans::entities::input::PriceCollectionInput;
use parity_scale_codec::Codec;
use sp_api::decl_runtime_apis;
use sp_runtime::DispatchError;
Expand All @@ -32,3 +34,17 @@ decl_runtime_apis! {
fn portfolio_valuation(pool_id: PoolId, input_prices: PriceCollectionInput) -> Result<Balance, DispatchError>;
}
}

pub fn update_nav_api_call<T: pallet_loans::Config>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT. We're using this for the API, but it seems like a common method that can be used for other purposes. I would move to runtime/common/src/lib.rs instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinion. Maybe infallible_update_nav() or similar?

pool: T::PoolId,
) -> Result<T::Balance, DispatchError> {
let price_input =
if let Ok(prices) = <T as pallet_loans::Config>::PriceRegistry::collection(pool) {
PriceCollectionInput::Custom(prices)
} else {
PriceCollectionInput::Empty
};

pallet_loans::Pallet::<T>::update_portfolio_valuation_for_pool(pool, price_input)
.map(|(nav, _)| nav)
}
6 changes: 3 additions & 3 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,7 @@ impl_runtime_apis! {
fn tranche_token_price(pool_id: PoolId, tranche: TrancheLoc<TrancheId>) -> Option<Quantity>{
let now = <Timestamp as UnixTime>::now().as_secs();
let mut pool = PoolSystem::pool(pool_id)?;
let nav = Loans::update_nav(pool_id).ok()?;
let nav = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let total_assets = pool.reserve.total.saturating_add(nav);
let index: usize = pool.tranches.tranche_index(&tranche)?.try_into().ok()?;
let prices = pool
Expand All @@ -2397,7 +2397,7 @@ impl_runtime_apis! {
fn tranche_token_prices(pool_id: PoolId) -> Option<Vec<Quantity>>{
let now = <Timestamp as UnixTime>::now().as_secs();
let mut pool = PoolSystem::pool(pool_id)?;
let nav = Loans::update_nav(pool_id).ok()?;
let nav = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let total_assets = pool.reserve.total.saturating_add(nav);
pool
.tranches
Expand All @@ -2423,7 +2423,7 @@ impl_runtime_apis! {

fn nav(pool_id: PoolId) -> Option<PoolNav<Balance>> {
let pool = pallet_pool_system::Pool::<Runtime>::get(pool_id)?;
let nav_loans = Loans::update_nav(pool_id).ok()?;
let nav_loans = runtime_common::apis::loans::update_nav_api_call(pool_id).ok()?;
let nav_fees = PoolFees::update_nav(pool_id).ok()?;
let nav = pallet_pool_system::Nav::new(nav_loans, nav_fees);
let total = nav.total(pool.reserve.total).unwrap_or(Balance::default());
Expand Down
Loading