// SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023 Snowfork //! //! System frontend pezpallet that acts as the user-facing control-plane for Snowbridge. //! //! Some operations are delegated to a backend pezpallet installed on a remote teyrchain. //! //! # Extrinsics //! //! * [`Call::register_token`]: Register Pezkuwi native asset as a wrapped ERC20 token on Ethereum. #![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 pezframe_support::{pezpallet_prelude::*, traits::EnsureOriginWithArg}; use pezframe_system::pezpallet_prelude::*; use pezpallet_asset_conversion::Swap; use pezsnowbridge_core::{ burn_for_teleport, operating_mode::ExportPausedQuery, reward::MessageId, AssetMetadata, BasicOperatingMode as OperatingMode, }; use pezsp_std::prelude::*; use xcm::{ latest::{validate_send, XcmHash}, prelude::*, }; use xcm_executor::traits::{FeeManager, FeeReason, TransactAsset}; #[cfg(feature = "runtime-benchmarks")] use pezframe_support::traits::OriginTrait; pub use pezpallet::*; pub type AccountIdOf = ::AccountId; pub const LOG_TARGET: &str = "snowbridge-system-frontend"; /// Call indices within BridgeHub runtime for dispatchables within /// `pezsnowbridge-pezpallet-system-v2` #[allow(clippy::large_enum_variant)] #[derive(Encode, Decode, Debug, PartialEq, Clone, TypeInfo)] pub enum BridgeHubRuntime { #[codec(index = 90)] EthereumSystem(EthereumSystemCall), } /// Call indices for dispatchables within `pezsnowbridge-pezpallet-system-v2` #[derive(Encode, Decode, Debug, PartialEq, Clone, TypeInfo)] pub enum EthereumSystemCall { #[codec(index = 2)] RegisterToken { sender: Box, asset_id: Box, metadata: AssetMetadata, amount: u128, }, #[codec(index = 3)] AddTip { sender: AccountIdOf, message_id: MessageId, amount: u128 }, } #[cfg(feature = "runtime-benchmarks")] pub trait BenchmarkHelper where O: OriginTrait, { fn make_xcm_origin(location: Location) -> O; fn initialize_storage(asset_location: Location, asset_owner: Location); fn setup_pools(caller: AccountId, asset: Location); } #[pezframe_support::pezpallet] pub mod pezpallet { use super::*; use xcm_executor::traits::ConvertLocation; #[pezpallet::pezpallet] pub struct Pezpallet(_); #[pezpallet::config] pub trait Config: pezframe_system::Config { #[allow(deprecated)] type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Origin check for XCM locations that can register token type RegisterTokenOrigin: EnsureOriginWithArg< Self::RuntimeOrigin, Location, Success = Location, >; /// XCM message sender type XcmSender: SendXcm; /// To withdraw and deposit an asset. type AssetTransactor: TransactAsset; /// To charge XCM delivery fees type XcmExecutor: ExecuteXcm + FeeManager; /// Fee asset for the execution cost on ethereum type EthereumLocation: Get; /// To swap the provided tip asset for type Swap: Swap; /// Location of bridge hub type BridgeHubLocation: Get; /// Universal location of this runtime. type UniversalLocation: Get; /// InteriorLocation of this pezpallet. type PalletLocation: Get; type AccountIdConverter: ConvertLocation; /// Weights for dispatching XCM to backend implementation of `register_token` type BackendWeightInfo: BackendWeightInfo; /// Weights for pezpallet dispatchables type WeightInfo: WeightInfo; /// A set of helper functions for benchmarking. #[cfg(feature = "runtime-benchmarks")] type Helper: BenchmarkHelper; } #[pezpallet::event] #[pezpallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { /// An XCM was sent MessageSent { origin: Location, destination: Location, message: Xcm<()>, message_id: XcmHash, }, /// Set OperatingMode ExportOperatingModeChanged { mode: OperatingMode }, } #[pezpallet::error] pub enum Error { /// Convert versioned location failure UnsupportedLocationVersion, /// Check location failure, should start from the dispatch origin as owner InvalidAssetOwner, /// Send xcm message failure SendFailure, /// Withdraw fee asset failure FeesNotMet, /// Convert to reanchored location failure LocationConversionFailed, /// Message export is halted Halted, /// The desired destination was unreachable, generally because there is a no way of routing /// to it. Unreachable, /// The asset provided for the tip is unsupported. UnsupportedAsset, /// Unable to withdraw asset. WithdrawError, /// Account could not be converted to a location. InvalidAccount, /// Provided tip asset could not be swapped for ether. SwapError, /// Ether could not be burned. BurnError, /// The tip provided is zero. TipAmountZero, } impl From for Error { fn from(e: SendError) -> Self { match e { SendError::Fees => Error::::FeesNotMet, SendError::NotApplicable => Error::::Unreachable, _ => Error::::SendFailure, } } } /// The current operating mode for exporting to Ethereum. #[pezpallet::storage] #[pezpallet::getter(fn export_operating_mode)] pub type ExportOperatingMode = StorageValue<_, OperatingMode, ValueQuery>; #[pezpallet::call] impl Pezpallet where ::AccountId: Into, { /// Set the operating mode for exporting messages to Ethereum. #[pezpallet::call_index(0)] #[pezpallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))] pub fn set_operating_mode(origin: OriginFor, mode: OperatingMode) -> DispatchResult { ensure_root(origin)?; ExportOperatingMode::::put(mode); Self::deposit_event(Event::ExportOperatingModeChanged { mode }); Ok(()) } /// Initiates the registration for a Pezkuwi-native token as a wrapped ERC20 token on /// Ethereum. /// - `asset_id`: Location of the asset /// - `metadata`: Metadata to include in the instantiated ERC20 contract on Ethereum /// /// All origins are allowed, however `asset_id` must be a location nested within the origin /// consensus system. #[pezpallet::call_index(1)] #[pezpallet::weight( T::WeightInfo::register_token() .saturating_add(T::BackendWeightInfo::transact_register_token()) .saturating_add(T::BackendWeightInfo::do_process_message()) .saturating_add(T::BackendWeightInfo::commit_single()) .saturating_add(T::BackendWeightInfo::submit_delivery_receipt()) )] pub fn register_token( origin: OriginFor, asset_id: Box, metadata: AssetMetadata, fee_asset: Asset, ) -> DispatchResult { ensure!(!Self::export_operating_mode().is_halted(), Error::::Halted); let asset_location: Location = (*asset_id).try_into().map_err(|_| Error::::UnsupportedLocationVersion)?; let origin_location = T::RegisterTokenOrigin::ensure_origin(origin, &asset_location)?; let ether_gained = if origin_location.is_here() { // Root origin/location does not pay any fees/tip. 0 } else { Self::swap_fee_asset_and_burn(origin_location.clone(), fee_asset)? }; let call = Self::build_register_token_call( origin_location.clone(), asset_location, metadata, ether_gained, )?; Self::send_transact_call(origin_location, call) } /// Add an additional relayer tip for a committed message identified by `message_id`. /// The tip asset will be swapped for ether. #[pezpallet::call_index(2)] #[pezpallet::weight( T::WeightInfo::add_tip() .saturating_add(T::BackendWeightInfo::transact_add_tip()) )] pub fn add_tip(origin: OriginFor, message_id: MessageId, asset: Asset) -> DispatchResult where ::AccountId: Into, { let who = ensure_signed(origin)?; let ether_gained = Self::swap_fee_asset_and_burn(who.clone().into(), asset)?; // Send the tip details to BH to be allocated to the reward in the Inbound/Outbound // pezpallet let call = Self::build_add_tip_call(who.clone(), message_id.clone(), ether_gained); Self::send_transact_call(who.into(), call) } } impl Pezpallet { fn send_xcm(origin: Location, dest: Location, xcm: Xcm<()>) -> Result { let is_waived = ::is_waived(Some(&origin), FeeReason::ChargeFees); let (ticket, price) = validate_send::(dest, xcm.clone())?; if !is_waived { T::XcmExecutor::charge_fees(origin, price).map_err(|_| SendError::Fees)?; } T::XcmSender::deliver(ticket) } /// Swaps a specified tip asset to Ether and then burns the resulting ether for /// teleportation. Returns the amount of Ether gained if successful, or a DispatchError if /// any step fails. fn swap_and_burn( origin: Location, tip_asset_location: Location, ether_location: Location, tip_amount: u128, ) -> Result { // Swap tip asset to ether let swap_path = vec![tip_asset_location.clone(), ether_location.clone()]; let who = T::AccountIdConverter::convert_location(&origin) .ok_or(Error::::LocationConversionFailed)?; let ether_gained = T::Swap::swap_exact_tokens_for_tokens( who.clone(), swap_path, tip_amount, None, // No minimum amount required who, true, )?; // Burn the ether let ether_asset = Asset::from((ether_location.clone(), ether_gained)); burn_for_teleport::(&origin, ðer_asset) .map_err(|_| Error::::BurnError)?; Ok(ether_gained) } // Build the call to dispatch the `EthereumSystem::register_token` extrinsic on BH fn build_register_token_call( sender: Location, asset: Location, metadata: AssetMetadata, amount: u128, ) -> Result, Error> { // reanchor locations relative to BH 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, amount, }); Ok(call) } // Build the call to dispatch the `EthereumSystem::add_tip` extrinsic on BH fn build_add_tip_call( sender: AccountIdOf, message_id: MessageId, amount: u128, ) -> BridgeHubRuntime { BridgeHubRuntime::EthereumSystem(EthereumSystemCall::AddTip { sender, message_id, amount, }) } 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, }, ]) } /// Reanchors `location` relative to BridgeHub. fn reanchored(location: Location) -> Result> { location .reanchored(&T::BridgeHubLocation::get(), &T::UniversalLocation::get()) .map_err(|_| Error::::LocationConversionFailed) } fn swap_fee_asset_and_burn( origin: Location, fee_asset: Asset, ) -> Result { let ether_location = T::EthereumLocation::get(); let (fee_asset_location, fee_amount) = match fee_asset { Asset { id: AssetId(ref loc), fun: Fungible(amount) } => (loc, amount), _ => { tracing::debug!(target: LOG_TARGET, ?fee_asset, "error matching fee asset"); return Err(Error::::UnsupportedAsset.into()); }, }; if fee_amount == 0 { return Ok(0); } let ether_gained = if *fee_asset_location != ether_location { Self::swap_and_burn( origin.clone(), fee_asset_location.clone(), ether_location, fee_amount, ) .inspect_err(|&e| { tracing::debug!(target: LOG_TARGET, ?e, "error swapping asset"); })? } else { burn_for_teleport::(&origin, &fee_asset) .map_err(|_| Error::::BurnError)?; fee_amount }; Ok(ether_gained) } fn send_transact_call( origin_location: Location, call: BridgeHubRuntime, ) -> DispatchResult { let dest = T::BridgeHubLocation::get(); 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::::from(error))?; Self::deposit_event(Event::::MessageSent { origin: T::PalletLocation::get().into(), destination: dest, message: remote_xcm, message_id, }); Ok(()) } } impl ExportPausedQuery for Pezpallet { fn is_paused() -> bool { Self::export_operating_mode().is_halted() } } }