1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! The XCM primitive trait implementations
18

            
19
#![cfg_attr(not(feature = "std"), no_std)]
20

            
21
mod asset_id_conversions;
22
pub use asset_id_conversions::*;
23

            
24
mod constants;
25
pub use constants::*;
26

            
27
mod ethereum_xcm;
28
pub use ethereum_xcm::*;
29

            
30
mod filter_asset_max_fee;
31
pub use filter_asset_max_fee::*;
32

            
33
mod origin_conversion;
34
pub use origin_conversion::*;
35

            
36
mod transactor_traits;
37
pub use transactor_traits::*;
38

            
39
use xcm::latest::{Junction, Junctions, Location};
40

            
41
2492
pub fn split_location_into_chain_part_and_beneficiary(
42
2492
	mut location: Location,
43
2492
) -> Option<(Location, Location)> {
44
2492
	let mut beneficiary_junctions = Junctions::Here;
45

            
46
	// start popping junctions until we reach chain identifier
47
4984
	while let Some(j) = location.last() {
48
3808
		if matches!(j, Junction::Parachain(_) | Junction::GlobalConsensus(_)) {
49
			// return chain subsection
50
1316
			return Some((location, beneficiary_junctions.into_location()));
51
		} else {
52
2492
			let (location_prefix, maybe_last_junction) = location.split_last_interior();
53
2492
			location = location_prefix;
54
2492
			if let Some(junction) = maybe_last_junction {
55
2492
				beneficiary_junctions.push(junction).ok()?;
56
			}
57
		}
58
	}
59

            
60
1176
	if location.parent_count() == 1 {
61
1176
		Some((Location::parent(), beneficiary_junctions.into_location()))
62
	} else {
63
		None
64
	}
65
2492
}