Trait xcm::v1::prelude::SendXcm

source ·
pub trait SendXcm {
    fn send_xcm(
        destination: impl Into<MultiLocation>,
        message: Xcm<()>
    ) -> Result; }
Expand description

Utility for sending an XCM message.

These can be amalgamated in tuples to form sophisticated routing systems. In tuple format, each router might return CannotReachDestination to pass the execution to the next sender item. Note that each CannotReachDestination might alter the destination and the XCM message for to the next router.

Example


/// A sender that only passes the message through and does nothing.
struct Sender1;
impl SendXcm for Sender1 {
    fn send_xcm(destination: impl Into<MultiLocation>, message: Xcm<()>) -> Result {
        return Err(Error::CannotReachDestination(destination.into(), message))
    }
}

/// A sender that accepts a message that has an X2 junction, otherwise stops the routing.
struct Sender2;
impl SendXcm for Sender2 {
    fn send_xcm(destination: impl Into<MultiLocation>, message: Xcm<()>) -> Result {
        let destination = destination.into();
        if matches!(destination.interior(), Junctions::X2(j1, j2))
            && destination.parent_count() == 0
        {
            Ok(())
        } else {
            Err(Error::Undefined)
        }
    }
}

/// A sender that accepts a message from an X1 parent junction, passing through otherwise.
struct Sender3;
impl SendXcm for Sender3 {
    fn send_xcm(destination: impl Into<MultiLocation>, message: Xcm<()>) -> Result {
        let destination = destination.into();
        if matches!(destination.interior(), Junctions::Here)
            && destination.parent_count() == 1
        {
            Ok(())
        } else {
            Err(Error::CannotReachDestination(destination, message))
        }
    }
}

// A call to send via XCM. We don't really care about this.
let call: Vec<u8> = ().encode();
let message = Xcm::Transact { origin_type: OriginKind::Superuser, require_weight_at_most: 0, call: call.into() };

assert!(
    // Sender2 will block this.
    <(Sender1, Sender2, Sender3) as SendXcm>::send_xcm(Parent, message.clone())
        .is_err()
);

assert!(
    // Sender3 will catch this.
    <(Sender1, Sender3) as SendXcm>::send_xcm(Parent, message.clone())
        .is_ok()
);

Required Methods§

Send an XCM message to a given destination.

If it is not a destination which can be reached with this type but possibly could by others, then it MUST return CannotReachDestination. Any other error will cause the tuple implementation to exit early without trying other type fields.

Implementations on Foreign Types§

Implementors§