Skip to content

Commit

Permalink
add asset support
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Sep 29, 2023
1 parent dd29541 commit 7ffa045
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 54 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions runtime/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
12 changes: 8 additions & 4 deletions runtime/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -29,13 +29,17 @@ pub trait Config:
ModifyPool = pallet_pool_system::Pallet<Self>,
ModifyWriteOffPolicy = pallet_loans::Pallet<Self>,
> + pallet_permissions::Config<Role = Role, Scope = PermissionScope<PoolId, CurrencyId>>
+ pallet_loans::Config<Balance = Balance, PoolId = PoolId>
+ orml_tokens::Config<CurrencyId = CurrencyId, Balance = Balance>
+ pallet_loans::Config<
Balance = Balance,
PoolId = PoolId,
CollectionId = CollectionId,
ItemId = ItemId,
> + orml_tokens::Config<CurrencyId = CurrencyId, Balance = Balance>
+ orml_asset_registry::Config<
AssetId = CurrencyId,
CustomMetadata = CustomMetadata,
Balance = Balance,
>
> + pallet_uniques::Config<CollectionId = CollectionId, ItemId = ItemId>
{
const KIND: RuntimeKind;
}
Expand Down
16 changes: 9 additions & 7 deletions runtime/tests/src/use_case/loans.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use cfg_primitives::PoolId;
use cfg_primitives::{CollectionId, ItemId, PoolId};
use sp_runtime::{traits::Get, AccountId32};

use crate::{
util::{self, genesis, MUSD_UNIT},
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<T: Config>() {
// Creating a pool
util::give_balance_to::<T>(ADMIN, T::PoolDeposit::get());
util::create_pool::<T>(ADMIN, POOL_A);
util::give_balance_to::<T>(POOL_ADMIN, T::PoolDeposit::get());
util::create_pool::<T>(POOL_ADMIN, POOL_A);

// Funding a pool
let funds = 100_000 * MUSD_UNIT;
Expand All @@ -26,6 +27,7 @@ fn borrow_from_pool<T: Config>() {

// Borrowing from a pool
util::give_borrower_role::<T>(BORROWER, POOL_A);
util::give_asset_to::<T>(BORROWER, ASSET_A);
}

crate::test_with_all_runtimes!(genesis, borrow_from_pool);
99 changes: 58 additions & 41 deletions runtime/tests/src/util.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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
Expand All @@ -36,24 +38,18 @@ pub fn genesis<T: Config>() -> sp_io::TestExternalities {
.unwrap();

pallet_balances::GenesisConfig::<T> {
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::<T> {
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)
Expand Down Expand Up @@ -81,6 +77,27 @@ pub fn genesis<T: Config>() -> sp_io::TestExternalities {
ext
}

pub fn give_asset_to<T: Config>(
dest: AccountId32,
(collection_id, item_id): (CollectionId, ItemId),
) {
pallet_uniques::Pallet::<T>::force_create(
RawOrigin::Root.into(),
collection_id,
T::Lookup::unlookup(dest.clone()),
true,
)
.unwrap();

pallet_uniques::Pallet::<T>::mint(
RawOrigin::Signed(dest.clone()).into(),
collection_id,
item_id,
T::Lookup::unlookup(dest),
)
.unwrap()
}

pub fn give_balance_to<T: Config>(dest: AccountId32, amount: Balance) {
let data = pallet_balances::Account::<T>::get(dest.clone());
pallet_balances::Pallet::<T>::set_balance(
Expand All @@ -104,6 +121,34 @@ pub fn give_musd_to<T: Config>(dest: AccountId32, amount: Balance) {
.unwrap();
}

pub fn give_investor_role<T: Config>(
investor: AccountId32,
pool_id: PoolId,
tranche_id: TrancheId,
) {
let role = Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, Moment::MAX));
pallet_permissions::Pallet::<T>::add(
RawOrigin::Root.into(),
role,
investor,
PermissionScope::Pool(pool_id),
role,
)
.unwrap();
}

pub fn give_borrower_role<T: Config>(borrower: AccountId32, pool_id: PoolId) {
let role = Role::PoolRole(PoolRole::Borrower);
pallet_permissions::Pallet::<T>::add(
RawOrigin::Root.into(),
role,
borrower,
PermissionScope::Pool(pool_id),
role,
)
.unwrap();
}

pub fn create_pool<T: Config>(admin: AccountId32, pool_id: PoolId) {
pallet_pool_registry::Pallet::<T>::register(
match T::KIND {
Expand Down Expand Up @@ -155,34 +200,6 @@ pub fn invest<T: Config>(
.unwrap();
}

pub fn give_investor_role<T: Config>(
investor: AccountId32,
pool_id: PoolId,
tranche_id: TrancheId,
) {
let role = Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, Moment::MAX));
pallet_permissions::Pallet::<T>::add(
RawOrigin::Root.into(),
role,
investor,
PermissionScope::Pool(pool_id),
role,
)
.unwrap();
}

pub fn give_borrower_role<T: Config>(borrower: AccountId32, pool_id: PoolId) {
let role = Role::PoolRole(PoolRole::Borrower);
pallet_permissions::Pallet::<T>::add(
RawOrigin::Root.into(),
role,
borrower,
PermissionScope::Pool(pool_id),
role,
)
.unwrap();
}

// Utilities that does not modify the state
pub mod get {
use super::*;
Expand Down

0 comments on commit 7ffa045

Please sign in to comment.