Skip to content

Commit

Permalink
schedule_upgrade cancel_upgrade and update_tranche_token_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jul 17, 2024
1 parent 8a4fc2a commit b3c8412
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 139 deletions.
5 changes: 0 additions & 5 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,6 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

ensure!(
T::PoolInspect::tranche_exists(pool_id, tranche_id),
Error::<T>::TrancheNotFound
);

let investment_id = Self::derive_invest_id(pool_id, tranche_id)?;
let metadata = T::AssetRegistry::metadata(&investment_id.into())
.ok_or(Error::<T>::TrancheMetadataNotFound)?;
Expand Down
185 changes: 185 additions & 0 deletions pallets/liquidity-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,101 @@ mod add_tranche {
}
}

mod update_tranche_token_metadata {
use super::*;

#[test]
fn success() {
System::externalities().execute_with(|| {
Pools::mock_pool_exists(|_| true);
Pools::mock_tranche_exists(|_, _| true);
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, ALICE);
assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain());
assert_eq!(
msg,
Message::UpdateTrancheTokenMetadata {
pool_id: POOL_ID,
tranche_id: TRANCHE_ID,
token_name: vec_to_fixed_array(NAME),
token_symbol: vec_to_fixed_array(SYMBOL),
}
);
Ok(())
});

assert_ok!(LiquidityPools::update_tranche_token_metadata(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_DOMAIN_ADDRESS.domain(),
));
})
}

mod erroring_out {
use super::*;

#[test]
fn with_wrong_pool() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
Pools::mock_pool_exists(|_| false);

assert_noop!(
LiquidityPools::update_tranche_token_metadata(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_DOMAIN_ADDRESS.domain(),
),
Error::<Runtime>::PoolNotFound
);
})
}

#[test]
fn with_wrong_tranche() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
Pools::mock_pool_exists(|_| true);
Pools::mock_tranche_exists(|_, _| false);

assert_noop!(
LiquidityPools::update_tranche_token_metadata(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_DOMAIN_ADDRESS.domain(),
),
Error::<Runtime>::TrancheNotFound,
);
})
}

#[test]
fn with_no_metadata() {
System::externalities().execute_with(|| {
Pools::mock_pool_exists(|_| true);
Pools::mock_tranche_exists(|_, _| true);
AssetRegistry::mock_metadata(|_| None);

assert_noop!(
LiquidityPools::update_tranche_token_metadata(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_DOMAIN_ADDRESS.domain(),
),
Error::<Runtime>::TrancheMetadataNotFound,
);
})
}
}
}

mod update_token_price {
use super::*;

Expand Down Expand Up @@ -1248,6 +1343,96 @@ mod disallow_investment_currency {
}
}

mod schedule_upgrade {
use super::*;

#[test]
fn success() {
System::externalities().execute_with(|| {
Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, TreasuryAccount::get());
assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain());
assert_eq!(
msg,
Message::ScheduleUpgrade {
contract: CONTRACT_ACCOUNT
}
);
Ok(())
});

assert_ok!(LiquidityPools::schedule_upgrade(
RuntimeOrigin::root(),
CHAIN_ID,
CONTRACT_ACCOUNT,
));
})
}

mod erroring_out {
use super::*;

#[test]
fn with_origin() {
System::externalities().execute_with(|| {
assert_noop!(
LiquidityPools::schedule_upgrade(
RuntimeOrigin::signed(ALICE),
CHAIN_ID,
CONTRACT_ACCOUNT,
),
DispatchError::BadOrigin
);
})
}
}
}

mod cancel_upgrade {
use super::*;

#[test]
fn success() {
System::externalities().execute_with(|| {
Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, TreasuryAccount::get());
assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain());
assert_eq!(
msg,
Message::CancelUpgrade {
contract: CONTRACT_ACCOUNT
}
);
Ok(())
});

assert_ok!(LiquidityPools::cancel_upgrade(
RuntimeOrigin::root(),
CHAIN_ID,
CONTRACT_ACCOUNT,
));
})
}

mod erroring_out {
use super::*;

#[test]
fn with_origin() {
System::externalities().execute_with(|| {
assert_noop!(
LiquidityPools::cancel_upgrade(
RuntimeOrigin::signed(ALICE),
CHAIN_ID,
CONTRACT_ACCOUNT,
),
DispatchError::BadOrigin
);
})
}
}
}

