Skip to content

Commit

Permalink
Merge pull request #138 from web3labs/xtokens-integration-dev
Browse files Browse the repository at this point in the history
fix sibling xfer test compilation: use KSM instead of KAR
  • Loading branch information
mohamedelshami authored Aug 24, 2022
2 parents fd96622 + 0f5ffda commit 5a303d1
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 32 deletions.
123 changes: 123 additions & 0 deletions runtime/integration-tests/src/kusama_cross_chain_xfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use arctic_runtime::{
AccountId, CurrencyId, Origin, RelayCurrencyId, TokenSymbol, Tokens, XTokens,
};
use frame_support::assert_ok;
use orml_traits::MultiCurrency;
// use xcm::{VersionedMultiLocation, v0::{MultiLocation::X1, Junction::{Parachain, self}}, VersionedMultiAssets};
use xcm::{latest::prelude::*, VersionedMultiAssets, VersionedMultiLocation};
use xcm_emulator::{Junctions::Here, NetworkId, TestExt};

use crate::{
dollar,
kusama_testnet::{Arctic, KusamaNet, Sibling, TestNet},
ALICE, BOB,
};

#[test]
fn transfer_from_relay_chain() {
KusamaNet::execute_with(|| {
assert_ok!(kusama_runtime::XcmPallet::reserve_transfer_assets(
kusama_runtime::Origin::signed(ALICE.into()),
Box::new(VersionedMultiLocation::V1(X1(Parachain(2001)).into())),
Box::new(VersionedMultiLocation::V1(
X1(Junction::AccountId32 {
id: BOB,
network: NetworkId::Any
})
.into()
)),
Box::new(VersionedMultiAssets::V1(
(Here, dollar(RelayCurrencyId::get())).into()
)),
0,
));
});

Arctic::execute_with(|| {
assert_eq!(
Tokens::free_balance(RelayCurrencyId::get(), &AccountId::from(BOB)),
999936000000
);
});
}

#[test]
fn transfer_to_relay_chain() {
Arctic::execute_with(|| {
assert_ok!(XTokens::transfer(
Origin::signed(ALICE.into()),
RelayCurrencyId::get(),
dollar(RelayCurrencyId::get()),
Box::new(xcm::VersionedMultiLocation::V1(MultiLocation::new(
1,
X1(Junction::AccountId32 {
id: BOB,
network: NetworkId::Any
})
))),
4_000_000_000
));
});

KusamaNet::execute_with(|| {
assert_eq!(
kusama_runtime::Balances::free_balance(&AccountId::from(BOB)),
999_834_059_328
);
});
}

#[test]
fn transfer_to_sibling() {
env_logger::init();

TestNet::reset();

fn arctic_reserve_account() -> AccountId {
use sp_runtime::traits::AccountIdConversion;
polkadot_parachain::primitives::Sibling::from(2001).into_account_truncating()
}

Arctic::execute_with(|| {
assert_ok!(Tokens::deposit(
CurrencyId::Token(TokenSymbol::ICZ),
&AccountId::from(ALICE),
100_000_000_000_000
));
});

Sibling::execute_with(|| {
assert_ok!(Tokens::deposit(
CurrencyId::Token(TokenSymbol::ICZ),
&arctic_reserve_account(),
100_000_000_000_000
));
});

Arctic::execute_with(|| {
assert_ok!(XTokens::transfer(
Origin::signed(ALICE.into()),
CurrencyId::Token(TokenSymbol::ICZ),
10_000_000_000_000,
Box::new(
MultiLocation::new(
1,
X2(
Parachain(2000),
Junction::AccountId32 {
network: NetworkId::Any,
id: BOB.into()
}
)
)
.into()
),
1_000_000_000,
));

assert_eq!(
Tokens::free_balance(CurrencyId::Token(TokenSymbol::ICZ), &AccountId::from(ALICE)),
90_000_000_000_000
);
});
}
225 changes: 197 additions & 28 deletions runtime/integration-tests/src/kusama_testnet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use arctic_runtime::{
AccountId, Balance, CurrencyId, ExistentialDeposit, NativeCurrencyId, Origin, RelayCurrencyId,
Runtime, System, TokenSymbol,
};
use cumulus_primitives_core::ParaId;
use frame_support::traits::GenesisBuild;
use polkadot_primitives::v2::{BlockNumber, MAX_CODE_SIZE, MAX_POV_SIZE};
use polkadot_runtime_parachains::configuration::HostConfiguration;
use sp_runtime::traits::AccountIdConversion;
use xcm_emulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};

use crate::{dollar, get_all_module_accounts, ALICE};

