|
| 1 | +use xcm::v0::{ |
| 2 | + Junction::{self, *}, |
| 3 | + MultiAsset, MultiLocation, |
| 4 | +}; |
| 5 | + |
| 6 | +pub trait Parse { |
| 7 | + /// Returns the "chain" location part. It could be parent, sibling |
| 8 | + /// parachain, or child parachain. |
| 9 | + fn chain_part(&self) -> Option<MultiLocation>; |
| 10 | + /// Returns "non-chain" location part. |
| 11 | + fn non_chain_part(&self) -> Option<MultiLocation>; |
| 12 | +} |
| 13 | + |
| 14 | +fn is_chain_junction(junction: Option<&Junction>) -> bool { |
| 15 | + matches!(junction, Some(Parent) | Some(Parachain { id: _ })) |
| 16 | +} |
| 17 | + |
| 18 | +impl Parse for MultiLocation { |
| 19 | + fn chain_part(&self) -> Option<MultiLocation> { |
| 20 | + match (self.first(), self.at(1)) { |
| 21 | + (Some(Parent), Some(Parachain { id })) => Some((Parent, Parachain { id: *id }).into()), |
| 22 | + (Some(Parent), _) => Some(Parent.into()), |
| 23 | + (Some(Parachain { id }), _) => Some(Parachain { id: *id }.into()), |
| 24 | + _ => None, |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + fn non_chain_part(&self) -> Option<MultiLocation> { |
| 29 | + let mut location = self.clone(); |
| 30 | + while is_chain_junction(location.first()) { |
| 31 | + let _ = location.take_first(); |
| 32 | + } |
| 33 | + |
| 34 | + if location != MultiLocation::Null { |
| 35 | + Some(location) |
| 36 | + } else { |
| 37 | + None |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub trait Reserve { |
| 43 | + /// Returns assets reserve location. |
| 44 | + fn reserve(&self) -> Option<MultiLocation>; |
| 45 | +} |
| 46 | + |
| 47 | +impl Reserve for MultiAsset { |
| 48 | + fn reserve(&self) -> Option<MultiLocation> { |
| 49 | + if let MultiAsset::ConcreteFungible { id, .. } = self { |
| 50 | + id.chain_part() |
| 51 | + } else { |
| 52 | + None |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use super::*; |
| 60 | + |
| 61 | + const PARACHAIN: Junction = Parachain { id: 1 }; |
| 62 | + const GENERAL_INDEX: Junction = GeneralIndex { id: 1 }; |
| 63 | + |
| 64 | + fn concrete_fungible(id: MultiLocation) -> MultiAsset { |
| 65 | + MultiAsset::ConcreteFungible { id, amount: 1 } |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn parent_as_reserve_chain() { |
| 70 | + assert_eq!( |
| 71 | + concrete_fungible(MultiLocation::X2(Parent, GENERAL_INDEX)).reserve(), |
| 72 | + Some(Parent.into()) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn sibling_parachain_as_reserve_chain() { |
| 78 | + assert_eq!( |
| 79 | + concrete_fungible(MultiLocation::X3(Parent, PARACHAIN, GENERAL_INDEX)).reserve(), |
| 80 | + Some((Parent, PARACHAIN).into()) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn child_parachain_as_reserve_chain() { |
| 86 | + assert_eq!( |
| 87 | + concrete_fungible(MultiLocation::X2(PARACHAIN, GENERAL_INDEX)).reserve(), |
| 88 | + Some(PARACHAIN.into()) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn no_reserve_chain() { |
| 94 | + assert_eq!( |
| 95 | + concrete_fungible(MultiLocation::X1(GeneralKey("DOT".into()))).reserve(), |
| 96 | + None |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn non_chain_part_works() { |
| 102 | + assert_eq!(MultiLocation::X1(Parent).non_chain_part(), None); |
| 103 | + assert_eq!(MultiLocation::X2(Parent, PARACHAIN).non_chain_part(), None); |
| 104 | + assert_eq!(MultiLocation::X1(PARACHAIN).non_chain_part(), None); |
| 105 | + |
| 106 | + assert_eq!( |
| 107 | + MultiLocation::X2(Parent, GENERAL_INDEX).non_chain_part(), |
| 108 | + Some(GENERAL_INDEX.into()) |
| 109 | + ); |
| 110 | + assert_eq!( |
| 111 | + MultiLocation::X3(Parent, GENERAL_INDEX, GENERAL_INDEX).non_chain_part(), |
| 112 | + Some((GENERAL_INDEX, GENERAL_INDEX).into()) |
| 113 | + ); |
| 114 | + assert_eq!( |
| 115 | + MultiLocation::X3(Parent, PARACHAIN, GENERAL_INDEX).non_chain_part(), |
| 116 | + Some(GENERAL_INDEX.into()) |
| 117 | + ); |
| 118 | + assert_eq!( |
| 119 | + MultiLocation::X2(PARACHAIN, GENERAL_INDEX).non_chain_part(), |
| 120 | + Some(GENERAL_INDEX.into()) |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
0 commit comments