#[test]
fn receiving_invalid_message() {
System::externalities().execute_with(|| {
Expand Down
142 changes: 8 additions & 134 deletions runtime/integration-tests/src/cases/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use runtime_common::{
};
use sp_core::{Get, H160};
use sp_runtime::{
traits::{AccountIdConversion, BadOrigin, Convert, EnsureAdd, One, Zero},
traits::{AccountIdConversion, Convert, EnsureAdd, One, Zero},
BoundedVec, DispatchError, FixedPointNumber, Perquintill, SaturatedConversion,
};
use staging_xcm::{
Expand All @@ -50,6 +50,13 @@ use crate::{
utils::{accounts::Keyring, genesis, genesis::Genesis, orml_asset_registry},
};

// ------------------
// NOTE
// This file only contains foreign investments tests, but the name must remain
// as it is until feature lpv2 is merged to avoid conflicts:
// (https://github.com/centrifuge/centrifuge-chain/pull/1909)
// ------------------

/// The AUSD asset id
pub const AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(3);
/// The USDT asset id
Expand Down Expand Up @@ -662,139 +669,6 @@ mod utils {

use utils::*;

mod add_allow_upgrade {
use super::*;

#[test_runtimes([development])]
fn schedule_upgrade<T: Runtime + FudgeSupport>() {
let mut env = FudgeEnv::<T>::from_parachain_storage(
Genesis::default()
.add(genesis::balances::<T>(cfg(1_000)))
.add(genesis::tokens::<T>(vec![(
GLMR_CURRENCY_ID,
DEFAULT_BALANCE_GLMR,
)]))
.storage(),
);

setup_test(&mut env);

env.parachain_state_mut(|| {
// Only Root can call `schedule_upgrade`
assert_noop!(
pallet_liquidity_pools::Pallet::<T>::schedule_upgrade(
RawOrigin::Signed(Keyring::Bob.into()).into(),
MOONBEAM_EVM_CHAIN_ID,
[7; 20]
),
BadOrigin
);

// Now it finally works
assert_ok!(pallet_liquidity_pools::Pallet::<T>::schedule_upgrade(
<T as frame_system::Config>::RuntimeOrigin::root(),
MOONBEAM_EVM_CHAIN_ID,
[7; 20]
));
});
}

#[test_runtimes([development])]
fn cancel_upgrade<T: Runtime + FudgeSupport>() {
let mut env = FudgeEnv::<T>::from_parachain_storage(
Genesis::default()
.add(genesis::balances::<T>(cfg(1_000)))
.add(genesis::tokens::<T>(vec![(
GLMR_CURRENCY_ID,
DEFAULT_BALANCE_GLMR,
)]))
.storage(),
);

setup_test(&mut env);

env.parachain_state_mut(|| {
// Only Root can call `cancel_upgrade`
assert_noop!(
pallet_liquidity_pools::Pallet::<T>::cancel_upgrade(
RawOrigin::Signed(Keyring::Bob.into()).into(),
MOONBEAM_EVM_CHAIN_ID,
[7; 20]
),
BadOrigin
);

// Now it finally works
assert_ok!(pallet_liquidity_pools::Pallet::<T>::cancel_upgrade(
<T as frame_system::Config>::RuntimeOrigin::root(),
MOONBEAM_EVM_CHAIN_ID,
[7; 20]
));
});
}

#[test_runtimes([development])]
fn update_tranche_token_metadata<T: Runtime + FudgeSupport>() {
let mut env = FudgeEnv::<T>::from_parachain_storage(
Genesis::default()
.add(genesis::balances::<T>(cfg(1_000)))
.add(genesis::tokens::<T>(vec![(
GLMR_CURRENCY_ID,
DEFAULT_BALANCE_GLMR,
)]))
.storage(),
);

setup_test(&mut env);

env.parachain_state_mut(|| {
let pool_id = POOL_ID;
// NOTE: Default pool admin is BOB
create_ausd_pool::<T>(pool_id);

// Missing tranche token should throw
let nonexistent_tranche = [71u8; 16];

assert_noop!(
pallet_liquidity_pools::Pallet::<T>::update_tranche_token_metadata(
RawOrigin::Signed(Keyring::Alice.into()).into(),
pool_id,
nonexistent_tranche,
Domain::EVM(MOONBEAM_EVM_CHAIN_ID),
),
pallet_liquidity_pools::Error::<T>::TrancheNotFound
);

let tranche_id = default_tranche_id::<T>(pool_id);

// Moving the update to another domain can be called by anyone
assert_ok!(
pallet_liquidity_pools::Pallet::<T>::update_tranche_token_metadata(
RawOrigin::Signed(Keyring::Alice.into()).into(),
pool_id,
tranche_id,
Domain::EVM(MOONBEAM_EVM_CHAIN_ID),
)
);

// Edge case: Should throw if tranche exists but metadata does not exist
let tranche_currency_id = CurrencyId::Tranche(pool_id, tranche_id);

orml_asset_registry::Metadata::<T>::remove(tranche_currency_id);

assert_noop!(
pallet_liquidity_pools::Pallet::<T>::update_tranche_token_metadata(
RawOrigin::Signed(POOL_ADMIN.into()).into(),
pool_id,
tranche_id,
Domain::EVM(MOONBEAM_EVM_CHAIN_ID),
),
pallet_liquidity_pools::Error::<T>::TrancheMetadataNotFound
);
});
}
}

mod foreign_investments {
use super::*;

Expand Down

0 comments on commit b3c8412

Please sign in to comment.