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
39mod fee_trader;
40pub use fee_trader::*;
41
42use xcm::latest::{Junction, Junctions, Location};
43
44pub fn split_location_into_chain_part_and_beneficiary(
45	mut location: Location,
46) -> Option<(Location, Location)> {
47	let mut beneficiary_junctions = Junctions::Here;
48
49	// start popping junctions until we reach chain identifier
50	while let Some(j) = location.last() {
51		if matches!(j, Junction::Parachain(_) | Junction::GlobalConsensus(_)) {
52			// return chain subsection
53			return Some((location, beneficiary_junctions.into_location()));
54		} else {
55			let (location_prefix, maybe_last_junction) = location.split_last_interior();
56			location = location_prefix;
57			if let Some(junction) = maybe_last_junction {
58				beneficiary_junctions.push(junction).ok()?;
59			}
60		}
61	}
62
63	if location.parent_count() == 1 {
64		Some((Location::parent(), beneficiary_junctions.into_location()))
65	} else {
66		None
67	}
68}