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

Chore: remove hardcode xcm config #1891

Merged
merged 11 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion integration-tests/src/kusama_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ decl_test_parachain! {
Origin = heiko_runtime::Origin,
XcmpMessageHandler = heiko_runtime::XcmpQueue,
DmpMessageHandler = heiko_runtime::DmpQueue,
new_ext = para_ext(2000),
new_ext = sibling_ext(2000),
}
}

Expand Down Expand Up @@ -156,6 +156,11 @@ pub fn para_ext(parachain_id: u32) -> sp_io::TestExternalities {
ext.parachain_id(parachain_id).heiko_build()
}

pub fn sibling_ext(parachain_id: u32) -> sp_io::TestExternalities {
let ext = ExtBuilder { parachain_id };
ext.parachain_id(parachain_id).karura_build()
}

pub fn stamine_ext() -> sp_io::TestExternalities {
let ext = ExtBuilder { parachain_id: 1000 };
ext.parachain_id(1000).statemine_build()
Expand Down
7 changes: 6 additions & 1 deletion integration-tests/src/polkadot_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ decl_test_parachain! {
Origin = parallel_runtime::Origin,
XcmpMessageHandler = parallel_runtime::XcmpQueue,
DmpMessageHandler = parallel_runtime::DmpQueue,
new_ext = para_ext(2002),
new_ext = sibling_ext(2002),
}
}

Expand Down Expand Up @@ -152,3 +152,8 @@ pub fn para_ext(parachain_id: u32) -> sp_io::TestExternalities {
let ext = ExtBuilder { parachain_id };
ext.parachain_id(parachain_id).parallel_build()
}

pub fn sibling_ext(parachain_id: u32) -> sp_io::TestExternalities {
let ext = ExtBuilder { parachain_id };
ext.parachain_id(parachain_id).clv_build()
}
196 changes: 153 additions & 43 deletions integration-tests/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use pallet_traits::{
xcm::AssetType,
};
use polkadot_parachain::primitives::Sibling;
use primitives::{tokens::*, AccountId, Balance, CurrencyId, Rate, Ratio};
use primitives::{paras, tokens::*, AccountId, Balance, CurrencyId, Rate, Ratio};
use sp_runtime::{
traits::{AccountIdConversion, One},
FixedPointNumber, MultiAddress,
Expand All @@ -45,6 +45,10 @@ pub const RMRK_ASSET_ID: u32 = 8;
pub const USDT_ASSET_ID: u32 = 1984;
pub const RMRK: CurrencyId = 126;

pub const PARA_WEIGHT_PER_SEC: u128 = 231_740_000_000;
pub const HKO_WEIGHT_PER_SEC: u128 = 231_740_000_000;
pub const DOT_WEIGHT_PER_SEC: u128 = 231_740_000_000;
pub const KSM_WEIGHT_PER_SEC: u128 = 231_740_000_000;
pub const RMRK_WEIGHT_PER_SEC: u128 = 20_000_000_000;
pub const USDT_WEIGHT_PER_SEC: u128 = 30_000_000;
pub const KAR_WEIGHT_PER_SEC: u128 = 30_000_000_000;
Expand Down Expand Up @@ -116,50 +120,10 @@ impl ExtBuilder {

pub fn heiko_build(self) -> sp_io::TestExternalities {
use heiko_runtime::{
constants::paras::statemine::{
ID as StatemineChainId, PALLET_INSTANCE as StatemineAssetInstance,
},
AssetRegistry, Assets, LiquidStaking, Loans, Origin, Runtime, System, XcmHelper,
};
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();

<parachain_info::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&parachain_info::GenesisConfig {
parachain_id: self.parachain_id.into(),
},
&mut t,
)
.unwrap();

<pallet_xcm::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_xcm::GenesisConfig {
safe_xcm_version: Some(2),
},
&mut t,
)
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(AccountId::from(ALICE), heiko(100)),
(AccountId::from(BOB), heiko(100)),
],
}
.assimilate_storage(&mut t)
.unwrap();

