-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathtests.rs
More file actions
149 lines (134 loc) · 3.62 KB
/
tests.rs
File metadata and controls
149 lines (134 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Unit tests for xcm-support implementations.
#![cfg(test)]
use super::*;
use frame_support::{pallet_prelude::Encode, parameter_types};
use orml_traits::{location::RelativeLocations, ConcreteFungibleAsset};
use sp_runtime::AccountId32;
#[derive(Debug, PartialEq, Eq)]
pub enum TestCurrencyId {
TokenA,
TokenB,
RelayChainToken,
}
pub struct CurrencyIdConvert;
impl Convert<MultiLocation, Option<TestCurrencyId>> for CurrencyIdConvert {
fn convert(l: MultiLocation) -> Option<TestCurrencyId> {
use TestCurrencyId::*;
let token_a: Vec<u8> = "TokenA".into();
let token_b: Vec<u8> = "TokenB".into();
if l == MultiLocation::parent() {
return Some(RelayChainToken);
}
if l == MultiLocation::sibling_parachain_general_key(1, token_a) {
return Some(TokenA);
}
if l == MultiLocation::sibling_parachain_general_key(2, token_b) {
return Some(TokenB);
}
None
}
}
type MatchesCurrencyId = IsNativeConcrete<TestCurrencyId, CurrencyIdConvert>;
#[test]
fn is_native_concrete_matches_native_currencies() {
assert_eq!(
MatchesCurrencyId::matches_fungible(&MultiAsset::parent_asset(100)),
Some(100),
);
assert_eq!(
MatchesCurrencyId::matches_fungible(&MultiAsset::sibling_parachain_asset(1, "TokenA".into(), 100)),
Some(100),
);
assert_eq!(
MatchesCurrencyId::matches_fungible(&MultiAsset::sibling_parachain_asset(2, "TokenB".into(), 100)),
Some(100),
);
}
#[test]
fn is_native_concrete_does_not_matches_non_native_currencies() {
assert!(
<MatchesCurrencyId as MatchesFungible<u128>>::matches_fungible(&MultiAsset::sibling_parachain_asset(
2,
"TokenC".into(),
100
))
.is_none()
);
assert!(
<MatchesCurrencyId as MatchesFungible<u128>>::matches_fungible(&MultiAsset::sibling_parachain_asset(
1,
"TokenB".into(),
100
))
.is_none()
);
assert!(
<MatchesCurrencyId as MatchesFungible<u128>>::matches_fungible(&MultiAsset {
fun: Fungible(100),
id: Concrete(MultiLocation::new(1, X1(GeneralKey("TokenB".into())))),
})
.is_none()
);
}
#[test]
fn multi_native_asset() {
assert!(MultiNativeAsset::filter_asset_location(
&MultiAsset {
fun: Fungible(10),
id: Concrete(MultiLocation::parent())
},
&Parent.into()
));
assert!(MultiNativeAsset::filter_asset_location(
&MultiAsset::sibling_parachain_asset(1, "TokenA".into(), 100),
&MultiLocation::new(1, X1(Parachain(1))),
));
assert!(!MultiNativeAsset::filter_asset_location(
&MultiAsset::sibling_parachain_asset(1, "TokenA".into(), 100),
&MultiLocation::parent(),
));
}
#[test]
fn relay_account_convert() {
use xcm_executor::traits::Convert;
parameter_types! {
const RelayNetwork: NetworkId = NetworkId::Any;
}
let destination: MultiLocation = (
Parent,
Junction::AccountId32 {
network: NetworkId::Any,
id: [0; 32],
},
)
.into();
let account: Result<AccountId32, MultiLocation> =
RelayChainAccountId32Aliases::<RelayNetwork, AccountId32>::convert(destination);
assert_eq!(account, Ok(AccountId32::new([0; 32])));
}
#[test]
fn allow_relayed_paid_execution_works() {
parameter_types! {
const RelayNetwork: NetworkId = NetworkId::Any;
}
let assets: MultiAsset = (Parent, 1000).into();
let mut xcm = Xcm::<()>(vec![
DescendOrigin(X1(Junction::AccountId32 {
network: NetworkId::Any,
id: [0; 32],
})),
WithdrawAsset(assets.clone().into()),
BuyExecution {
fees: assets,
weight_limit: Limited(1000),
},
Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: 1000 as u64,
call: Encode::encode(&100).into(),
},
]);
let r =
AllowRelayedPaidExecutionFromParent::<RelayNetwork>::should_execute(&(Parent.into()), &mut xcm, 100, &mut 100);
assert_eq!(r, Ok(()));
}