From ca7b903bc2b4f153148d0d7d7a07c3e4d663e153 Mon Sep 17 00:00:00 2001 From: Shaun Wang Date: Wed, 17 Mar 2021 16:32:36 +1300 Subject: [PATCH] Add unit tests for xcm-support. --- xcm-support/src/lib.rs | 3 + xcm-support/src/tests.rs | 145 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 xcm-support/src/tests.rs diff --git a/xcm-support/src/lib.rs b/xcm-support/src/lib.rs index 890744d97..12ff94f4c 100644 --- a/xcm-support/src/lib.rs +++ b/xcm-support/src/lib.rs @@ -16,6 +16,9 @@ pub use currency_adapter::MultiCurrencyAdapter; mod currency_adapter; +#[cfg(test)] +mod tests; + pub trait XcmHandler { fn execute_xcm(origin: AccountId, xcm: Xcm) -> DispatchResult; } diff --git a/xcm-support/src/tests.rs b/xcm-support/src/tests.rs new file mode 100644 index 000000000..eb2bb777f --- /dev/null +++ b/xcm-support/src/tests.rs @@ -0,0 +1,145 @@ +//! Unit tests for xcm-support implementations. + +use super::*; + +use frame_support::parameter_types; +use sp_runtime::traits::{Convert, Identity}; + +#[derive(Debug, PartialEq, Eq)] +pub enum TestCurrencyId { + TokenA, + TokenB, + RelayChainToken, +} +impl TryFrom> for TestCurrencyId { + type Error = (); + fn try_from(v: Vec) -> Result { + match v.as_slice() { + [1] => Ok(TestCurrencyId::TokenA), + [2] => Ok(TestCurrencyId::TokenB), + [3] => Ok(TestCurrencyId::RelayChainToken), + _ => Err(()), + } + } +} + +type IdentityMatch = IsConcreteWithGeneralKey; + +pub struct NativeToRelay; +impl Convert for NativeToRelay { + fn convert(val: u128) -> u128 { + // native is 13 + // relay is 12 + val / 10 + } +} + +type TenToOneMatch = IsConcreteWithGeneralKey; + +parameter_types! { + pub NativeOrmlTokens: BTreeSet<(Vec, MultiLocation)> = { + let mut t = BTreeSet::new(); + t.insert((vec![1], (Junction::Parent, Junction::Parachain { id: 1 }).into())); + t + }; + + pub const RelayChainCurrencyId: TestCurrencyId = TestCurrencyId::RelayChainToken; +} + +type AssetFilter = NativePalletAssetOr; + +type TestCurrencyIdConverter = CurrencyIdConverter; + +#[test] +fn is_concrete_with_general_key_matches_relay_chain_token() { + let relay_chain_asset = MultiAsset::ConcreteFungible { + id: MultiLocation::X1(Junction::Parent), + amount: 10, + }; + assert_eq!(IdentityMatch::matches_fungible(&relay_chain_asset), Some(10)); + assert_eq!(TenToOneMatch::matches_fungible(&relay_chain_asset), Some(1)); +} + +#[test] +fn is_concrete_with_general_key_matches_parachain_token_with_general_key() { + let token_a = MultiAsset::ConcreteFungible { + id: MultiLocation::X3( + Junction::Parent, + Junction::Parachain { id: 1 }, + Junction::GeneralKey(vec![1]), + ), + amount: 10, + }; + let unknown_token = MultiAsset::ConcreteFungible { + id: MultiLocation::X3( + Junction::Parent, + Junction::Parachain { id: 1 }, + Junction::GeneralKey(vec![100]), + ), + amount: 10, + }; + assert_eq!(IdentityMatch::matches_fungible(&token_a), Some(10)); + assert_eq!( + >::matches_fungible(&unknown_token), + None, + ); +} + +#[test] +fn native_pallet_asset_or_can_filter_native_asset() { + let token_a = MultiAsset::ConcreteFungible { + id: MultiLocation::X2(Junction::Parent, Junction::Parachain { id: 1 }), + amount: 10, + }; + assert!(AssetFilter::filter_asset_location( + &token_a, + &MultiLocation::X2(Junction::Parent, Junction::Parachain { id: 1 }), + )); +} + +#[test] +fn native_pallet_asset_or_can_filter_orml_tokens() { + let token_a = MultiAsset::ConcreteFungible { + id: MultiLocation::X3( + Junction::Parent, + Junction::Parachain { id: 1 }, + Junction::GeneralKey(vec![1]), + ), + amount: 10, + }; + // origin is different from concrete fungible id, thus it's not native. + assert!(AssetFilter::filter_asset_location( + &token_a, + &MultiLocation::X2(Junction::Parent, Junction::Parachain { id: 1 }), + )); +} + +#[test] +fn currency_id_converts_relay_chain_token() { + let relay_chain_asset = MultiAsset::ConcreteFungible { + id: MultiLocation::X1(Junction::Parent), + amount: 10, + }; + + assert_eq!( + TestCurrencyIdConverter::from_asset(&relay_chain_asset), + Some(TestCurrencyId::RelayChainToken), + ); +} + +#[test] +fn currency_id_converts_parachain_token() { + let token_a = MultiAsset::ConcreteFungible { + id: MultiLocation::X3( + Junction::Parent, + Junction::Parachain { id: 1 }, + Junction::GeneralKey(vec![1]), + ), + amount: 10, + }; + + assert_eq!( + TestCurrencyIdConverter::from_asset(&token_a), + Some(TestCurrencyId::TokenA), + ); +}