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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use sp_runtime::{traits::ConstU32, WeakBoundedVec};
use sp_std::prelude::*;
use xcm::latest::prelude::*;

pub trait Parse {
	/// Returns the "chain" location part. It could be parent, sibling
	/// parachain, or child parachain.
	fn chain_part(&self) -> Option<MultiLocation>;
	/// Returns "non-chain" location part.
	fn non_chain_part(&self) -> Option<MultiLocation>;
}

fn is_chain_junction(junction: Option<&Junction>) -> bool {
	matches!(junction, Some(Parachain(_)))
}

impl Parse for MultiLocation {
	fn chain_part(&self) -> Option<MultiLocation> {
		match (self.parents, self.first_interior()) {
			// sibling parachain
			(1, Some(Parachain(id))) => Some(MultiLocation::new(1, X1(Parachain(*id)))),
			// parent
			(1, _) => Some(MultiLocation::parent()),
			// children parachain
			(0, Some(Parachain(id))) => Some(MultiLocation::new(0, X1(Parachain(*id)))),
			_ => None,
		}
	}

	fn non_chain_part(&self) -> Option<MultiLocation> {
		let mut junctions = self.interior().clone();
		while is_chain_junction(junctions.first()) {
			let _ = junctions.take_first();
		}

		if junctions != Here {
			Some(MultiLocation::new(0, junctions))
		} else {
			None
		}
	}
}

pub trait Reserve {
	/// Returns assets reserve location.
	fn reserve(asset: &MultiAsset) -> Option<MultiLocation>;
}

// Provide reserve in absolute path view
pub struct AbsoluteReserveProvider;

impl Reserve for AbsoluteReserveProvider {
	fn reserve(asset: &MultiAsset) -> Option<MultiLocation> {
		if let Concrete(location) = &asset.id {
			location.chain_part()
		} else {
			None
		}
	}
}

// Provide reserve in relative path view
// Self tokens are represeneted as Here
pub struct RelativeReserveProvider;

impl Reserve for RelativeReserveProvider {
	fn reserve(asset: &MultiAsset) -> Option<MultiLocation> {
		if let Concrete(location) = &asset.id {
			if location.parents == 0 && !is_chain_junction(location.first_interior()) {
				Some(MultiLocation::here())
			} else {
				location.chain_part()
			}
		} else {
			None
		}
	}
}

pub trait RelativeLocations {
	fn sibling_parachain_general_key(para_id: u32, general_key: WeakBoundedVec<u8, ConstU32<32>>) -> MultiLocation;
}

impl RelativeLocations for MultiLocation {
	fn sibling_parachain_general_key(para_id: u32, general_key: WeakBoundedVec<u8, ConstU32<32>>) -> MultiLocation {
		MultiLocation::new(1, X2(Parachain(para_id), GeneralKey(general_key)))
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	const PARACHAIN: Junction = Parachain(1);
	const GENERAL_INDEX: Junction = GeneralIndex(1);

	fn concrete_fungible(id: MultiLocation) -> MultiAsset {
		(id, 1).into()
	}

	#[test]
	fn parent_as_reserve_chain() {
		assert_eq!(
			AbsoluteReserveProvider::reserve(&concrete_fungible(MultiLocation::new(1, X1(GENERAL_INDEX)))),
			Some(MultiLocation::parent())
		);
		assert_eq!(
			RelativeReserveProvider::reserve(&concrete_fungible(MultiLocation::new(1, X1(GENERAL_INDEX)))),
			Some(MultiLocation::parent())
		);
	}

	#[test]
	fn sibling_parachain_as_reserve_chain() {
		assert_eq!(
			AbsoluteReserveProvider::reserve(&concrete_fungible(MultiLocation::new(1, X2(PARACHAIN, GENERAL_INDEX)))),
			Some(MultiLocation::new(1, X1(PARACHAIN)))
		);
		assert_eq!(
			RelativeReserveProvider::reserve(&concrete_fungible(MultiLocation::new(1, X2(PARACHAIN, GENERAL_INDEX)))),
			Some(MultiLocation::new(1, X1(PARACHAIN)))
		);
	}

	#[test]
	fn child_parachain_as_reserve_chain() {
		assert_eq!(
			AbsoluteReserveProvider::reserve(&concrete_fungible(MultiLocation::new(0, X2(PARACHAIN, GENERAL_INDEX)))),
			Some(PARACHAIN.into())
		);
		assert_eq!(
			RelativeReserveProvider::reserve(&concrete_fungible(MultiLocation::new(0, X2(PARACHAIN, GENERAL_INDEX)))),
			Some(PARACHAIN.into())
		);
	}

	#[test]
	fn no_reserve_chain_for_absolute_self_for_relative() {
		assert_eq!(
			AbsoluteReserveProvider::reserve(&concrete_fungible(MultiLocation::new(
				0,
				X1(GeneralKey(b"DOT".to_vec().try_into().unwrap()))
			))),
			None
		);
		assert_eq!(
			RelativeReserveProvider::reserve(&concrete_fungible(MultiLocation::new(
				0,
				X1(GeneralKey(b"DOT".to_vec().try_into().unwrap()))
			))),
			Some(MultiLocation::here())
		);
	}

	#[test]
	fn non_chain_part_works() {
		assert_eq!(MultiLocation::parent().non_chain_part(), None);
		assert_eq!(MultiLocation::new(1, X1(PARACHAIN)).non_chain_part(), None);
		assert_eq!(MultiLocation::new(0, X1(PARACHAIN)).non_chain_part(), None);

		assert_eq!(
			MultiLocation::new(1, X1(GENERAL_INDEX)).non_chain_part(),
			Some(GENERAL_INDEX.into())
		);
		assert_eq!(
			MultiLocation::new(1, X2(GENERAL_INDEX, GENERAL_INDEX)).non_chain_part(),
			Some((GENERAL_INDEX, GENERAL_INDEX).into())
		);
		assert_eq!(
			MultiLocation::new(1, X2(PARACHAIN, GENERAL_INDEX)).non_chain_part(),
			Some(GENERAL_INDEX.into())
		);
		assert_eq!(
			MultiLocation::new(0, X2(PARACHAIN, GENERAL_INDEX)).non_chain_part(),
			Some(GENERAL_INDEX.into())
		);
	}
}