pub use crate::v3::{Error as OldError, SendError, XcmHash};
use codec::{Decode, DecodeWithMemTracking, Encode};
use core::result;
use scale_info::TypeInfo;
pub use 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,
#[codec(index = 35)]
TooManyAssets,
UnhandledXcmVersion,
WeightLimitReached(Weight),
Barrier,
WeightNotComputable,
ExceedsStackLimit,
}
impl TryFrom<OldError> for Error {
type Error = ();
fn try_from(old_error: OldError) -> result::Result<Error, ()> {
use OldError::*;
Ok(match old_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,
UnhandledXcmVersion => Self::UnhandledXcmVersion,
WeightLimitReached(weight) => Self::WeightLimitReached(weight),
Barrier => Self::Barrier,
WeightNotComputable => Self::WeightNotComputable,
ExceedsStackLimit => Self::ExceedsStackLimit,
})
}
}
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, DecodeWithMemTracking, Eq, PartialEq, Debug, TypeInfo)]
pub enum Outcome {
Complete { used: Weight },
Incomplete { used: Weight, error: InstructionError },
Error(InstructionError),
}
#[derive(Copy, Clone, Encode, Decode, DecodeWithMemTracking, Eq, PartialEq, Debug, TypeInfo)]
pub struct InstructionError {
pub index: InstructionIndex,
pub error: Error,
}
impl Outcome {
pub fn ensure_complete(self) -> result::Result<(), InstructionError> {
match self {
Outcome::Complete { .. } => Ok(()),
Outcome::Incomplete { error, .. } => Err(error),
Outcome::Error(error) => Err(error),
}
}
pub fn ensure_execution(self) -> result::Result<Weight, InstructionError> {
match self {
Outcome::Complete { used, .. } => Ok(used),
Outcome::Incomplete { used, .. } => Ok(used),
Outcome::Error(error) => Err(error),
}
}
pub fn weight_used(&self) -> Weight {
match self {
Outcome::Complete { used, .. } => *used,
Outcome::Incomplete { used, .. } => *used,
Outcome::Error(_) => Weight::zero(),
}
}
}
impl From<Error> for Outcome {
fn from(error: Error) -> Self {
Self::Error(InstructionError { error, index: 0 })
}
}
pub trait PreparedMessage {
fn weight_of(&self) -> Weight;
}
pub type InstructionIndex = u8;
pub trait ExecuteXcm<Call> {
type Prepared: PreparedMessage;
fn prepare(
message: Xcm<Call>,
weight_limit: Weight,
) -> result::Result<Self::Prepared, InstructionError>;
fn execute(
origin: impl Into<Location>,
pre: Self::Prepared,
id: &mut XcmHash,
weight_credit: Weight,
) -> Outcome;
fn prepare_and_execute(
origin: impl Into<Location>,
message: Xcm<Call>,
id: &mut XcmHash,
weight_limit: Weight,
weight_credit: Weight,
) -> Outcome {
let pre = match Self::prepare(message, weight_limit) {
Ok(x) => x,
Err(error) => return Outcome::Error(error),
};
Self::execute(origin, pre, id, weight_credit)
}
fn charge_fees(location: impl Into<Location>, fees: Assets) -> Result;
}
pub enum Weightless {}
impl PreparedMessage for Weightless {
fn weight_of(&self) -> Weight {
unreachable!()
}
}
impl<C> ExecuteXcm<C> for () {
type Prepared = Weightless;
fn prepare(_: Xcm<C>, _: Weight) -> result::Result<Self::Prepared, InstructionError> {
Err(InstructionError { index: 0, error: Error::Unimplemented })
}
fn execute(_: impl Into<Location>, _: Self::Prepared, _: &mut XcmHash, _: Weight) -> Outcome {
unreachable!()
}
fn charge_fees(_location: impl Into<Location>, _fees: Assets) -> Result {
Err(Error::Unimplemented)
}
}
pub trait Reanchorable: Sized {
type Error: Debug;
fn reanchor(
&mut self,
target: &Location,
context: &InteriorLocation,
) -> core::result::Result<(), ()>;
fn reanchored(
self,
target: &Location,
context: &InteriorLocation,
) -> core::result::Result<Self, Self::Error>;
}
pub type SendResult<T> = result::Result<(T, Assets), SendError>;
pub trait SendXcm {
type Ticket;
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket>;
fn deliver(ticket: Self::Ticket) -> result::Result<XcmHash, SendError>;
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_delivery(_location: Option<Location>) {}
}
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl SendXcm for Tuple {
for_tuples! { type Ticket = (#( Option<Tuple::Ticket> ),* ); }
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let mut maybe_cost: Option<Assets> = 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)
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_delivery(location: Option<Location>) {
for_tuples!( #(
return Tuple::ensure_successful_delivery(location.clone());
)* );
}
}
pub fn validate_send<T: SendXcm>(dest: Location, msg: Xcm<()>) -> SendResult<T::Ticket> {
T::validate(&mut Some(dest), &mut Some(msg))
}
pub fn send_xcm<T: SendXcm>(
dest: Location,
msg: Xcm<()>,
) -> result::Result<(XcmHash, Assets), SendError> {
let (ticket, price) = T::validate(&mut Some(dest), &mut Some(msg))?;
let hash = T::deliver(ticket)?;
Ok((hash, price))
}