diff --git a/Cargo.lock b/Cargo.lock index bb8078ea52..d246a95b45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11165,6 +11165,7 @@ dependencies = [ "pallet-permissions", "pallet-pool-registry", "pallet-pool-system", + "pallet-uniques", "runtime-common", "sp-core", "sp-io", diff --git a/runtime/tests/Cargo.toml b/runtime/tests/Cargo.toml index c42c40b9c3..2f5c9f92ef 100644 --- a/runtime/tests/Cargo.toml +++ b/runtime/tests/Cargo.toml @@ -34,9 +34,10 @@ pallet-loans = { path = "../../pallets/loans" } pallet-permissions = { path = "../../pallets/permissions" } pallet-pool-registry = { path = "../../pallets/pool-registry" } pallet-pool-system = { path = "../../pallets/pool-system" } +pallet-uniques = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" } [features] -# Boost your compilation of the tests to increment the feedback loop using --no-default-features flag -# Tests will be compiled but not executed. +# Boost your tests compilation incrementing the feedback loop using --no-default-features flag +# Tests will be compiled much faster without runtimes, but not executed. default = ["compile-runtimes"] compile-runtimes = ["development-runtime", "altair-runtime", "centrifuge-runtime"] diff --git a/runtime/tests/src/lib.rs b/runtime/tests/src/lib.rs index 5cbaf4885c..826cc98ede 100644 --- a/runtime/tests/src/lib.rs +++ b/runtime/tests/src/lib.rs @@ -1,6 +1,6 @@ #![cfg(test)] -use cfg_primitives::{Balance, PoolId, TrancheId}; +use cfg_primitives::{Balance, CollectionId, ItemId, PoolId, TrancheId}; use cfg_types::{ permissions::{PermissionScope, Role}, tokens::{CurrencyId, CustomMetadata, TrancheCurrency}, @@ -29,13 +29,17 @@ pub trait Config: ModifyPool = pallet_pool_system::Pallet, ModifyWriteOffPolicy = pallet_loans::Pallet, > + pallet_permissions::Config> - + pallet_loans::Config - + orml_tokens::Config + + pallet_loans::Config< + Balance = Balance, + PoolId = PoolId, + CollectionId = CollectionId, + ItemId = ItemId, + > + orml_tokens::Config + orml_asset_registry::Config< AssetId = CurrencyId, CustomMetadata = CustomMetadata, Balance = Balance, - > + > + pallet_uniques::Config { const KIND: RuntimeKind; } diff --git a/runtime/tests/src/use_case/loans.rs b/runtime/tests/src/use_case/loans.rs index f2e5934e1a..2f44408dcb 100644 --- a/runtime/tests/src/use_case/loans.rs +++ b/runtime/tests/src/use_case/loans.rs @@ -1,4 +1,4 @@ -use cfg_primitives::PoolId; +use cfg_primitives::{CollectionId, ItemId, PoolId}; use sp_runtime::{traits::Get, AccountId32}; use crate::{ @@ -6,16 +6,17 @@ use crate::{ Config, }; -const POOL_A: PoolId = 23; +const POOL_ADMIN: AccountId32 = util::account(1); +const INVESTOR: AccountId32 = util::account(2); +const BORROWER: AccountId32 = util::account(3); -const ADMIN: AccountId32 = AccountId32::new([1; 32]); -const INVESTOR: AccountId32 = AccountId32::new([2; 32]); -const BORROWER: AccountId32 = AccountId32::new([3; 32]); +const POOL_A: PoolId = 23; +const ASSET_A: (CollectionId, ItemId) = (1, ItemId(10)); fn borrow_from_pool() { // Creating a pool - util::give_balance_to::(ADMIN, T::PoolDeposit::get()); - util::create_pool::(ADMIN, POOL_A); + util::give_balance_to::(POOL_ADMIN, T::PoolDeposit::get()); + util::create_pool::(POOL_ADMIN, POOL_A); // Funding a pool let funds = 100_000 * MUSD_UNIT; @@ -26,6 +27,7 @@ fn borrow_from_pool() { // Borrowing from a pool util::give_borrower_role::(BORROWER, POOL_A); + util::give_asset_to::(BORROWER, ASSET_A); } crate::test_with_all_runtimes!(genesis, borrow_from_pool); diff --git a/runtime/tests/src/util.rs b/runtime/tests/src/util.rs index b786fe9fb7..1af0de42f0 100644 --- a/runtime/tests/src/util.rs +++ b/runtime/tests/src/util.rs @@ -1,4 +1,4 @@ -use cfg_primitives::{Balance, Moment, PoolId, TrancheId}; +use cfg_primitives::{Balance, CollectionId, ItemId, Moment, PoolId, TrancheId}; use cfg_traits::investments::TrancheCurrency as TrancheCurrencyT; use cfg_types::{ permissions::{PermissionScope, PoolRole, Role}, @@ -22,10 +22,12 @@ use crate::{Config, RuntimeKind}; pub const MUSD_DECIMALS: u32 = 6; pub const MUSD_UNIT: Balance = 10u128.pow(MUSD_DECIMALS); pub const MUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(23); - pub const POOL_FUNDS: Balance = 100_000_000 * MUSD_UNIT; +pub const MAX_FUNDED_ACCOUNTS: u8 = 20; -const MAX_ACCOUNTS: u8 = 20; +pub const fn account(value: u8) -> AccountId32 { + AccountId32::new([value; 32]) +} /// This genesis basically do: /// - ED for any account available @@ -36,24 +38,18 @@ pub fn genesis() -> sp_io::TestExternalities { .unwrap(); pallet_balances::GenesisConfig:: { - balances: (0..MAX_ACCOUNTS) + balances: (0..MAX_FUNDED_ACCOUNTS) .into_iter() - .map(|i| (AccountId32::new([i; 32]), T::ExistentialDeposit::get())) + .map(|i| (account(i), T::ExistentialDeposit::get())) .collect(), } .assimilate_storage(&mut storage) .unwrap(); orml_tokens::GenesisConfig:: { - balances: (0..MAX_ACCOUNTS) + balances: (0..MAX_FUNDED_ACCOUNTS) .into_iter() - .map(|i| { - ( - AccountId32::new([i; 32]), - MUSD_CURRENCY_ID, - T::ExistentialDeposit::get(), - ) - }) + .map(|i| (account(i), MUSD_CURRENCY_ID, T::ExistentialDeposit::get())) .collect(), } .assimilate_storage(&mut storage) @@ -81,6 +77,27 @@ pub fn genesis() -> sp_io::TestExternalities { ext } +pub fn give_asset_to( + dest: AccountId32, + (collection_id, item_id): (CollectionId, ItemId), +) { + pallet_uniques::Pallet::::force_create( + RawOrigin::Root.into(), + collection_id, + T::Lookup::unlookup(dest.clone()), + true, + ) + .unwrap(); + + pallet_uniques::Pallet::::mint( + RawOrigin::Signed(dest.clone()).into(), + collection_id, + item_id, + T::Lookup::unlookup(dest), + ) + .unwrap() +} + pub fn give_balance_to(dest: AccountId32, amount: Balance) { let data = pallet_balances::Account::::get(dest.clone()); pallet_balances::Pallet::::set_balance( @@ -104,6 +121,34 @@ pub fn give_musd_to(dest: AccountId32, amount: Balance) { .unwrap(); } +pub fn give_investor_role( + investor: AccountId32, + pool_id: PoolId, + tranche_id: TrancheId, +) { + let role = Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, Moment::MAX)); + pallet_permissions::Pallet::::add( + RawOrigin::Root.into(), + role, + investor, + PermissionScope::Pool(pool_id), + role, + ) + .unwrap(); +} + +pub fn give_borrower_role(borrower: AccountId32, pool_id: PoolId) { + let role = Role::PoolRole(PoolRole::Borrower); + pallet_permissions::Pallet::::add( + RawOrigin::Root.into(), + role, + borrower, + PermissionScope::Pool(pool_id), + role, + ) + .unwrap(); +} + pub fn create_pool(admin: AccountId32, pool_id: PoolId) { pallet_pool_registry::Pallet::::register( match T::KIND { @@ -155,34 +200,6 @@ pub fn invest( .unwrap(); } -pub fn give_investor_role( - investor: AccountId32, - pool_id: PoolId, - tranche_id: TrancheId, -) { - let role = Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, Moment::MAX)); - pallet_permissions::Pallet::::add( - RawOrigin::Root.into(), - role, - investor, - PermissionScope::Pool(pool_id), - role, - ) - .unwrap(); -} - -pub fn give_borrower_role(borrower: AccountId32, pool_id: PoolId) { - let role = Role::PoolRole(PoolRole::Borrower); - pallet_permissions::Pallet::::add( - RawOrigin::Root.into(), - role, - borrower, - PermissionScope::Pool(pool_id), - role, - ) - .unwrap(); -} - // Utilities that does not modify the state pub mod get { use super::*;