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

relay account and tests #651

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
20 changes: 10 additions & 10 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,22 @@ impl<Network: Get<NetworkId>, AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone
let id = match location {
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
MultiLocation {
parents: 1,
interior: X1(AccountId32 {
id,
network: NetworkId::Any,
}),
} => id,
interior: X1(AccountId32 { id, network }),
} if network == Network::get() => id,
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
_ => return Err(location),
};
Ok(id.into())
}

fn reverse(who: AccountId) -> Result<MultiLocation, AccountId> {
Ok(AccountId32 {
id: who.into(),
network: Network::get(),
}
.into())
Ok((
1,
AccountId32 {
id: who.into(),
network: Network::get(),
},
)
.into())
}
}

Expand Down
49 changes: 47 additions & 2 deletions xtokens/src/mock/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use super::para::AccountIdToMultiLocation;
use super::*;
use orml_traits::MultiCurrency;
use xcm_builder::IsConcrete;
use xcm_executor::traits::MatchesFungible;
use xcm_executor::traits::{MatchesFungible, WeightTrader};
use xcm_simulator::TestExt;

use crate::mock::para::RelayLocation;
use crate::mock::relay::KsmLocation;
use xcm_executor::Assets;

#[test]
fn test_init_balance() {
Expand Down Expand Up @@ -120,7 +121,7 @@ fn test_parachain_convert_location_to_account() {
assert_eq!(account, Ok(sibling_a_account()));

let alice = Junction::AccountId32 {
network: NetworkId::Any,
network: NetworkId::Kusama,
id: ALICE.into(),
};

Expand Down Expand Up @@ -180,6 +181,10 @@ fn test_parachain_convert_origin() {
use xcm_executor::traits::ConvertOrigin;

let alice = Junction::AccountId32 {
network: NetworkId::Kusama,
id: ALICE.into(),
};
let alice_any = Junction::AccountId32 {
network: NetworkId::Any,
id: ALICE.into(),
};
Expand All @@ -202,6 +207,8 @@ fn test_parachain_convert_origin() {

// unsupported destination convert with OriginKind::SovereignAccount
let unsupported_sovereign_account_destination: Vec<MultiLocation> = vec![
// network not matched can't be kind of sovereign account
(Parent, alice_any.clone()).into(),
// sibling parachain's account can't be kind of sovereign account
(Parent, Parachain(1), alice.clone()).into(),
// relaychain's account with unmatched network can't be kind of sovereign account
Expand Down Expand Up @@ -264,6 +271,18 @@ fn test_call_weight_info() {
let call = Call::Balances(pallet_balances::Call::<Runtime>::transfer { dest: BOB, value: 100 });
let weight = call.get_dispatch_info().weight;
assert_eq!(weight, 195952000);

let call_para = Call::Balances(pallet_balances::Call::<Runtime>::transfer { dest: BOB, value: 100 });
let call_relay = relay::Call::XcmPallet(pallet_xcm::Call::<relay::Runtime>::send {
dest: Box::new(VersionedMultiLocation::V1(Parachain(2).into())),
message: Box::new(VersionedXcm::from(Xcm(vec![Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: 1000,
call: call_para.encode().into(),
}]))),
});
let weight = call_relay.get_dispatch_info().weight;
assert_eq!(weight, 100000000);
}

#[test]
Expand Down Expand Up @@ -313,3 +332,29 @@ fn test_parachain_weigher_calculate() {
let xcm_weight = <XcmConfig as xcm_executor::Config>::Weigher::weight(&mut Xcm(instructions));
assert_eq!(xcm_weight.unwrap(), expect_weight + 40);
}

#[test]
fn test_trader() {
use para::XcmConfig;

let asset: MultiAsset = (Parent, 1000).into();

let mut holding = Assets::new();
holding.subsume(asset.clone());

let backup = holding.clone();

let fees: MultiAsset = (Parent, 1000).into();
let max_fee = holding.try_take(fees.into()).unwrap();

assert_eq!(holding.is_empty(), true);
assert_eq!(max_fee, backup);

let mut trader = para::AllTokensAreCreatedEqualToWeight::new();
let result = <XcmConfig as xcm_executor::Config>::Trader::buy_weight(&mut trader, 1000, max_fee.clone());
assert_eq!(result.is_ok(), true);
assert_eq!(result.unwrap().is_empty(), true);

let result = <XcmConfig as xcm_executor::Config>::Trader::buy_weight(&mut trader, 2000, max_fee);
assert_eq!(result.is_err(), true);
}
64 changes: 29 additions & 35 deletions xtokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn relay_transact_to_para_remark_use_normal_account() {
});

let alice = Junctions::X1(Junction::AccountId32 {
network: NetworkId::Any,
network: NetworkId::Kusama,
id: ALICE.into(),
});
relaychain_transact_to_parachain_remark(alice.clone(), 6040);
Expand All @@ -457,26 +457,19 @@ fn relay_transact_to_para_remark_use_normal_account() {
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
System::reset_events();
});

relaychain_transact_to_parachain_remark(alice.clone(), 100);

ParaA::execute_with(|| {
use para::{Event, System};
assert_eq!(900, ParaTokens::free_balance(CurrencyId::R, &ALICE));
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
});

relaychain_transact_to_parachain_remark(alice, 1000);

ParaA::execute_with(|| {
use para::{Event, System};
assert_eq!(900, ParaTokens::free_balance(CurrencyId::R, &ALICE));
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))));
assert_eq!(
System::events()
.iter()
.find(|r| matches!(r.event, Event::System(frame_system::Event::Remarked(_, _)))),
None
);
});
}