decl_test_relay_chain! {
pub struct KusamaNet {
Runtime = kusama_runtime::Runtime,
Expand All @@ -12,17 +23,7 @@ decl_test_parachain! {
pub struct Arctic {
Runtime = Runtime,
Origin = Origin,
XcmpMessageHandler = arctic_runtime::XcmpQueue,
DmpMessageHandler = arctic_runtime::DmpQueue,
new_ext = para_ext(2000),
}
}

decl_test_parachain! {
pub struct MockBifrost {
Runtime = Runtime,
Origin = Origin,
XcmpMessageHandler = arctic_runtime::XcmpQueue,
XcmpMessageHandler = arctic_runtime ::XcmpQueue,
DmpMessageHandler = arctic_runtime::DmpQueue,
new_ext = para_ext(2001),
}
Expand All @@ -32,30 +33,198 @@ decl_test_parachain! {
pub struct Sibling {
Runtime = Runtime,
Origin = Origin,
XcmpMessageHandler = arctic_runtime::XcmpQueue,
XcmpMessageHandler = arctic_runtime ::XcmpQueue,
DmpMessageHandler = arctic_runtime::DmpQueue,
new_ext = para_ext(2002),
}
}

decl_test_parachain! {
pub struct Statemine {
Runtime = statemine_runtime::Runtime,
Origin = statemine_runtime::Origin,
XcmpMessageHandler = statemine_runtime::XcmpQueue,
DmpMessageHandler = statemine_runtime::DmpQueue,
new_ext = para_ext(1000),
new_ext = para_ext(2000),
}
}

decl_test_network! {
pub struct TestNet {
relay_chain = KusamaNet,
parachains = vec![
(1000, Statemine),
(2000, Karura),
(2001, MockBifrost),
(2002, Sibling),
(2001, Arctic),
(2000, Sibling),
],
}
}

fn default_parachains_host_configuration() -> HostConfiguration<BlockNumber> {
HostConfiguration {
minimum_validation_upgrade_delay: 5,
validation_upgrade_cooldown: 5u32,
validation_upgrade_delay: 5,
code_retention_period: 1200,
max_code_size: MAX_CODE_SIZE,
max_pov_size: MAX_POV_SIZE,
max_head_data_size: 32 * 1024,
group_rotation_frequency: 20,
chain_availability_period: 4,
thread_availability_period: 4,
max_upward_queue_count: 8,
max_upward_queue_size: 1024 * 1024,
max_downward_message_size: 1024,
ump_service_total_weight: 4 * 1_000_000_000,
max_upward_message_size: 1024 * 50,
max_upward_message_num_per_candidate: 5,
hrmp_sender_deposit: 0,
hrmp_recipient_deposit: 0,
hrmp_channel_max_capacity: 8,
hrmp_channel_max_total_size: 8 * 1024,
hrmp_max_parachain_inbound_channels: 4,
hrmp_max_parathread_inbound_channels: 4,
hrmp_channel_max_message_size: 1024 * 1024,
hrmp_max_parachain_outbound_channels: 4,
hrmp_max_parathread_outbound_channels: 4,
hrmp_max_message_num_per_candidate: 5,
dispute_period: 6,
no_show_slots: 2,
n_delay_tranches: 25,
needed_approvals: 2,
relay_vrf_modulo_samples: 2,
zeroth_delay_tranche_width: 0,
..Default::default()
}
}

pub fn kusama_ext() -> sp_io::TestExternalities {
use kusama_runtime::{Runtime, System};

let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(
AccountId::from(ALICE),
2002 * dollar(CurrencyId::Token(TokenSymbol::KSM)),
),
(
ParaId::from(2001).into_account_truncating(),
2 * dollar(CurrencyId::Token(TokenSymbol::KSM)),
),
],
}
}
.assimilate_storage(&mut t)
.unwrap();

polkadot_runtime_parachains::configuration::GenesisConfig::<Runtime> {
config: default_parachains_host_configuration(),
}
.assimilate_storage(&mut t)
.unwrap();

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

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}

pub fn para_ext(parachain_id: u32) -> sp_io::TestExternalities {
ExtBuilder::default()
.balances(vec![(
AccountId::from(ALICE),
RelayCurrencyId::get(),
10 * dollar(RelayCurrencyId::get()),
)])
.parachain_id(parachain_id)
.build()
}

pub struct ExtBuilder {
balances: Vec<(AccountId, CurrencyId, Balance)>,
parachain_id: u32,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {
balances: vec![],
parachain_id: 2001,
}
}
}

impl ExtBuilder {
pub fn balances(mut self, balances: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
self.balances = balances;
self
}

#[allow(dead_code)]
pub fn parachain_id(mut self, parachain_id: u32) -> Self {
self.parachain_id = parachain_id;
self
}

pub fn build(self) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();

let native_currency_id = NativeCurrencyId::get();
let existential_deposit = ExistentialDeposit::get();

pallet_balances::GenesisConfig::<Runtime> {
balances: self
.balances
.clone()
.into_iter()
.filter(|(_, currency_id, _)| *currency_id == native_currency_id)
.map(|(account_id, _, initial_balance)| (account_id, initial_balance))
.chain(
get_all_module_accounts()
.iter()
.map(|x| (x.clone(), existential_deposit)),
)
.collect::<Vec<_>>(),
}
.assimilate_storage(&mut t)
.unwrap();

orml_tokens::GenesisConfig::<Runtime> {
balances: self
.balances
.into_iter()
.filter(|(_, currency_id, _)| *currency_id != native_currency_id)
.collect::<Vec<_>>(),
}
.assimilate_storage(&mut t)
.unwrap();

pallet_membership::GenesisConfig::<Runtime, pallet_membership::Instance1> {
members: vec![],
phantom: Default::default(),
}
.assimilate_storage(&mut t)
.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();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}
Loading

0 comments on commit 5a303d1

Please sign in to comment.