snowbridge_pallet_system_frontend/
lib.rs#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::*;
pub mod backend_weights;
pub use backend_weights::*;
use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg};
use frame_system::pallet_prelude::*;
use snowbridge_core::{
operating_mode::ExportPausedQuery, AssetMetadata, BasicOperatingMode as OperatingMode,
};
use sp_std::prelude::*;
use xcm::{
latest::{validate_send, XcmHash},
prelude::*,
};
use xcm_executor::traits::{FeeManager, FeeReason, TransactAsset};
#[cfg(feature = "runtime-benchmarks")]
use frame_support::traits::OriginTrait;
pub use pallet::*;
pub const LOG_TARGET: &str = "snowbridge-system-frontend";
#[allow(clippy::large_enum_variant)]
#[derive(Encode, Decode, Debug, PartialEq, Clone, TypeInfo)]
pub enum BridgeHubRuntime {
#[codec(index = 90)]
EthereumSystem(EthereumSystemCall),
}
#[derive(Encode, Decode, Debug, PartialEq, Clone, TypeInfo)]
pub enum EthereumSystemCall {
#[codec(index = 0)]
RegisterToken {
sender: Box<VersionedLocation>,
asset_id: Box<VersionedLocation>,
metadata: AssetMetadata,
},
}
#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper<O>
where
O: OriginTrait,
{
fn make_xcm_origin(location: Location) -> O;
fn initialize_storage(asset_location: Location, asset_owner: Location);
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type RegisterTokenOrigin: EnsureOriginWithArg<
Self::RuntimeOrigin,
Location,
Success = Location,
>;
type XcmSender: SendXcm;
type AssetTransactor: TransactAsset;
type XcmExecutor: ExecuteXcm<Self::RuntimeCall> + FeeManager;
type EthereumLocation: Get<Location>;
type BridgeHubLocation: Get<Location>;
type UniversalLocation: Get<InteriorLocation>;
type PalletLocation: Get<InteriorLocation>;
type BackendWeightInfo: BackendWeightInfo;
type WeightInfo: WeightInfo;
#[cfg(feature = "runtime-benchmarks")]
type Helper: BenchmarkHelper<Self::RuntimeOrigin>;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
MessageSent {
origin: Location,
destination: Location,
message: Xcm<()>,
message_id: XcmHash,
},
ExportOperatingModeChanged { mode: OperatingMode },
}
#[pallet::error]
pub enum Error<T> {
UnsupportedLocationVersion,
InvalidAssetOwner,
SendFailure,
FeesNotMet,
LocationConversionFailed,
Halted,
Unreachable,
}
impl<T: Config> From<SendError> for Error<T> {
fn from(e: SendError) -> Self {
match e {
SendError::Fees => Error::<T>::FeesNotMet,
SendError::NotApplicable => Error::<T>::Unreachable,
_ => Error::<T>::SendFailure,
}
}
}
#[pallet::storage]
#[pallet::getter(fn export_operating_mode)]
pub type ExportOperatingMode<T: Config> = StorageValue<_, OperatingMode, ValueQuery>;
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
pub fn set_operating_mode(origin: OriginFor<T>, mode: OperatingMode) -> DispatchResult {
ensure_root(origin)?;
ExportOperatingMode::<T>::put(mode);
Self::deposit_event(Event::ExportOperatingModeChanged { mode });
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(
T::WeightInfo::register_token()
.saturating_add(T::BackendWeightInfo::transact_register_token())
)]
pub fn register_token(
origin: OriginFor<T>,
asset_id: Box<VersionedLocation>,
metadata: AssetMetadata,
) -> DispatchResult {
ensure!(!Self::export_operating_mode().is_halted(), Error::<T>::Halted);
let asset_location: Location =
(*asset_id).try_into().map_err(|_| Error::<T>::UnsupportedLocationVersion)?;
let origin_location = T::RegisterTokenOrigin::ensure_origin(origin, &asset_location)?;
let dest = T::BridgeHubLocation::get();
let call =
Self::build_register_token_call(origin_location.clone(), asset_location, metadata)?;
let remote_xcm = Self::build_remote_xcm(&call);
let message_id = Self::send_xcm(origin_location, dest.clone(), remote_xcm.clone())
.map_err(|error| Error::<T>::from(error))?;
Self::deposit_event(Event::<T>::MessageSent {
origin: T::PalletLocation::get().into(),
destination: dest,
message: remote_xcm,
message_id,
});
Ok(())
}
}
impl<T: Config> Pallet<T> {
fn send_xcm(origin: Location, dest: Location, xcm: Xcm<()>) -> Result<XcmHash, SendError> {
let is_waived =
<T::XcmExecutor as FeeManager>::is_waived(Some(&origin), FeeReason::ChargeFees);
let (ticket, price) = validate_send::<T::XcmSender>(dest, xcm.clone())?;
if !is_waived {
T::XcmExecutor::charge_fees(origin, price).map_err(|_| SendError::Fees)?;
}
T::XcmSender::deliver(ticket)
}
fn build_register_token_call(
sender: Location,
asset: Location,
metadata: AssetMetadata,
) -> Result<BridgeHubRuntime, Error<T>> {
let sender = Self::reanchored(sender)?;
let asset = Self::reanchored(asset)?;
let call = BridgeHubRuntime::EthereumSystem(EthereumSystemCall::RegisterToken {
sender: Box::new(VersionedLocation::from(sender)),
asset_id: Box::new(VersionedLocation::from(asset)),
metadata,
});
Ok(call)
}
fn build_remote_xcm(call: &impl Encode) -> Xcm<()> {
Xcm(vec![
DescendOrigin(T::PalletLocation::get()),
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
Transact {
origin_kind: OriginKind::Xcm,
call: call.encode().into(),
fallback_max_weight: None,
},
])
}
fn reanchored(location: Location) -> Result<Location, Error<T>> {
location
.reanchored(&T::BridgeHubLocation::get(), &T::UniversalLocation::get())
.map_err(|_| Error::<T>::LocationConversionFailed)
}
}
impl<T: Config> ExportPausedQuery for Pallet<T> {
fn is_paused() -> bool {
Self::export_operating_mode().is_halted()
}
}
}