Skip to content

Commit

Permalink
feat: add NAV runtime API (#1703)
Browse files Browse the repository at this point in the history
* feat: add NAV runtime API

* refactor: explicit nav type
  • Loading branch information
wischli authored Jan 30, 2024
1 parent f7e1232 commit 23daeeb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions libs/types/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ pub enum PoolRegistrationStatus {
Unregistered,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct PoolNav<Balance> {
pub nav_aum: Balance,
pub nav_fees: Balance,
pub reserve: Balance,
pub total: Balance,
}

/// The dynamic representation of a pool fee, its editor and destination
/// address.
///
Expand Down
12 changes: 11 additions & 1 deletion runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ mod __runtime_api_use {

#[cfg(not(feature = "disable-runtime-api"))]
use __runtime_api_use::*;
use cfg_types::{locations::Location, tokens::FilterCurrency};
use cfg_types::{locations::Location, pools::PoolNav, tokens::FilterCurrency};
use runtime_common::transfer_filter::PreNativeTransfer;

#[cfg(not(feature = "disable-runtime-api"))]
Expand Down Expand Up @@ -2174,6 +2174,16 @@ impl_runtime_apis! {
let pool = pallet_pool_system::Pool::<Runtime>::get(pool_id)?;
pool.tranches.tranche_currency(tranche_loc).map(Into::into)
}

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_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).ok()?;

Some(PoolNav { nav_aum: nav.nav_aum, nav_fees: nav.nav_fees, reserve: pool.reserve.total, total })
}
}

// RewardsApi
Expand Down
12 changes: 11 additions & 1 deletion runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ mod __runtime_api_use {

#[cfg(not(feature = "disable-runtime-api"))]
use __runtime_api_use::*;
use cfg_types::{locations::Location, tokens::FilterCurrency};
use cfg_types::{locations::Location, pools::PoolNav, tokens::FilterCurrency};
use runtime_common::transfer_filter::PreNativeTransfer;

#[cfg(not(feature = "disable-runtime-api"))]
Expand Down Expand Up @@ -2218,6 +2218,16 @@ impl_runtime_apis! {
let pool = pallet_pool_system::Pool::<Runtime>::get(pool_id)?;
pool.tranches.tranche_currency(tranche_loc).map(Into::into)
}

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_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).ok()?;

Some(PoolNav { nav_aum: nav.nav_aum, nav_fees: nav.nav_fees, reserve: pool.reserve.total, total })
}
}

// RewardsApi
Expand Down
3 changes: 3 additions & 0 deletions runtime/common/src/apis/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_types::pools::PoolNav;
use pallet_pool_system::{
tranches::{TrancheIndex, TrancheLoc, TrancheSolution},
EpochSolution,
Expand Down Expand Up @@ -46,5 +47,7 @@ decl_runtime_apis! {
fn tranche_id(pool_id: PoolId, tranche_index: TrancheIndex) -> Option<TrancheId>;

fn tranche_currency(pool_id: PoolId, tranche_loc: TrancheLoc<TrancheId>) -> Option<Currency>;

fn nav(pool_id: PoolId) -> Option<PoolNav<Balance>>;
}
}
2 changes: 1 addition & 1 deletion runtime/development/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "development-runtime"
version = "0.10.38"
version = "0.10.39"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
14 changes: 12 additions & 2 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("centrifuge-devel"),
impl_name: create_runtime_str!("centrifuge-devel"),
authoring_version: 1,
spec_version: 1038,
spec_version: 1039,
impl_version: 1,
#[cfg(not(feature = "disable-runtime-api"))]
apis: RUNTIME_API_VERSIONS,
Expand Down Expand Up @@ -2118,7 +2118,7 @@ mod __runtime_api_use {

#[cfg(not(feature = "disable-runtime-api"))]
use __runtime_api_use::*;
use cfg_types::tokens::FilterCurrency;
use cfg_types::{pools::PoolNav, tokens::FilterCurrency};
use runtime_common::{remarks::Remark, transfer_filter::PreNativeTransfer};

#[cfg(not(feature = "disable-runtime-api"))]
Expand Down Expand Up @@ -2296,6 +2296,16 @@ impl_runtime_apis! {
let pool = pallet_pool_system::Pool::<Runtime>::get(pool_id)?;
pool.tranches.tranche_currency(tranche_loc).map(Into::into)
}

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_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).ok()?;

Some(PoolNav { nav_aum: nav.nav_aum, nav_fees: nav.nav_fees, reserve: pool.reserve.total, total })
}
}

// RewardsApi
Expand Down

0 comments on commit 23daeeb

Please sign in to comment.