assets_common/
foreign_creators.rs1use core::fmt::Debug;
17use frame_support::traits::{
18 ContainsPair, EnsureOrigin, EnsureOriginWithArg, Everything, OriginTrait,
19};
20use pallet_xcm::{EnsureXcm, Origin as XcmOrigin};
21use xcm::latest::Location;
22use xcm_executor::traits::ConvertLocation;
23
24pub struct ForeignCreators<IsForeign, AccountOf, AccountId, L = Location>(
27 core::marker::PhantomData<(IsForeign, AccountOf, AccountId, L)>,
28);
29impl<
30 IsForeign: ContainsPair<L, L>,
31 AccountOf: ConvertLocation<AccountId>,
32 AccountId: Clone,
33 RuntimeOrigin: From<XcmOrigin> + OriginTrait + Clone + Debug,
34 L: TryFrom<Location> + TryInto<Location> + Clone + Debug,
35 > EnsureOriginWithArg<RuntimeOrigin, L> for ForeignCreators<IsForeign, AccountOf, AccountId, L>
36where
37 for<'a> &'a RuntimeOrigin::PalletsOrigin: TryInto<&'a XcmOrigin>,
38{
39 type Success = AccountId;
40
41 fn try_origin(
42 origin: RuntimeOrigin,
43 asset_location: &L,
44 ) -> core::result::Result<Self::Success, RuntimeOrigin> {
45 tracing::trace!(target: "xcm::try_origin", ?origin, ?asset_location, "ForeignCreators");
46 let origin_location = EnsureXcm::<Everything, L>::try_origin(origin.clone())?;
47 if !IsForeign::contains(asset_location, &origin_location) {
48 tracing::trace!(target: "xcm::try_origin", ?asset_location, ?origin_location, "ForeignCreators: no match");
49 return Err(origin)
50 }
51 let latest_location: Location =
52 origin_location.clone().try_into().map_err(|_| origin.clone())?;
53 AccountOf::convert_location(&latest_location).ok_or(origin)
54 }
55
56 #[cfg(feature = "runtime-benchmarks")]
57 fn try_successful_origin(a: &L) -> Result<RuntimeOrigin, ()> {
58 let latest_location: Location = (*a).clone().try_into().map_err(|_| ())?;
59 Ok(pallet_xcm::Origin::Xcm(latest_location).into())
60 }
61}