Skip to content

Commit

Permalink
transfer relay tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jun 12, 2024
1 parent e9174c9 commit 57908e9
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 296 deletions.
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;

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
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
186 changes: 0 additions & 186 deletions runtime/integration-tests/src/generic/cases/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5065,138 +5065,6 @@ mod altair {

use utils::*;

mod transfers {
use super::*;

fn transfer_ksm_from_relay_chain<T: Runtime + FudgeSupport>(
env: &mut FudgeEnv<T>,
transfer_amount: Balance,
currency_id: CurrencyId,
meta: AssetMetadata,
) {
env.parachain_state_mut(|| {
assert_ok!(orml_asset_registry::Pallet::<T>::register_asset(
<T as frame_system::Config>::RuntimeOrigin::root(),
meta.clone(),
Some(currency_id),
));

assert_eq!(
orml_tokens::Pallet::<T>::free_balance(currency_id, &Keyring::Bob.into()),
0
);
});

env.relay_state_mut(|| {
assert_ok!(
pallet_balances::Pallet::<FudgeRelayRuntime<T>>::force_set_balance(
<FudgeRelayRuntime<T> as frame_system::Config>::RuntimeOrigin::root(),
<FudgeRelayRuntime<T> as frame_system::Config>::Lookup::unlookup(
Keyring::Alice.id()
),
transfer_amount * 2,
)
);

assert_ok!(
pallet_xcm::Pallet::<FudgeRelayRuntime<T>>::force_xcm_version(
<FudgeRelayRuntime<T> as frame_system::Config>::RuntimeOrigin::root(),
Box::new(Location::new(
0,
Junction::Parachain(T::FudgeHandle::PARA_ID),
)),
XCM_VERSION,
)
);

assert_ok!(
pallet_xcm::Pallet::<FudgeRelayRuntime<T>>::reserve_transfer_assets(
RawOrigin::Signed(Keyring::Alice.into()).into(),
Box::new(Parachain(T::FudgeHandle::PARA_ID).into()),
Box::new(
Junction::AccountId32 {
network: None,
id: Keyring::Bob.into(),
}
.into()
),
Box::new((Here, transfer_amount).into()),
0
)
);
});

env.pass(Blocks::ByNumber(2));

env.parachain_state(|| {
assert_eq!(
orml_tokens::Pallet::<T>::free_balance(currency_id, &Keyring::Bob.into()),
1991963000000 // Comes from `transfer_amount - fee(meta.decimals)` with noise
);
});
}

#[test_runtimes([altair])]
fn transfer_ksm_to_and_from_relay_chain<T: Runtime + FudgeSupport>() {
let mut env = FudgeEnv::<T>::default();

let transfer_amount: Balance = ksm(2);
let currency_id = CurrencyId::ForeignAsset(3001);
let meta: AssetMetadata = AssetMetadata {
decimals: 12,
name: BoundedVec::default(),
symbol: BoundedVec::default(),
existential_deposit: 1_000_000_000,
location: Some(VersionedLocation::V4(Location::new(1, Here))),
additional: CustomMetadata {
transferability: CrossChainTransferability::Xcm(Default::default()),
..CustomMetadata::default()
},
};

// First we need some KSM on Altair
transfer_ksm_from_relay_chain(&mut env, transfer_amount, currency_id, meta.clone());

let currency_id = CurrencyId::ForeignAsset(3001);

env.parachain_state_mut(|| {
assert_ok!(pallet_xcm::Pallet::<T>::force_xcm_version(
<T as frame_system::Config>::RuntimeOrigin::root(),
Box::new(Location::new(1, Junctions::Here)),
XCM_VERSION,
));

assert_ok!(orml_xtokens::Pallet::<T>::transfer(
RawOrigin::Signed(Keyring::Bob.into()).into(),
currency_id,
ksm(1),
Box::new(
Location::new(
1,
Junction::AccountId32 {
id: Keyring::Bob.into(),
network: None,
}
)
.into()
),
WeightLimit::Limited(4_000_000_000.into())
));
});

env.pass(Blocks::ByNumber(2));

env.relay_state_mut(|| {
assert_eq!(
pallet_balances::Pallet::<FudgeRelayRuntime<T>>::free_balance(
&Keyring::Bob.into()
),
999989698923
);
});
}
}

mod asset_registry {
use super::*;

Expand Down Expand Up @@ -6762,58 +6630,4 @@ mod centrifuge {
});
}
}

mod transfers {
use super::*;
#[test_runtimes([centrifuge])]
fn transfer_dot_to_and_from_relay_chain<T: Runtime + FudgeSupport>() {
let mut env = FudgeEnv::<T>::default();

transfer_dot_from_relay_chain(&mut env);

env.parachain_state_mut(|| {
let alice_initial_dot =
orml_tokens::Pallet::<T>::free_balance(DOT_ASSET_ID, &Keyring::Alice.into());

assert_ok!(pallet_xcm::Pallet::<T>::force_xcm_version(
<T as frame_system::Config>::RuntimeOrigin::root(),
Box::new(Location::new(1, Junctions::Here)),
XCM_VERSION,
));

assert_ok!(orml_xtokens::Pallet::<T>::transfer(
RawOrigin::Signed(Keyring::Alice.into()).into(),
DOT_ASSET_ID,
dot(1),
Box::new(
Location::new(
1,
Junction::AccountId32 {
id: Keyring::Alice.into(),
network: None,
}
)
.into()
),
WeightLimit::Unlimited,
));

assert_eq!(
orml_tokens::Pallet::<T>::free_balance(DOT_ASSET_ID, &Keyring::Alice.into()),
alice_initial_dot - dot(1),
);
});

env.pass(Blocks::ByNumber(2));

env.relay_state_mut(|| {
assert_eq!(
pallet_balances::Pallet::<FudgeRelayRuntime<T>>::free_balance(
&Keyring::Alice.into()
),
79857365914
);
});
}
}
}
Loading

0 comments on commit 57908e9

Please sign in to comment.