Skip to content

Commit

Permalink
IT: Simplify liquidity_pool test module (#1869)
Browse files Browse the repository at this point in the history
* basic xtransfer tests

* parachain transfer tested

* fix tests

* transfer relay tests

* polish and minor cleans

* fixes

* assets tests

* currency conversion tests

* fix cargo fmt

* fix tests

* reduce xcm restricted_transfer tests

* rename file

* restricted ethereum tests simplified and moved to restricted

* cleaning warnings

* removed re-added tranche tokens tests

* remove fudge from proxy tests

* minor cleans

* fix warnings

* decouple PARA_ID and SIBLING_ID from FudgeHandle

* revert liquidity-pool changes

* William suggestions
  • Loading branch information
lemunozm authored Jun 24, 2024
1 parent 737278c commit ee8fc69
Show file tree
Hide file tree
Showing 25 changed files with 4,838 additions and 7,596 deletions.
7 changes: 5 additions & 2 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ pub mod constants {

/// The maximum number of pool fees per pool fee bucket
pub const MAX_POOL_FEES_PER_BUCKET: u32 = 100;

/// Identification of the native token of the chain. Used in XCM locations.
pub const NATIVE_KEY: &[u8] = &[0, 1];
}

/// Listing of parachains we integrate with.
Expand All @@ -301,7 +304,7 @@ pub mod parachains {

pub mod altair {
pub const ID: u32 = 2088;
pub const AIR_KEY: &[u8] = &[0, 1];
pub const AIR_KEY: &[u8] = crate::NATIVE_KEY;
}
}

Expand All @@ -313,7 +316,7 @@ pub mod parachains {

pub mod centrifuge {
pub const ID: u32 = 2031;
pub const CFG_KEY: &[u8] = &[0, 1];
pub const CFG_KEY: &[u8] = crate::NATIVE_KEY;
}
}

Expand Down
17 changes: 17 additions & 0 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,23 @@ impl CrossChainTransferability {
pub fn includes_liquidity_pools(self) -> bool {
matches!(self, Self::LiquidityPools)
}

/// Fees will be charged using `FixedRateOfFungible`.
#[cfg(feature = "std")]
pub fn xcm_default() -> Self {
Self::Xcm(XcmMetadata {
fee_per_second: None,
})
}

/// Fees will be charged using `AssetRegistryTrader`.
/// If value is 0, no fees will be charged.
#[cfg(feature = "std")]
pub fn xcm_with_fees(value: Balance) -> Self {
Self::Xcm(XcmMetadata {
fee_per_second: Some(value),
})
}
}

/// Liquidity Pools-wrapped tokens
Expand Down
22 changes: 3 additions & 19 deletions runtime/altair/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_primitives::{
parachains,
types::{EnsureRootOr, HalfOfCouncil},
};
use cfg_primitives::types::{EnsureRootOr, HalfOfCouncil};
use cfg_types::tokens::CurrencyId;
use frame_support::{
parameter_types,
Expand All @@ -27,10 +24,9 @@ use pallet_xcm::XcmPassthrough;
use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
general_key, AccountIdToLocation, Barrier, FixedConversionRateProvider,
AccountIdToLocation, Barrier, CanonicalNativePerSecond, FixedConversionRateProvider,
LocalOriginToLocation, ToTreasury,
},
xcm_fees::native_per_second,
};
use sp_core::ConstU32;
use staging_xcm::{
Expand Down Expand Up @@ -88,25 +84,13 @@ impl staging_xcm_executor::Config for XcmConfig {
/// else the xcm executor won't know how to charge fees for a transfer of said
/// token.
pub type Trader = (
FixedRateOfFungible<CanonicalAirPerSecond, ToTreasury<Runtime>>,
FixedRateOfFungible<CanonicalNativePerSecond, ToTreasury<Runtime>>,
AssetRegistryTrader<
FixedRateAssetRegistryTrader<FixedConversionRateProvider<OrmlAssetRegistry>>,
ToTreasury<Runtime>,
>,
);

parameter_types! {
// Canonical location: https://github.com/paritytech/polkadot/pull/4470
pub CanonicalAirPerSecond: (AssetId, u128, u128) = (
Location::new(
0,
general_key(parachains::kusama::altair::AIR_KEY)
).into(),
native_per_second(),
0,
);
}

/// Means for transacting the fungibles assets of this parachain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation
Expand Down
22 changes: 3 additions & 19 deletions runtime/centrifuge/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_primitives::{
parachains,
types::{EnsureRootOr, HalfOfCouncil},
};
use cfg_primitives::types::{EnsureRootOr, HalfOfCouncil};
use cfg_traits::TryConvert;
use cfg_types::{tokens::CurrencyId, EVMChainId};
use frame_support::{
Expand All @@ -28,10 +25,9 @@ use pallet_xcm::XcmPassthrough;
use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
general_key, AccountIdToLocation, Barrier, FixedConversionRateProvider,
AccountIdToLocation, Barrier, CanonicalNativePerSecond, FixedConversionRateProvider,
LocalOriginToLocation, LpInstanceRelayer, ToTreasury,
},
xcm_fees::native_per_second,
};
use sp_core::ConstU32;
use staging_xcm::{
Expand Down Expand Up @@ -89,25 +85,13 @@ impl staging_xcm_executor::Config for XcmConfig {
/// else the xcm executor won't know how to charge fees for a transfer of said
/// token.
pub type Trader = (
FixedRateOfFungible<CanonicalCfgPerSecond, ToTreasury<Runtime>>,
FixedRateOfFungible<CanonicalNativePerSecond, ToTreasury<Runtime>>,
AssetRegistryTrader<
FixedRateAssetRegistryTrader<FixedConversionRateProvider<OrmlAssetRegistry>>,
ToTreasury<Runtime>,
>,
);

parameter_types! {
// Canonical location: https://github.com/paritytech/polkadot/pull/4470
pub CanonicalCfgPerSecond: (AssetId, u128, u128) = (
Location::new(
0,
general_key(parachains::polkadot::centrifuge::CFG_KEY),
).into(),
native_per_second(),
0,
);
}

/// Means for transacting the fungibles assets of this parachain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation
Expand Down
14 changes: 13 additions & 1 deletion runtime/common/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use staging_xcm_builder::{
TakeWeightCredit,
};

use crate::xcm_fees::default_per_second;
use crate::xcm_fees::{default_per_second, native_per_second};

/// Our FixedConversionRateProvider, used to charge XCM-related fees for
/// tokens registered in the asset registry that were not already handled by
Expand Down Expand Up @@ -71,6 +71,18 @@ pub fn general_key(data: &[u8]) -> staging_xcm::latest::Junction {
}
}

frame_support::parameter_types! {
// Canonical location: https://github.com/paritytech/polkadot/pull/4470
pub CanonicalNativePerSecond: (AssetId, u128, u128) = (
Location::new(
0,
general_key(cfg_primitives::NATIVE_KEY),
).into(),
native_per_second(),
0,
);
}

/// How we convert an `[AccountId]` into an XCM Location
pub struct AccountIdToLocation;
impl<AccountId: Into<[u8; 32]>> Convert<AccountId, Location> for AccountIdToLocation {
Expand Down
21 changes: 2 additions & 19 deletions runtime/development/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
use cfg_primitives::{
parachains,
types::{EnsureRootOr, HalfOfCouncil},
};
use cfg_primitives::types::{EnsureRootOr, HalfOfCouncil};
use cfg_traits::TryConvert;
use cfg_types::{tokens::CurrencyId, EVMChainId};
use frame_support::{
Expand All @@ -27,10 +24,9 @@ use pallet_xcm::XcmPassthrough;
use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
general_key, AccountIdToLocation, Barrier, FixedConversionRateProvider,
AccountIdToLocation, Barrier, CanonicalNativePerSecond, FixedConversionRateProvider,
LocalOriginToLocation, LpInstanceRelayer, ToTreasury,
},
xcm_fees::native_per_second,
};
use sp_core::ConstU32;
use staging_xcm::{
Expand Down Expand Up @@ -95,19 +91,6 @@ pub type Trader = (
>,
);

parameter_types! {
// Canonical location: https://github.com/paritytech/polkadot/pull/4470
pub CanonicalNativePerSecond: (AssetId, u128, u128) = (
Location::new(
0,
general_key(parachains::kusama::altair::AIR_KEY),
).into(),
native_per_second(),
0,
);

}

/// Means for transacting the fungibles assets of this parachain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation
Expand Down
54 changes: 54 additions & 0 deletions runtime/integration-tests/src/generic/cases/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use cfg_types::tokens::CurrencyId;
use frame_support::{assert_noop, assert_ok, dispatch::RawOrigin};
use sp_runtime::{DispatchError, DispatchError::BadOrigin};

use crate::{
generic::{
config::Runtime, env::Env, envs::runtime_env::RuntimeEnv, utils::currency::default_metadata,
},
utils::orml_asset_registry,
};

#[test_runtimes(all)]
fn authority_configured<T: Runtime>() {
let mut env = RuntimeEnv::<T>::default();

env.parachain_state_mut(|| {
assert_ok!(orml_asset_registry::Pallet::<T>::register_asset(
RawOrigin::Root.into(),
default_metadata(),
Some(CurrencyId::Native)
));

assert_ok!(orml_asset_registry::Pallet::<T>::register_asset(
RawOrigin::Root.into(),
default_metadata(),
Some(CurrencyId::ForeignAsset(42))
));

assert_noop!(
orml_asset_registry::Pallet::<T>::register_asset(
RawOrigin::Root.into(),
default_metadata(),
Some(CurrencyId::Tranche(42, [1; 16]))
),
BadOrigin
);
});
}

#[test_runtimes(all)]
fn processor_configured<T: Runtime>() {
let mut env = RuntimeEnv::<T>::default();

env.parachain_state_mut(|| {
assert_noop!(
orml_asset_registry::Pallet::<T>::register_asset(
RawOrigin::Root.into(),
default_metadata(),
None
),
DispatchError::Other("asset-registry: AssetId is required")
);
});
}
100 changes: 100 additions & 0 deletions runtime/integration-tests/src/generic/cases/currency_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use cfg_types::tokens::CurrencyId;
use orml_traits::asset_registry::AssetMetadata;
use runtime_common::xcm::CurrencyIdConvert;
use sp_runtime::traits::Convert;
use staging_xcm::{
v4::{Junction::*, Junctions::Here, Location},
VersionedLocation,
};

use crate::generic::{
config::Runtime,
env::Env,
envs::runtime_env::RuntimeEnv,
utils::{
currency::{default_metadata, CurrencyInfo, CustomCurrency},
genesis::{self, Genesis},
xcm::transferable_custom,
},
};

const PARA_ID: u32 = 1000;

#[test_runtimes(all)]
fn convert_transferable_asset<T: Runtime>() {
// The way the native currency is represented relative to its runtime
let location_inner = Location::new(0, Here);

// The canonical way the native currency is represented out in the wild
let location_canonical = Location::new(1, Parachain(PARA_ID));

let curr = CustomCurrency(
CurrencyId::ForeignAsset(1),
AssetMetadata {
decimals: 18,
location: Some(VersionedLocation::V4(location_canonical.clone())),
additional: transferable_custom(),
..default_metadata()
},
);

let env = RuntimeEnv::<T>::from_parachain_storage(
Genesis::default()
.add(genesis::parachain_id::<T>(PARA_ID))
.add(genesis::assets::<T>([(curr.id(), curr.metadata())]))
.storage(),
);

env.parachain_state(|| {
assert_eq!(
CurrencyIdConvert::<T>::convert(location_inner),
Some(curr.id()),
);

assert_eq!(
CurrencyIdConvert::<T>::convert(curr.id()),
Some(location_canonical)
)
});
}

#[test_runtimes(all)]
fn cannot_convert_nontransferable_asset<T: Runtime>() {
let curr = CustomCurrency(
CurrencyId::ForeignAsset(1),
AssetMetadata {
decimals: 18,
location: Some(VersionedLocation::V4(Location::new(1, Parachain(PARA_ID)))),
additional: Default::default(), // <- Not configured for transfers
..default_metadata()
},
);

let env = RuntimeEnv::<T>::from_parachain_storage(
Genesis::default()
.add(genesis::parachain_id::<T>(PARA_ID))
.add(genesis::assets::<T>([(curr.id(), curr.metadata())]))
.storage(),
);

env.parachain_state(|| {
assert_eq!(
CurrencyIdConvert::<T>::convert(Location::new(0, Here)),
Some(curr.id()),
);

assert_eq!(CurrencyIdConvert::<T>::convert(curr.id()), None);
});
}

#[test_runtimes(all)]
fn convert_unknown_location<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

env.parachain_state(|| {
assert_eq!(
CurrencyIdConvert::<T>::convert(Location::new(0, Here)),
None,
);
});
}
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/generic/cases/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod common {
.add(genesis::balances::<T>(
T::ExistentialDeposit::get() + FOR_FEES,
))
.add(genesis::assets::<T>(vec![Box::new(Usd6)]))
.add(genesis::assets::<T>(vec![(Usd6.id(), &Usd6.metadata())]))
.add(genesis::tokens::<T>(vec![(Usd6.id(), Usd6.ed())]))
.storage(),
);
Expand Down
Loading

0 comments on commit ee8fc69

Please sign in to comment.