Skip to content

Commit

Permalink
fix: essence tranche token metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Apr 5, 2024
1 parent d3a0f0c commit e256eb2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pallets/pool-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ pub mod pallet {
/// Pre-requirements for a TrancheUpdate are not met
/// for example: Tranche changed but not its metadata or vice versa
InvalidTrancheUpdate,
/// No metada for the given currency found
/// No metadata for the given currency found
MetadataForCurrencyNotFound,
/// The given tranche token name exceeds the length limit
TrancheTokenNameTooLong,
Expand Down
21 changes: 10 additions & 11 deletions pallets/pool-system/src/pool_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use frame_support::{
traits::Get,
BoundedVec, RuntimeDebug,
};
use orml_traits::{asset_registry::AssetMetadata, Change};
use orml_traits::Change;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_arithmetic::traits::{BaseArithmetic, Unsigned};
Expand Down Expand Up @@ -119,6 +119,7 @@ pub struct PoolDetails<
Rate: FixedPointNumber<Inner = Balance>,
Balance: FixedPointOperand + sp_arithmetic::MultiplyRational,
MaxTranches: Get<u32>,
TrancheCurrency: Into<CurrencyId>,
{
/// Currency that the pool is denominated in (immutable).
pub currency: CurrencyId,
Expand Down Expand Up @@ -208,6 +209,7 @@ pub struct PoolEssence<
MaxTokenSymbolLength,
> where
CurrencyId: Copy,
TrancheCurrency: Into<CurrencyId>,
MaxTokenNameLength: Get<u32>,
MaxTokenSymbolLength: Get<u32>,
{
Expand Down Expand Up @@ -249,6 +251,7 @@ impl<
Balance:
FixedPointOperand + BaseArithmetic + Unsigned + From<u64> + sp_arithmetic::MultiplyRational,
CurrencyId: Copy,
TrancheCurrency: Into<CurrencyId>,
EpochId: BaseArithmetic + Copy,
PoolId: Copy + Encode,
Rate: FixedPointNumber<Inner = Balance>,
Expand Down Expand Up @@ -300,16 +303,12 @@ impl<
> = Vec::new();

for tranche in self.tranches.residual_top_slice().iter() {
let metadata = AssetRegistry::metadata(&self.currency.into())
.ok_or(AssetMetadata {
decimals: 0,
name: Vec::new(),
symbol: Vec::new(),
existential_deposit: (),
location: None,
additional: (),
})
.unwrap();
let metadata = AssetRegistry::metadata(
&<AssetRegistry as orml_traits::asset_registry::Inspect>::AssetId::from(

Check warning on line 307 in pallets/pool-system/src/pool_types.rs

View check run for this annotation

Codecov / codecov/patch

pallets/pool-system/src/pool_types.rs#L307

Added line #L307 was not covered by tests
tranche.currency.into(),
),
)
.ok_or(DispatchError::CannotLookup)?;

tranches.push(TrancheEssence {
currency: tranche.currency,
Expand Down
63 changes: 63 additions & 0 deletions pallets/pool-system/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,69 @@ fn create_tranche_token_metadata() {
});
}

#[test]
fn essence() {
new_test_ext().execute_with(|| {
let pool_owner = 1_u64;
let tranche_input = vec![
TrancheInput {
tranche_type: TrancheType::Residual,
seniority: None,
metadata: TrancheMetadata {
token_name: BoundedVec::try_from("ResName".as_bytes().to_owned())
.expect("String not out of bounds"),
token_symbol: BoundedVec::try_from("ResSym".as_bytes().to_owned())
.expect("String not out of bounds"),
},
},
TrancheInput {
tranche_type: TrancheType::NonResidual {
interest_rate_per_sec: Rate::one(),
min_risk_buffer: Perquintill::from_percent(10),
},
seniority: None,
metadata: TrancheMetadata {
token_name: BoundedVec::try_from("NonResName".as_bytes().to_owned())
.expect("String not out of bounds"),
token_symbol: BoundedVec::try_from("NRSym".as_bytes().to_owned())
.expect("String not out of bounds"),
},
},
];

assert_ok!(PoolSystem::create(
pool_owner.clone(),
pool_owner.clone(),
DEFAULT_POOL_ID,
tranche_input.clone(),
AUSD_CURRENCY_ID,
10_000 * CURRENCY,
vec![],
));

let pool_details = Pool::<Runtime>::get(DEFAULT_POOL_ID).expect("Pool is registered; qed");
let essence = pool_details
.essence::<<Runtime as Config>::AssetRegistry, Balance, MaxTokenNameLength, MaxTokenSymbolLength>(
)
.expect("Tranche token metadata is registered; qed");

assert_eq!(essence.currency, AUSD_CURRENCY_ID);
assert_eq!(essence.max_reserve, 10_000 * CURRENCY);
assert_eq!(essence.max_nav_age, pool_details.parameters.max_nav_age);
assert_eq!(
essence.min_epoch_time,
pool_details.parameters.min_epoch_time
);

tranche_input.iter().zip(essence.tranches.iter()).for_each(
|(tranche_input, tranche_essence)| {
assert_eq!(tranche_input.metadata, tranche_essence.metadata);
assert_eq!(tranche_input.tranche_type, tranche_essence.ty);
},
);
})
}

mod changes {
use cfg_traits::changes::ChangeGuard;
use sp_std::collections::btree_set::BTreeSet;
Expand Down

0 comments on commit e256eb2

Please sign in to comment.