pub trait SendXcm {
    fn send_xcm(
        destination: impl Into<MultiLocation>,
        message: Xcm<()>
    ) -> Result<(), SendError>; }
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<()>) -> SendResult {
        return Err(SendError::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<()>) -> SendResult {
        if let MultiLocation { parents: 0, interior: X2(j1, j2) } = destination.into() {
            Ok(())
        } else {
            Err(SendError::Unroutable)
        }
    }
}

/// A sender that accepts a message from a parent, passing through otherwise.
struct Sender3;
impl SendXcm for Sender3 {
    fn send_xcm(destination: impl Into<MultiLocation>, message: Xcm<()>) -> SendResult {
        let destination = destination.into();
        match destination {
            MultiLocation { parents: 1, interior: Here } => Ok(()),
            _ => Err(SendError::CannotReachDestination(destination, message)),
        }
    }
}

// A call to send via XCM. We don't really care about this.
let call: Vec<u8> = ().encode();
let message = Xcm(vec![Instruction::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§