Expand All @@ -490,37 +483,35 @@ fn relay_transact_to_para_transfer_use_normal_account() {
});

let alice = Junctions::X1(Junction::AccountId32 {
network: NetworkId::Any,
network: NetworkId::Kusama,
id: ALICE.into(),
});
relaychain_transact_to_parachain_transfer(alice.clone(), 195952040);
relaychain_transact_to_parachain_transfer(alice.clone(), 195952040, 500);

ParaA::execute_with(|| {
use para::{Event, System};
assert_eq!(1000, ParaTokens::free_balance(CurrencyId::R, &ALICE));
assert_eq!(500, ParaBalances::free_balance(&ALICE));
assert_eq!(500, ParaBalances::free_balance(&BOB));
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::Balances(pallet_balances::Event::Transfer(_, _, _)))));
System::reset_events();
});

relaychain_transact_to_parachain_transfer(alice.clone(), 100);

ParaA::execute_with(|| {
use para::{Event, System};
assert_eq!(900, ParaBalances::free_balance(&ALICE));
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::Balances(pallet_balances::Event::Transfer(_, _, _)))));
});

relaychain_transact_to_parachain_transfer(alice, 1000);
relaychain_transact_to_parachain_transfer(alice.clone(), 100, 100);

ParaA::execute_with(|| {
use para::{Event, System};
assert_eq!(900, ParaBalances::free_balance(&ALICE));
assert!(System::events()
.iter()
.any(|r| matches!(r.event, Event::Balances(pallet_balances::Event::Transfer(_, _, _)))));
assert_eq!(900, ParaTokens::free_balance(CurrencyId::R, &ALICE));
assert_eq!(500, ParaBalances::free_balance(&ALICE));
assert_eq!(500, ParaBalances::free_balance(&BOB));
assert_eq!(
System::events()
.iter()
.find(|r| matches!(r.event, Event::Balances(pallet_balances::Event::Transfer(_, _, _)))),
None
);
});
}

Expand Down Expand Up @@ -565,7 +556,7 @@ fn para_transact_to_sibling_remark_use_account_failed() {
}

#[test]
fn relay_transact_to_para_use_wrong_kind() {
fn relay_transact_to_para_unsupport_kind_failed() {
ParaA::execute_with(|| {
assert_ok!(ParaTokens::deposit(CurrencyId::R, &DEFAULT, 6040));
});
Expand Down Expand Up @@ -632,9 +623,12 @@ fn relaychain_transact_to_parachain_remark(junctions: Junctions, amount: u128) {
});
}

fn relaychain_transact_to_parachain_transfer(junctions: Junctions, amount: u128) {
fn relaychain_transact_to_parachain_transfer(junctions: Junctions, amount: u128, transfer_amount: u128) {
use para::{Call, Runtime};
let call = Call::Balances(pallet_balances::Call::<Runtime>::transfer { dest: BOB, value: 100 });
let call = Call::Balances(pallet_balances::Call::<Runtime>::transfer {
dest: BOB,
value: transfer_amount,
});
let assets: MultiAsset = (Parent, amount).into();

let limit: u64 = match junctions {
Expand Down