<pallet_liquid_staking::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_liquid_staking::GenesisConfig {
exchange_rate: Rate::one(),
reserve_factor: Ratio::from_perthousand(5),
},
&mut t,
)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
use paras::statemine::{ID as StatemineChainId, PALLET_INSTANCE as StatemineAssetInstance};
let mut ext = sp_io::TestExternalities::new(self.init());
ext.execute_with(|| {
System::set_block_number(1);
Assets::force_create(
Expand Down Expand Up @@ -206,6 +170,35 @@ impl ExtBuilder {
)
.unwrap();

let hko_asset_location = MultiLocation::new(
1,
X2(
Parachain(2085),
GeneralKey(WeakBoundedVec::<u8, ConstU32<32>>::force_from(
b"HKO".to_vec(),
None,
)),
),
);
let hko_asset_type = AssetType::Xcm(hko_asset_location);
AssetRegistry::register_asset(Origin::root(), HKO, hko_asset_type.clone()).unwrap();
AssetRegistry::update_asset_units_per_second(
Origin::root(),
hko_asset_type,
HKO_WEIGHT_PER_SEC,
)
.unwrap();

let ksm_asset_location = MultiLocation::parent();
let ksm_asset_type = AssetType::Xcm(ksm_asset_location);
AssetRegistry::register_asset(Origin::root(), KSM, ksm_asset_type.clone()).unwrap();
AssetRegistry::update_asset_units_per_second(
Origin::root(),
ksm_asset_type,
KSM_WEIGHT_PER_SEC,
)
.unwrap();

let statemine_rmrk_asset_location = MultiLocation::new(
1,
X3(
Expand Down Expand Up @@ -427,6 +420,33 @@ impl ExtBuilder {
false,
)
.unwrap();
let para_asset_location = MultiLocation::new(
1,
X2(
Parachain(2012),
GeneralKey(WeakBoundedVec::<u8, ConstU32<32>>::force_from(
b"PARA".to_vec(),
None,
)),
),
);
let para_asset_type = AssetType::Xcm(para_asset_location);
AssetRegistry::register_asset(Origin::root(), PARA, para_asset_type.clone()).unwrap();
AssetRegistry::update_asset_units_per_second(
Origin::root(),
para_asset_type,
PARA_WEIGHT_PER_SEC,
)
.unwrap();
let dot_asset_location = MultiLocation::parent();
let dot_asset_type = AssetType::Xcm(dot_asset_location);
AssetRegistry::register_asset(Origin::root(), DOT, dot_asset_type.clone()).unwrap();
AssetRegistry::update_asset_units_per_second(
Origin::root(),
dot_asset_type,
DOT_WEIGHT_PER_SEC,
)
.unwrap();
let clv_asset_location = MultiLocation::new(
1,
X2(
Expand Down Expand Up @@ -538,4 +558,94 @@ impl ExtBuilder {
});
ext
}

pub fn karura_build(self) -> sp_io::TestExternalities {
use heiko_runtime::{AssetRegistry, Origin, System};
let mut ext = sp_io::TestExternalities::new(self.init());
ext.execute_with(|| {
System::set_block_number(1);
let hko_asset_location = MultiLocation::new(
0,
X1(GeneralKey(WeakBoundedVec::<u8, ConstU32<32>>::force_from(
b"HKO".to_vec(),
None,
))),
);
let hko_asset_type = AssetType::Xcm(hko_asset_location);
AssetRegistry::register_asset(Origin::root(), HKO, hko_asset_type.clone()).unwrap();
AssetRegistry::update_asset_units_per_second(
Origin::root(),
hko_asset_type,
HKO_WEIGHT_PER_SEC,
)
.unwrap();
});
ext
}

pub fn clv_build(self) -> sp_io::TestExternalities {
use heiko_runtime::{AssetRegistry, Origin, System};
let mut ext = sp_io::TestExternalities::new(self.init());
ext.execute_with(|| {
System::set_block_number(1);
let para_asset_location = MultiLocation::new(
0,
X1(GeneralKey(WeakBoundedVec::<u8, ConstU32<32>>::force_from(
b"PARA".to_vec(),
None,
))),
);
let para_asset_type = AssetType::Xcm(para_asset_location);
AssetRegistry::register_asset(Origin::root(), PARA, para_asset_type.clone()).unwrap();
AssetRegistry::update_asset_units_per_second(
Origin::root(),
para_asset_type,
PARA_WEIGHT_PER_SEC,
)
.unwrap();
});
ext
}

fn init(self) -> sp_runtime::Storage {
use heiko_runtime::Runtime;
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();

<parachain_info::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&parachain_info::GenesisConfig {
parachain_id: self.parachain_id.into(),
},
&mut t,
)
.unwrap();

<pallet_xcm::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_xcm::GenesisConfig {
safe_xcm_version: Some(2),
},
&mut t,
)
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(AccountId::from(ALICE), heiko(100)),
(AccountId::from(BOB), heiko(100)),
],
}
.assimilate_storage(&mut t)
.unwrap();

<pallet_liquid_staking::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_liquid_staking::GenesisConfig {
exchange_rate: Rate::one(),
reserve_factor: Ratio::from_perthousand(5),
},
&mut t,
)
.unwrap();
t
}
}
49 changes: 49 additions & 0 deletions pallets/traits/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,52 @@ impl<
Ok(asset.clone().into())
}
}

pub struct CurrencyIdConvert<AssetIdInfoGetter>(PhantomData<AssetIdInfoGetter>);
impl<AssetIdInfoGetter: AssetTypeGetter<CurrencyId, AssetType>>
Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert<AssetIdInfoGetter>
{
fn convert(id: CurrencyId) -> Option<MultiLocation> {
let multi_location =
AsAssetType::<CurrencyId, AssetType, AssetIdInfoGetter>::reverse_ref(&id).ok();
log::trace!(
target: "xcm::convert",
"currency_id: {:?}, multi_location: {:?}",
id,
multi_location,
);
multi_location
}
}

impl<AssetIdInfoGetter: AssetTypeGetter<CurrencyId, AssetType>>
Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert<AssetIdInfoGetter>
{
fn convert(location: MultiLocation) -> Option<CurrencyId> {
let currency_id =
AsAssetType::<CurrencyId, AssetType, AssetIdInfoGetter>::convert_ref(&location).ok();
log::trace!(
target: "xcm::convert",
"multi_location: {:?}. currency_id: {:?}",
location,
currency_id,
);
currency_id
}
}

impl<AssetIdInfoGetter: AssetTypeGetter<CurrencyId, AssetType>>
Convert<MultiAsset, Option<CurrencyId>> for CurrencyIdConvert<AssetIdInfoGetter>
{
fn convert(a: MultiAsset) -> Option<CurrencyId> {
if let MultiAsset {
id: xcmAssetId::Concrete(id),
fun: _,
} = a
{
Self::convert(id)
} else {
None
}
}
}
1 change: 1 addition & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use sp_runtime::{
use sp_std::prelude::*;

pub mod network;
pub mod paras;
pub mod tokens;
pub use tokens::*;

Expand Down
22 changes: 22 additions & 0 deletions primitives/src/paras.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 Parallel Finance Developer.
// This file is part of Parallel Finance.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod statemint {
pub const ID: u32 = 1000;
}

pub mod statemine {
pub const ID: u32 = 1000;
pub const PALLET_INSTANCE: u8 = 50;
}
Loading