axelar-cgp-sui

Module 0xa2::discovery

This module implements a discovery mechanic for the Relayer to be able to call some (!) transactions automatically.

Warning: this solution does allow for any transaction to be executed and should be treated as a reference and a temporary solution until there’s a proper discovery / execution mechanism in place.

use 0x1::ascii;
use 0x1::vector;
use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::versioned;
use 0xa1::channel;
use 0xa2::relayer_discovery_v0;
use 0xa2::transaction;
use 0xb0::version_control;

Resource RelayerDiscovery


Structs ——-

struct RelayerDiscovery has key
Fields
id: object::UID
inner: versioned::Versioned

Constants


Version ——- This is the version of the package that should change every package upgrade.

const VERSION: u64 = 0;

This is the version of the data that should change when we need to migrate Versioned type (e.g. from RelayerDiscovery_v0 to RelayerDiscoveryV1)

const DATA_VERSION: u64 = 0;

Function init

fun init(ctx: &mut tx_context::TxContext)
Implementation
fun init(ctx: &mut TxContext) {
    let inner = versioned::create(
        DATA_VERSION,
        relayer_discovery_v0::new(
            version_control(),
            ctx,
        ),
        ctx,
    );
    transfer::share_object(RelayerDiscovery {
        id: object::new(ctx),
        inner,
    });
}

Function register_transaction

During the creation of the object, the UID should be passed here to receive the Channel and emit an event which will be handled by the Relayer.

Example:

let id = object::new(ctx);
let channel = discovery::create_configuration(
relayer_discovery, &id, contents, ctx
);
let wrapper = ExampleWrapper { id, channel };
transfer::share_object(wrapper);

Note: Wrapper must be a shared object so that Relayer can access it.

public fun register_transaction(self: &mut discovery::RelayerDiscovery, channel: &channel::Channel, tx: transaction::Transaction)
Implementation
public fun register_transaction(
    self: &mut RelayerDiscovery,
    channel: &Channel,
    tx: Transaction,
) {
    let value = self.value_mut!(b"register_transaction");
    let channel_id = channel.id();
    value.set_transaction(channel_id, tx);
}

Function remove_transaction

public fun remove_transaction(self: &mut discovery::RelayerDiscovery, channel: &channel::Channel)
Implementation
public fun remove_transaction(self: &mut RelayerDiscovery, channel: &Channel) {
    let value = self.value_mut!(b"remove_transaction");
    let channel_id = channel.id();
    value.remove_transaction(channel_id);
}

Function get_transaction

Get a transaction for a specific channel by the channel ID.

public fun get_transaction(self: &discovery::RelayerDiscovery, channel_id: object::ID): transaction::Transaction
Implementation
public fun get_transaction(
    self: &RelayerDiscovery,
    channel_id: ID,
): Transaction {
    let value = self.value!(b"register_transaction");
    value.get_transaction(channel_id)
}

Function version_control


Private Functions —————–

fun version_control(): version_control::VersionControl
Implementation
fun version_control(): VersionControl {
    version_control::new(vector[
        // version 0
        vector[
            b"register_transaction",
            b"remove_transaction",
            b"get_transaction",
        ].map!(|function_name| function_name.to_ascii_string()),
    ])
}