Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IT: Simplify liquidity_pool test module #1869

Merged
merged 25 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ea08cec
basic xtransfer tests
lemunozm Jun 11, 2024
0bc96f6
parachain transfer tested
lemunozm Jun 11, 2024
e9174c9
fix tests
lemunozm Jun 11, 2024
57908e9
transfer relay tests
lemunozm Jun 12, 2024
1770f11
polish and minor cleans
lemunozm Jun 13, 2024
5b41f5e
fixes
lemunozm Jun 13, 2024
cdf8837
Merge remote-tracking branch 'origin/main' into it/simplify-xtransfer…
lemunozm Jun 13, 2024
8dd05e3
assets tests
lemunozm Jun 13, 2024
bf338c8
currency conversion tests
lemunozm Jun 13, 2024
126eeb5
fix cargo fmt
lemunozm Jun 13, 2024
891bfe7
fix tests
lemunozm Jun 14, 2024
d781110
reduce xcm restricted_transfer tests
lemunozm Jun 14, 2024
eeb1dbf
rename file
lemunozm Jun 14, 2024
bdee3ed
restricted ethereum tests simplified and moved to restricted
lemunozm Jun 14, 2024
0c3bb43
cleaning warnings
lemunozm Jun 14, 2024
5a832ad
removed re-added tranche tokens tests
lemunozm Jun 17, 2024
de3760a
remove fudge from proxy tests
lemunozm Jun 17, 2024
1b79822
Merge remote-tracking branch 'origin/main' into it/simplify-xtransfer…
lemunozm Jun 17, 2024
abace1f
minor cleans
lemunozm Jun 17, 2024
a2f7119
fix warnings
lemunozm Jun 18, 2024
d24c686
decouple PARA_ID and SIBLING_ID from FudgeHandle
lemunozm Jun 20, 2024
415a158
Merge remote-tracking branch 'origin/main' into it/simplify-xtransfer…
lemunozm Jun 24, 2024
cce79b6
revert liquidity-pool changes
lemunozm Jun 24, 2024
7f79140
Merge branch 'main' into it/simplify-xtransfer-tests
lemunozm Jun 24, 2024
3991dea
William suggestions
lemunozm Jun 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ pub mod constants {

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

lemunozm marked this conversation as resolved.
Show resolved Hide resolved
pub const NATIVE_KEY: &[u8] = &[0, 1];
}

/// Listing of parachains we integrate with.
Expand All @@ -301,7 +303,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 +315,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),
})
}
Comment on lines +368 to +383
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tech-debt to create some tests to be able to compute the exact fee charged in the above two cases. Using FixedRateOfFungible and using AssetRegistryTrader

}

/// 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);
Comment on lines +81 to +86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, here I would expect a symmetric behavior, but it isn't.

The implementation only checks the transferability when converting from location -> currency ref. Should we add the same filter for currency -> location ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly not sure if we want this to be symmetric. AFAICT, we could switch to that based on my below observations.

If we decide on that, it blocks converting any local currency locations into their corresponding currency id. One implication is that those currencies cannot be used for paying XCM transaction fees. On mainnet, CFG is configured as XCM transferable.

It would be interesting to see the failing tests when switching to symmetric behaviour. Depending on the failing tests, this could be a good indicator on whether we require location-to-currency location for local currencies as part of XCM for instance.

AFAICS, the CurrencyIdConvert is used to determine and withdraw cross-chain fee amounts in the configured fee currency in three different pallets:

  1. As orml_xtokens::CurrencyIdConvert to derive the asset origination location
  2. As pallet_xcm_transactor::CurrencyIdLocation to withdraw the fee amount of the configured fee currency
  3. In staging_xcm_executor::Config::AssetTransactor = FungiblesTransactor as Matcher AT of our FungiblesAdaptor impl to withdraw the XCM fee in the configured fee currencies

Curious about your thoughts @mustermeiszer

});
}

#[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
Loading