axelar-cgp-sui

Module axelar_gateway::channel

Channels

Channels allow sending and receiving messages between Sui and other chains. A channel has a unique id and is treated as the destination address by the Axelar protocol. Apps can create a channel and hold on to it for cross-chain messaging.

use axelar_gateway::bytes32;
use axelar_gateway::events;
use axelar_gateway::message;
use axelar_gateway::weighted_signer;
use axelar_gateway::weighted_signers;
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::bcs;
use sui::event;
use sui::hash;
use sui::hex;
use sui::object;
use sui::tx_context;

Struct Channel

The Channel object. Acts as a destination for the messages sent through the bridge. The destination_id is compared against the id of the Channel when the message is consumed

public struct Channel has key, store
Fields
id: sui::object::UID
Unique ID of the channel

Struct ApprovedMessage

A HotPotato - this should be received by the application contract and consumed

public struct ApprovedMessage
Fields
source_chain: std::ascii::String
Source chain axelar-registered name
message_id: std::ascii::String
Unique ID of the message
source_address: std::ascii::String
Address of the source chain, encoded as a string (e.g. EVM address will be hex string 0x1234...abcd)
destination_id: address
The destination Channel's UID
payload: vector<u8>
Message payload

Constants

If approved message is consumed by an invalid destination id

#[error]
const EInvalidDestination: vector<u8> = b"invalid destination";

Function new

Create new Channel object. Anyone can create their own Channel to receive cross-chain messages. In most use cases, a package should create this on init, and hold on to it forever.

public fun new(ctx: &mut sui::tx_context::TxContext): axelar_gateway::channel::Channel
Implementation
public fun new(ctx: &mut TxContext): Channel {
    let id = object::new(ctx);
    events::channel_created(id.uid_to_address());
    Channel {
        id,
    }
}

Function destroy

Destroy a Channel. Allows apps to destroy the Channel object when it’s no longer needed.

public fun destroy(self: axelar_gateway::channel::Channel)
Implementation
public fun destroy(self: Channel) {
    let Channel { id } = self;
    events::channel_destroyed(id.uid_to_address());
    id.delete();
}

Function id

public fun id(self: &axelar_gateway::channel::Channel): sui::object::ID
Implementation
public fun id(self: &Channel): ID {
    object::id(self)
}

Function to_address

public fun to_address(self: &axelar_gateway::channel::Channel): address
Implementation
public fun to_address(self: &Channel): address {
    object::id_address(self)
}

Function consume_approved_message

Consume an approved message hot potato object intended for this Channel.

public fun consume_approved_message(channel: &axelar_gateway::channel::Channel, approved_message: axelar_gateway::channel::ApprovedMessage): (std::ascii::String, std::ascii::String, std::ascii::String, vector<u8>)
Implementation
public fun consume_approved_message(channel: &Channel, approved_message: ApprovedMessage): (String, String, String, vector<u8>) {
    let ApprovedMessage {
        source_chain,
        message_id,
        source_address,
        destination_id,
        payload,
    } = approved_message;
    // Check if the message is sent to the correct destination.
    assert!(destination_id == object::id_address(channel), EInvalidDestination);
    (source_chain, message_id, source_address, payload)
}

Function create_approved_message

Create a new ApprovedMessage object to be sent to another chain. Is called by the gateway when a message is “picked up” by the relayer.

public(package) fun create_approved_message(source_chain: std::ascii::String, message_id: std::ascii::String, source_address: std::ascii::String, destination_id: address, payload: vector<u8>): axelar_gateway::channel::ApprovedMessage
Implementation
public(package) fun create_approved_message(
    source_chain: String,
    message_id: String,
    source_address: String,
    destination_id: address,
    payload: vector<u8>,
): ApprovedMessage {
    ApprovedMessage {
        source_chain,
        message_id,
        source_address,
        destination_id,
        payload,
    }
}