use crate::v5::Error as NewError;
use core::result;
use scale_info::TypeInfo;
pub use pezsp_weights::Weight;
pub trait GetWeight<W> {
fn weight(&self) -> pezsp_weights::Weight;
}
use super::*;
#[derive(
Copy,
Clone,
Encode,
Decode,
DecodeWithMemTracking,
Eq,
PartialEq,
Debug,
TypeInfo,
MaxEncodedLen,
)]
#[scale_info(replace_segment("pezstaging_xcm", "xcm"))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum Error {
#[codec(index = 0)]
Overflow,
#[codec(index = 1)]
Unimplemented,
#[codec(index = 2)]
UntrustedReserveLocation,
#[codec(index = 3)]
UntrustedTeleportLocation,
#[codec(index = 4)]
LocationFull,
#[codec(index = 5)]
LocationNotInvertible,
#[codec(index = 6)]
BadOrigin,
#[codec(index = 7)]
InvalidLocation,
#[codec(index = 8)]
AssetNotFound,
#[codec(index = 9)]
FailedToTransactAsset(#[codec(skip)] &'static str),
#[codec(index = 10)]
NotWithdrawable,
#[codec(index = 11)]
LocationCannotHold,
#[codec(index = 12)]
ExceedsMaxMessageSize,
#[codec(index = 13)]
DestinationUnsupported,
#[codec(index = 14)]
Transport(#[codec(skip)] &'static str),
#[codec(index = 15)]
Unroutable,
#[codec(index = 16)]
UnknownClaim,
#[codec(index = 17)]
FailedToDecode,
#[codec(index = 18)]
MaxWeightInvalid,
#[codec(index = 19)]
NotHoldingFees,
#[codec(index = 20)]
TooExpensive,
#[codec(index = 21)]
Trap(u64),
#[codec(index = 22)]
ExpectationFalse,
#[codec(index = 23)]
PalletNotFound,
#[codec(index = 24)]
NameMismatch,
#[codec(index = 25)]
VersionIncompatible,
#[codec(index = 26)]
HoldingWouldOverflow,
#[codec(index = 27)]
ExportError,
#[codec(index = 28)]
ReanchorFailed,
#[codec(index = 29)]
NoDeal,
#[codec(index = 30)]
FeesNotMet,
#[codec(index = 31)]
LockError,
#[codec(index = 32)]
NoPermission,
#[codec(index = 33)]
Unanchored,
#[codec(index = 34)]
NotDepositable,
UnhandledXcmVersion,
WeightLimitReached(Weight),
Barrier,
WeightNotComputable,
ExceedsStackLimit,
}
impl TryFrom<NewError> for Error {
type Error = ();
fn try_from(new_error: NewError) -> result::Result<Error, ()> {
use NewError::*;
Ok(match new_error {
Overflow => Self::Overflow,
Unimplemented => Self::Unimplemented,
UntrustedReserveLocation => Self::UntrustedReserveLocation,
UntrustedTeleportLocation => Self::UntrustedTeleportLocation,
LocationFull => Self::LocationFull,
LocationNotInvertible => Self::LocationNotInvertible,
BadOrigin => Self::BadOrigin,
InvalidLocation => Self::InvalidLocation,
AssetNotFound => Self::AssetNotFound,
FailedToTransactAsset(s) => Self::FailedToTransactAsset(s),
NotWithdrawable => Self::NotWithdrawable,
LocationCannotHold => Self::LocationCannotHold,
ExceedsMaxMessageSize => Self::ExceedsMaxMessageSize,
DestinationUnsupported => Self::DestinationUnsupported,
Transport(s) => Self::Transport(s),
Unroutable => Self::Unroutable,
UnknownClaim => Self::UnknownClaim,
FailedToDecode => Self::FailedToDecode,
MaxWeightInvalid => Self::MaxWeightInvalid,
NotHoldingFees => Self::NotHoldingFees,
TooExpensive => Self::TooExpensive,
Trap(i) => Self::Trap(i),
ExpectationFalse => Self::ExpectationFalse,
PalletNotFound => Self::PalletNotFound,
NameMismatch => Self::NameMismatch,
VersionIncompatible => Self::VersionIncompatible,
HoldingWouldOverflow => Self::HoldingWouldOverflow,
ExportError => Self::ExportError,
ReanchorFailed => Self::ReanchorFailed,
NoDeal => Self::NoDeal,
FeesNotMet => Self::FeesNotMet,
LockError => Self::LockError,
NoPermission => Self::NoPermission,
Unanchored => Self::Unanchored,
NotDepositable => Self::NotDepositable,
_ => return Err(()),
})
}
}
impl From<SendError> for Error {
fn from(e: SendError) -> Self {
match e {
SendError::NotApplicable | SendError::Unroutable | SendError::MissingArgument => {
Error::Unroutable
},
SendError::Transport(s) => Error::Transport(s),
SendError::DestinationUnsupported => Error::DestinationUnsupported,
SendError::ExceedsMaxMessageSize => Error::ExceedsMaxMessageSize,
SendError::Fees => Error::FeesNotMet,
}
}
}
pub type Result = result::Result<(), Error>;
#[derive(Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
#[scale_info(replace_segment("pezstaging_xcm", "xcm"))]
pub enum Outcome {
Complete(Weight),
Incomplete(Weight, Error),
Error(Error),
}
impl Outcome {
pub fn ensure_complete(self) -> result::Result<Weight, Error> {
match self {
Outcome::Complete(weight) => Ok(weight),
Outcome::Incomplete(_, e) => Err(e),
Outcome::Error(e) => Err(e),
}
}
pub fn ensure_execution(self) -> result::Result<Weight, Error> {
match self {
Outcome::Complete(w) => Ok(w),
Outcome::Incomplete(w, _) => Ok(w),
Outcome::Error(e) => Err(e),
}
}
pub fn weight_used(&self) -> Weight {
match self {
Outcome::Complete(w) => *w,
Outcome::Incomplete(w, _) => *w,
Outcome::Error(_) => Weight::zero(),
}
}
}
pub trait PreparedMessage {
fn weight_of(&self) -> Weight;
}
pub trait ExecuteXcm<Call> {
type Prepared: PreparedMessage;
fn prepare(message: Xcm<Call>) -> result::Result<Self::Prepared, Xcm<Call>>;
fn execute(
origin: impl Into<MultiLocation>,
pre: Self::Prepared,
id: &mut XcmHash,
weight_credit: Weight,
) -> Outcome;
fn prepare_and_execute(
origin: impl Into<MultiLocation>,
message: Xcm<Call>,
id: &mut XcmHash,
weight_limit: Weight,
weight_credit: Weight,
) -> Outcome {
let pre = match Self::prepare(message) {
Ok(x) => x,
Err(_) => return Outcome::Error(Error::WeightNotComputable),
};
let xcm_weight = pre.weight_of();
if xcm_weight.any_gt(weight_limit) {
return Outcome::Error(Error::WeightLimitReached(xcm_weight));
}
Self::execute(origin, pre, id, weight_credit)
}
fn execute_xcm(
origin: impl Into<MultiLocation>,
message: Xcm<Call>,
hash: XcmHash,
weight_limit: Weight,
) -> Outcome {
let origin = origin.into();
tracing::trace!(
target: "xcm::execute_xcm",
?origin,
?message,
?weight_limit,
);
Self::execute_xcm_in_credit(origin, message, hash, weight_limit, Weight::zero())
}
fn execute_xcm_in_credit(
origin: impl Into<MultiLocation>,
message: Xcm<Call>,
mut hash: XcmHash,
weight_limit: Weight,
weight_credit: Weight,
) -> Outcome {
let pre = match Self::prepare(message) {
Ok(x) => x,
Err(_) => return Outcome::Error(Error::WeightNotComputable),
};
let xcm_weight = pre.weight_of();
if xcm_weight.any_gt(weight_limit) {
return Outcome::Error(Error::WeightLimitReached(xcm_weight));
}
Self::execute(origin, pre, &mut hash, weight_credit)
}
fn charge_fees(location: impl Into<MultiLocation>, fees: MultiAssets) -> Result;
}
pub enum Weightless {}
impl PreparedMessage for Weightless {
fn weight_of(&self) -> Weight {
unreachable!()
}
}
impl<C> ExecuteXcm<C> for () {
type Prepared = Weightless;
fn prepare(message: Xcm<C>) -> result::Result<Self::Prepared, Xcm<C>> {
Err(message)
}
fn execute(
_: impl Into<MultiLocation>,
_: Self::Prepared,
_: &mut XcmHash,
_: Weight,
) -> Outcome {
unreachable!()
}
fn charge_fees(_location: impl Into<MultiLocation>, _fees: MultiAssets) -> Result {
Err(Error::Unimplemented)
}
}
#[derive(
Clone, Encode, Decode, DecodeWithMemTracking, Eq, PartialEq, Debug, scale_info::TypeInfo,
)]
#[scale_info(replace_segment("pezstaging_xcm", "xcm"))]
pub enum SendError {
NotApplicable,
Transport(#[codec(skip)] &'static str),
Unroutable,
DestinationUnsupported,
ExceedsMaxMessageSize,
MissingArgument,
Fees,
}
pub type XcmHash = [u8; 32];
pub type SendResult<T> = result::Result<(T, MultiAssets), SendError>;
pub trait SendXcm {
type Ticket;
fn validate(
destination: &mut Option<MultiLocation>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket>;
fn deliver(ticket: Self::Ticket) -> result::Result<XcmHash, SendError>;
}
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl SendXcm for Tuple {
for_tuples! { type Ticket = (#( Option<Tuple::Ticket> ),* ); }
fn validate(
destination: &mut Option<MultiLocation>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let mut maybe_cost: Option<MultiAssets> = None;
let one_ticket: Self::Ticket = (for_tuples! { #(
if maybe_cost.is_some() {
None
} else {
match Tuple::validate(destination, message) {
Err(SendError::NotApplicable) => None,
Err(e) => { return Err(e) },
Ok((v, c)) => {
maybe_cost = Some(c);
Some(v)
},
}
}
),* });
if let Some(cost) = maybe_cost {
Ok((one_ticket, cost))
} else {
Err(SendError::NotApplicable)
}
}
fn deliver(one_ticket: Self::Ticket) -> result::Result<XcmHash, SendError> {
for_tuples!( #(
if let Some(validated) = one_ticket.Tuple {
return Tuple::deliver(validated);
}
)* );
Err(SendError::Unroutable)
}
}
pub fn validate_send<T: SendXcm>(dest: MultiLocation, msg: Xcm<()>) -> SendResult<T::Ticket> {
T::validate(&mut Some(dest), &mut Some(msg))
}
pub fn send_xcm<T: SendXcm>(
dest: MultiLocation,
msg: Xcm<()>,
) -> result::Result<(XcmHash, MultiAssets), SendError> {
let (ticket, price) = T::validate(&mut Some(dest), &mut Some(msg))?;
let hash = T::deliver(ticket)?;
Ok((hash, price))
}