axelar-cgp-sui

Module 0xa6::discovery

use 0x1::ascii;
use 0x1::type_name;
use 0x1::vector;
use 0x2::bcs;
use 0x2::object;
use 0x2::versioned;
use 0xa1::channel;
use 0xa1::gateway;
use 0xa5::discovery;
use 0xa5::interchain_token_service;
use 0xa5::token_id;
use 0xa6::deepbook_v3;
use 0xa6::squid;
use 0xa6::squid_v0;
use 0xa6::swap_type;
use 0xa6::transfers;
use 0xaa::discovery;
use 0xaa::transaction;
use 0xb0::version_control;

Constants

#[error]
const EInvalidSwapType: vector<u8> = b"swap type does not exist";

Function register_transaction

public fun register_transaction(squid: &squid::Squid, its: &interchain_token_service::InterchainTokenService, gateway: &gateway::Gateway, relayer_discovery: &mut discovery::RelayerDiscovery)
Implementation
public fun register_transaction(
    squid: &Squid,
    its: &InterchainTokenService,
    gateway: &Gateway,
    relayer_discovery: &mut RelayerDiscovery,
) {
    let mut squid_arg = vector[0];
    squid_arg.append(object::id(squid).id_to_bytes());

    let mut its_arg = vector[0];
    its_arg.append(object::id(its).id_to_bytes());

    let mut gateway_arg = vector[0];
    gateway_arg.append(object::id(gateway).id_to_bytes());

    let transaction = transaction::new_transaction(
        false,
        vector[
            transaction::new_move_call(
                transaction::new_function(
                    transaction::package_id<Squid>(),
                    ascii::string(b"discovery"),
                    ascii::string(b"transaction"),
                ),
                vector[squid_arg, its_arg, gateway_arg, vector[3]],
                vector[],
            ),
        ],
    );

    relayer_discovery.register_transaction(
        squid.value!(b"register_transaction").channel(),
        transaction,
    )
}

Function transaction

public fun transaction(squid: &squid::Squid, its: &interchain_token_service::InterchainTokenService, gateway: &gateway::Gateway, payload: vector<u8>): transaction::Transaction
Implementation
public fun transaction(
    squid: &Squid,
    its: &InterchainTokenService,
    gateway: &Gateway,
    payload: vector<u8>,
): Transaction {
    let (token_id, _, _, data) = interchain_token_service::discovery::interchain_transfer_info(
        payload,
    );
    let type_in = (*its.registered_coin_type(token_id)).into_string();
    let package_id = transaction::package_id<Squid>();
    let swap_data = bcs::new(data).peel_vec_vec_u8();

    let mut squid_arg = vector[0];
    squid_arg.append(object::id(squid).id_to_bytes());

    let mut its_arg = vector[0];
    its_arg.append(object::id(its).id_to_bytes());

    let mut gateway_arg = vector[0];
    gateway_arg.append(object::id(gateway).id_to_bytes());

    let swap_info_arg = vector[4, 0, 0];

    let mut move_calls = vector[
        start_swap(package_id, squid_arg, its_arg, type_in),
    ];
    swap_data.do_ref!(|data| {
        let mut bcs = bcs::new(*data);
        let swap_type = swap_type::peel(&mut bcs);

        let call = if (swap_type == swap_type::deepbook_v3()) {
            deepbook_v3::estimate_move_call(
                package_id,
                bcs,
                swap_info_arg,
            )
        } else if (swap_type == swap_type::sui_transfer()) {
            transfers::sui_estimate_move_call(
                package_id,
                bcs,
                swap_info_arg,
            )
        } else {
            assert!(swap_type == swap_type::its_transfer(), EInvalidSwapType);
            transfers::its_estimate_move_call(
                package_id,
                bcs,
                swap_info_arg,
            )
        };
        move_calls.push_back(call);
    });

    swap_data.do!(|data| {
        let mut bcs = bcs::new(data);
        let swap_type = swap_type::peel(&mut bcs);

        let call = if (swap_type == swap_type::deepbook_v3()) {
            deepbook_v3::swap_move_call(
                package_id,
                bcs,
                swap_info_arg,
                squid_arg,
            )
        } else if (swap_type == swap_type::sui_transfer()) {
            transfers::sui_transfer_move_call(
                package_id,
                bcs,
                swap_info_arg,
            )
        } else {
            assert!(swap_type == swap_type::its_transfer(), EInvalidSwapType);
            transfers::its_transfer_move_call(
                package_id,
                bcs,
                swap_info_arg,
                squid_arg,
                gateway_arg,
                its_arg,
            )
        };
        move_calls.push_back(call);
    });

    move_calls.push_back(finalize(package_id, swap_info_arg));

    transaction::new_transaction(
        true,
        move_calls,
    )
}

Function start_swap

fun start_swap(package_id: address, squid_arg: vector<u8>, its_arg: vector<u8>, type_in: ascii::String): transaction::MoveCall
Implementation
fun start_swap(
    package_id: address,
    squid_arg: vector<u8>,
    its_arg: vector<u8>,
    type_in: String,
): MoveCall {
    transaction::new_move_call(
        transaction::new_function(
            package_id,
            ascii::string(b"squid"),
            ascii::string(b"start_swap"),
        ),
        vector[squid_arg, its_arg, vector[2], vector[0, 6]],
        vector[type_in],
    )
}

Function finalize

fun finalize(package_id: address, swap_info_arg: vector<u8>): transaction::MoveCall
Implementation
fun finalize(package_id: address, swap_info_arg: vector<u8>): MoveCall {
    transaction::new_move_call(
        transaction::new_function(
            package_id,
            ascii::string(b"squid"),
            ascii::string(b"finalize"),
        ),
        vector[swap_info_arg],
        vector[],
    )
}