Skip to content

Commit

Permalink
fix: essence tranche token metadata (#1798)
Browse files Browse the repository at this point in the history
* fix: essence tranche token metadata

* fix: merge conflict
  • Loading branch information
wischli authored Apr 15, 2024
1 parent c63c370 commit 58045f4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 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 @@ -521,7 +521,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
12 changes: 9 additions & 3 deletions pallets/pool-system/src/pool_types.rs
Original file line number Diff line number Diff line change
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 @@ -197,6 +198,7 @@ pub struct PoolEssence<CurrencyId, Balance, TrancheCurrency, Rate, StringLimit>
where
CurrencyId: Copy,
StringLimit: Get<u32>,
TrancheCurrency: Into<CurrencyId>,
{
/// Currency that the pool is denominated in (immutable).
pub currency: CurrencyId,
Expand Down Expand Up @@ -235,6 +237,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 @@ -273,9 +276,12 @@ impl<
let mut tranches: Vec<TrancheEssence<TrancheCurrency, Rate, StringLimit>> = Vec::new();

for tranche in self.tranches.residual_top_slice().iter() {
let metadata = AssetRegistry::metadata(&self.currency).ok_or(DispatchError::Other(
"Always exists a currency for an existing pool",
))?;
let metadata = AssetRegistry::metadata(
&<AssetRegistry as orml_traits::asset_registry::Inspect>::AssetId::from(
tranche.currency.into(),
),
)
.ok_or(DispatchError::CannotLookup)?;

tranches.push(TrancheEssence {
currency: tranche.currency,
Expand Down
62 changes: 62 additions & 0 deletions pallets/pool-system/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,68 @@ 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, StringLimit>()
.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 58045f4

Please sign in to comment.