referrerpolicy=no-referrer-when-downgrade

snowbridge_test_utils/
mock_origin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3
4// A stripped-down version of pallet-xcm that only inserts an XCM origin into the runtime
5#[frame_support::pallet]
6pub mod pallet_xcm_origin {
7	use codec::DecodeWithMemTracking;
8	use frame_support::{
9		pallet_prelude::*,
10		traits::{Contains, OriginTrait},
11	};
12	use xcm::latest::prelude::*;
13
14	#[pallet::pallet]
15	pub struct Pallet<T>(_);
16
17	#[pallet::config]
18	pub trait Config: frame_system::Config {
19		type RuntimeOrigin: From<Origin> + From<<Self as frame_system::Config>::RuntimeOrigin>;
20	}
21
22	// Insert this custom Origin into the aggregate RuntimeOrigin
23	#[pallet::origin]
24	#[derive(
25		PartialEq,
26		Eq,
27		Clone,
28		Encode,
29		Decode,
30		DecodeWithMemTracking,
31		RuntimeDebug,
32		TypeInfo,
33		MaxEncodedLen,
34	)]
35	pub struct Origin(pub Location);
36
37	impl From<Location> for Origin {
38		fn from(location: Location) -> Origin {
39			Origin(location)
40		}
41	}
42
43	/// `EnsureOrigin` implementation succeeding with a `Location` value to recognize and
44	/// filter the contained location
45	pub struct EnsureXcm<F>(PhantomData<F>);
46	impl<O: OriginTrait + From<Origin>, F: Contains<Location>> EnsureOrigin<O> for EnsureXcm<F>
47	where
48		O::PalletsOrigin: From<Origin> + TryInto<Origin, Error = O::PalletsOrigin>,
49	{
50		type Success = Location;
51
52		fn try_origin(outer: O) -> Result<Self::Success, O> {
53			outer.try_with_caller(|caller| {
54				caller.try_into().and_then(|o| match o {
55					Origin(location) if F::contains(&location) => Ok(location),
56					o => Err(o.into()),
57				})
58			})
59		}
60
61		#[cfg(feature = "runtime-benchmarks")]
62		fn try_successful_origin() -> Result<O, ()> {
63			Ok(O::from(Origin(Location::here().into())))
64		}
65	}
66}