xcm_primitives/
lib.rs

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
21mod asset_id_conversions;
22pub use asset_id_conversions::*;
23
24mod constants;
25pub use constants::*;
26
27mod ethereum_xcm;
28pub use ethereum_xcm::*;
29
30mod filter_asset_max_fee;
31pub use filter_asset_max_fee::*;
32
33mod origin_conversion;
34pub use origin_conversion::*;
35
36mod transactor_traits;
37pub use transactor_traits::*;
38
39use xcm::latest::{Junction, Junctions, Location};
40
41pub fn split_location_into_chain_part_and_beneficiary(
42	mut location: Location,
43) -> Option<(Location, Location)> {
44	let mut beneficiary_junctions = Junctions::Here;
45
46	// start popping junctions until we reach chain identifier
47	while let Some(j) = location.last() {
48		if matches!(j, Junction::Parachain(_) | Junction::GlobalConsensus(_)) {
49			// return chain subsection
50			return Some((location, beneficiary_junctions.into_location()));
51		} else {
52			let (location_prefix, maybe_last_junction) = location.split_last_interior();
53			location = location_prefix;
54			if let Some(junction) = maybe_last_junction {
55				beneficiary_junctions.push(junction).ok()?;
56			}
57		}
58	}
59
60	if location.parent_count() == 1 {
61		Some((Location::parent(), beneficiary_junctions.into_location()))
62	} else {
63		None
64	}
65}