axelar-cgp-sui

Module 0xa5::interchain_token_service

use 0x1::ascii;
use 0x1::option;
use 0x1::string;
use 0x1::type_name;
use 0x1::vector;
use 0x2::balance;
use 0x2::clock;
use 0x2::coin;
use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::versioned;
use 0xa1::bytes32;
use 0xa1::channel;
use 0xa1::message_ticket;
use 0xa5::coin_data;
use 0xa5::coin_info;
use 0xa5::coin_management;
use 0xa5::creator_cap;
use 0xa5::interchain_token_service_v0;
use 0xa5::interchain_transfer_ticket;
use 0xa5::operator_cap;
use 0xa5::owner_cap;
use 0xa5::token_id;
use 0xa5::token_manager_type;
use 0xa5::treasury_cap_reclaimer;
use 0xaa::discovery;
use 0xaa::transaction;
use 0xb0::version_control;

Resource InterchainTokenService

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

Constants

const VERSION: u64 = 1;

const DATA_VERSION: u64 = 0;

#[error]
const EUnsupported: vector<u8> = b"legacy method is no longer supported";

Function init

fun init(ctx: &mut tx_context::TxContext)
Implementation
fun init(ctx: &mut TxContext) {
    transfer::public_transfer(
        owner_cap::create(ctx),
        ctx.sender(),
    );

    transfer::public_transfer(
        operator_cap::create(ctx),
        ctx.sender(),
    );

    transfer::public_transfer(
        creator_cap::create(ctx),
        ctx.sender(),
    );
}

Function setup

entry fun setup(creator_cap: creator_cap::CreatorCap, chain_name: ascii::String, its_hub_address: ascii::String, ctx: &mut tx_context::TxContext)
Implementation
entry fun setup(creator_cap: CreatorCap, chain_name: String, its_hub_address: String, ctx: &mut TxContext) {
    let inner = versioned::create(
        DATA_VERSION,
        interchain_token_service_v0::new(
            version_control(),
            chain_name,
            its_hub_address,
            ctx,
        ),
        ctx,
    );

    // Share the its object for anyone to use.
    transfer::share_object(InterchainTokenService {
        id: object::new(ctx),
        inner,
    });

    creator_cap.destroy();
}

Function allow_function

entry fun allow_function(self: &mut interchain_token_service::InterchainTokenService, _: &owner_cap::OwnerCap, version: u64, function_name: ascii::String)
Implementation
entry fun allow_function(self: &mut InterchainTokenService, _: &OwnerCap, version: u64, function_name: String) {
    self.value_mut!(b"allow_function").allow_function(version, function_name);
}

Function disallow_function

entry fun disallow_function(self: &mut interchain_token_service::InterchainTokenService, _: &owner_cap::OwnerCap, version: u64, function_name: ascii::String)
Implementation
entry fun disallow_function(self: &mut InterchainTokenService, _: &OwnerCap, version: u64, function_name: String) {
    self.value_mut!(b"disallow_function").disallow_function(version, function_name);
}

Function migrate_coin_metadata

Publicly freeze CoinMetadata for a token id and remove it from CoinInfo storage. Needs to be called after the v0 -> v1 upgrade

entry fun migrate_coin_metadata<T>(self: &mut interchain_token_service::InterchainTokenService, _: &operator_cap::OperatorCap, token_id: address)
Implementation
entry fun migrate_coin_metadata<T>(self: &mut InterchainTokenService, _: &OperatorCap, token_id: address) {
    self.value_mut!(b"migrate_coin_metadata").migrate_coin_metadata<T>(token_id);
}

Function migrate

This function should only be called once (checks should be made on versioned to ensure this) It upgrades the version control to the new version control.

entry fun migrate(self: &mut interchain_token_service::InterchainTokenService, _: &owner_cap::OwnerCap)
Implementation
entry fun migrate(self: &mut InterchainTokenService, _: &OwnerCap) {
    self.inner.load_value_mut<InterchainTokenService_v0>().migrate(version_control());
}

Function register_coin

Legacy function to register a coin from the given CoinInfo. @deprecated

public fun register_coin<T>(_: &mut interchain_token_service::InterchainTokenService, _: coin_info::CoinInfo<T>, _: coin_management::CoinManagement<T>): token_id::TokenId
Implementation
public fun register_coin<T>(_: &mut InterchainTokenService, _: CoinInfo<T>, _: CoinManagement<T>): TokenId {
    abort EUnsupported
}

Function register_coin_from_info

Register a coin from user supplied values. Replaces legacy function register_coin.

public fun register_coin_from_info<T>(self: &mut interchain_token_service::InterchainTokenService, name: string::String, symbol: ascii::String, decimals: u8, coin_management: coin_management::CoinManagement<T>): token_id::TokenId
Implementation
public fun register_coin_from_info<T>(
    self: &mut InterchainTokenService,
    name: std::string::String,
    symbol: ascii::String,
    decimals: u8,
    coin_management: CoinManagement<T>,
): TokenId {
    let value = self.value_mut!(b"register_coin_from_info");

    value.register_coin_from_info(name, symbol, decimals, coin_management)
}

Function register_coin_from_metadata

Register a coin from the given CoinMetadata reference. Replaces legacy function register_coin.

public fun register_coin_from_metadata<T>(self: &mut interchain_token_service::InterchainTokenService, metadata: &coin::CoinMetadata<T>, coin_management: coin_management::CoinManagement<T>): token_id::TokenId
Implementation
public fun register_coin_from_metadata<T>(
    self: &mut InterchainTokenService,
    metadata: &CoinMetadata<T>,
    coin_management: CoinManagement<T>,
): TokenId {
    let value = self.value_mut!(b"register_coin_from_metadata");

    value.register_coin_from_metadata(metadata, coin_management)
}

Function register_custom_coin

public fun register_custom_coin<T>(self: &mut interchain_token_service::InterchainTokenService, deployer: &channel::Channel, salt: bytes32::Bytes32, coin_metadata: &coin::CoinMetadata<T>, coin_management: coin_management::CoinManagement<T>, ctx: &mut tx_context::TxContext): (token_id::TokenId, option::Option<treasury_cap_reclaimer::TreasuryCapReclaimer<T>>)
Implementation
public fun register_custom_coin<T>(
    self: &mut InterchainTokenService,
    deployer: &Channel,
    salt: Bytes32,
    coin_metadata: &CoinMetadata<T>,
    coin_management: CoinManagement<T>,
    ctx: &mut TxContext,
): (TokenId, Option<TreasuryCapReclaimer<T>>) {
    let value = self.value_mut!(b"register_custom_coin");

    value.register_custom_coin(deployer, salt, coin_metadata, coin_management, ctx)
}

public fun link_coin(self: &interchain_token_service::InterchainTokenService, deployer: &channel::Channel, salt: bytes32::Bytes32, destination_chain: ascii::String, destination_token_address: vector<u8>, token_manager_type: token_manager_type::TokenManagerType, link_params: vector<u8>): message_ticket::MessageTicket
Implementation
public fun link_coin(
    self: &InterchainTokenService,
    deployer: &Channel,
    salt: Bytes32,
    destination_chain: String,
    destination_token_address: vector<u8>,
    token_manager_type: TokenManagerType,
    link_params: vector<u8>,
): MessageTicket {
    let value = self.value!(b"link_coin");

    value.link_coin(deployer, salt, destination_chain, destination_token_address, token_manager_type, link_params)
}

Function register_coin_metadata

public fun register_coin_metadata<T>(self: &interchain_token_service::InterchainTokenService, coin_metadata: &coin::CoinMetadata<T>): message_ticket::MessageTicket
Implementation
public fun register_coin_metadata<T>(self: &InterchainTokenService, coin_metadata: &CoinMetadata<T>): MessageTicket {
    let value = self.value!(b"register_coin_metadata");

    value.register_coin_metadata(coin_metadata)
}

Function deploy_remote_interchain_token

public fun deploy_remote_interchain_token<T>(self: &interchain_token_service::InterchainTokenService, token_id: token_id::TokenId, destination_chain: ascii::String): message_ticket::MessageTicket
Implementation
public fun deploy_remote_interchain_token<T>(
    self: &InterchainTokenService,
    token_id: TokenId,
    destination_chain: String,
): MessageTicket {
    let value = self.value!(b"deploy_remote_interchain_token");

    value.deploy_remote_interchain_token<T>(token_id, destination_chain)
}

Function prepare_interchain_transfer

public fun prepare_interchain_transfer<T>(token_id: token_id::TokenId, coin: coin::Coin<T>, destination_chain: ascii::String, destination_address: vector<u8>, metadata: vector<u8>, source_channel: &channel::Channel): interchain_transfer_ticket::InterchainTransferTicket<T>
Implementation
public fun prepare_interchain_transfer<T>(
    token_id: TokenId,
    coin: Coin<T>,
    destination_chain: String,
    destination_address: vector<u8>,
    metadata: vector<u8>,
    source_channel: &Channel,
): InterchainTransferTicket<T> {
    interchain_transfer_ticket::new<T>(
        token_id,
        coin.into_balance(),
        source_channel.to_address(),
        destination_chain,
        destination_address,
        metadata,
        VERSION,
    )
}

Function send_interchain_transfer

public fun send_interchain_transfer<T>(self: &mut interchain_token_service::InterchainTokenService, ticket: interchain_transfer_ticket::InterchainTransferTicket<T>, clock: &clock::Clock): message_ticket::MessageTicket
Implementation
public fun send_interchain_transfer<T>(
    self: &mut InterchainTokenService,
    ticket: InterchainTransferTicket<T>,
    clock: &Clock,
): MessageTicket {
    let value = self.value_mut!(b"send_interchain_transfer");

    value.send_interchain_transfer<T>(
        ticket,
        VERSION,
        clock,
    )
}

Function receive_interchain_transfer

public fun receive_interchain_transfer<T>(self: &mut interchain_token_service::InterchainTokenService, approved_message: channel::ApprovedMessage, clock: &clock::Clock, ctx: &mut tx_context::TxContext)
Implementation
public fun receive_interchain_transfer<T>(
    self: &mut InterchainTokenService,
    approved_message: ApprovedMessage,
    clock: &Clock,
    ctx: &mut TxContext,
) {
    let value = self.value_mut!(b"receive_interchain_transfer");

    value.receive_interchain_transfer<T>(approved_message, clock, ctx);
}

Function receive_interchain_transfer_with_data

public fun receive_interchain_transfer_with_data<T>(self: &mut interchain_token_service::InterchainTokenService, approved_message: channel::ApprovedMessage, channel: &channel::Channel, clock: &clock::Clock, ctx: &mut tx_context::TxContext): (ascii::String, vector<u8>, vector<u8>, coin::Coin<T>)
Implementation
public fun receive_interchain_transfer_with_data<T>(
    self: &mut InterchainTokenService,
    approved_message: ApprovedMessage,
    channel: &Channel,
    clock: &Clock,
    ctx: &mut TxContext,
): (String, vector<u8>, vector<u8>, Coin<T>) {
    let value = self.value_mut!(b"receive_interchain_transfer_with_data");

    value.receive_interchain_transfer_with_data<T>(
        approved_message,
        channel,
        clock,
        ctx,
    )
}

Function receive_deploy_interchain_token

public fun receive_deploy_interchain_token<T>(self: &mut interchain_token_service::InterchainTokenService, approved_message: channel::ApprovedMessage)
Implementation
public fun receive_deploy_interchain_token<T>(self: &mut InterchainTokenService, approved_message: ApprovedMessage) {
    let value = self.value_mut!(b"receive_deploy_interchain_token");

    value.receive_deploy_interchain_token<T>(approved_message);
}

public fun receive_link_coin<T>(self: &mut interchain_token_service::InterchainTokenService, approved_message: channel::ApprovedMessage)
Implementation
public fun receive_link_coin<T>(self: &mut InterchainTokenService, approved_message: ApprovedMessage) {
    let value = self.value_mut!(b"receive_link_coin");

    value.receive_link_coin<T>(approved_message);
}

Function give_unregistered_coin

public fun give_unregistered_coin<T>(self: &mut interchain_token_service::InterchainTokenService, treasury_cap: coin::TreasuryCap<T>, coin_metadata: coin::CoinMetadata<T>)
Implementation
public fun give_unregistered_coin<T>(self: &mut InterchainTokenService, treasury_cap: TreasuryCap<T>, coin_metadata: CoinMetadata<T>) {
    let value = self.value_mut!(b"give_unregistered_coin");

    value.give_unregistered_coin<T>(treasury_cap, coin_metadata);
}

Function give_unlinked_coin

This function needs to be called before receiving a link token message. It ensures the coin metadata is recorded with the ITS, and that the treasury cap is present in case of mint_burn.

public fun give_unlinked_coin<T>(self: &mut interchain_token_service::InterchainTokenService, token_id: token_id::TokenId, coin_metadata: &coin::CoinMetadata<T>, treasury_cap: option::Option<coin::TreasuryCap<T>>, ctx: &mut tx_context::TxContext): option::Option<treasury_cap_reclaimer::TreasuryCapReclaimer<T>>
Implementation
public fun give_unlinked_coin<T>(
    self: &mut InterchainTokenService,
    token_id: TokenId,
    coin_metadata: &CoinMetadata<T>,
    treasury_cap: Option<TreasuryCap<T>>,
    ctx: &mut TxContext,
): Option<TreasuryCapReclaimer<T>> {
    let value = self.value_mut!(b"give_unlinked_coin");

    value.give_unlinked_coin(token_id, coin_metadata, treasury_cap, ctx)
}

Function remove_unlinked_coin

public fun remove_unlinked_coin<T>(self: &mut interchain_token_service::InterchainTokenService, treasury_cap_reclaimer: treasury_cap_reclaimer::TreasuryCapReclaimer<T>): coin::TreasuryCap<T>
Implementation
public fun remove_unlinked_coin<T>(self: &mut InterchainTokenService, treasury_cap_reclaimer: TreasuryCapReclaimer<T>): TreasuryCap<T> {
    let value = self.value_mut!(b"remove_unlinked_coin");

    value.remove_unlinked_coin(treasury_cap_reclaimer)
}

Function mint_as_distributor

public fun mint_as_distributor<T>(self: &mut interchain_token_service::InterchainTokenService, channel: &channel::Channel, token_id: token_id::TokenId, amount: u64, ctx: &mut tx_context::TxContext): coin::Coin<T>
Implementation
public fun mint_as_distributor<T>(
    self: &mut InterchainTokenService,
    channel: &Channel,
    token_id: TokenId,
    amount: u64,
    ctx: &mut TxContext,
): Coin<T> {
    let value = self.value_mut!(b"mint_as_distributor");

    value.mint_as_distributor<T>(
        channel,
        token_id,
        amount,
        ctx,
    )
}

Function mint_to_as_distributor

public fun mint_to_as_distributor<T>(self: &mut interchain_token_service::InterchainTokenService, channel: &channel::Channel, token_id: token_id::TokenId, to: address, amount: u64, ctx: &mut tx_context::TxContext)
Implementation
public fun mint_to_as_distributor<T>(
    self: &mut InterchainTokenService,
    channel: &Channel,
    token_id: TokenId,
    to: address,
    amount: u64,
    ctx: &mut TxContext,
) {
    let value = self.value_mut!(b"mint_to_as_distributor");

    value.mint_to_as_distributor<T>(
        channel,
        token_id,
        to,
        amount,
        ctx,
    );
}

Function burn_as_distributor

public fun burn_as_distributor<T>(self: &mut interchain_token_service::InterchainTokenService, channel: &channel::Channel, token_id: token_id::TokenId, coin: coin::Coin<T>)
Implementation
public fun burn_as_distributor<T>(self: &mut InterchainTokenService, channel: &Channel, token_id: TokenId, coin: Coin<T>) {
    let value = self.value_mut!(b"mint_to_as_distributor");

    value.burn_as_distributor<T>(
        channel,
        token_id,
        coin,
    );
}

Function set_flow_limit_as_token_operator

public fun set_flow_limit_as_token_operator<T>(self: &mut interchain_token_service::InterchainTokenService, channel: &channel::Channel, token_id: token_id::TokenId, limit: option::Option<u64>)
Implementation
public fun set_flow_limit_as_token_operator<T>(
    self: &mut InterchainTokenService,
    channel: &Channel,
    token_id: TokenId,
    limit: Option<u64>,
) {
    let value = self.value_mut!(b"set_flow_limit_as_token_operator");

    value.set_flow_limit_as_token_operator<T>(
        channel,
        token_id,
        limit,
    );
}

Function set_flow_limit

public fun set_flow_limit<T>(self: &mut interchain_token_service::InterchainTokenService, _: &operator_cap::OperatorCap, token_ids: token_id::TokenId, limit: option::Option<u64>)
Implementation
public fun set_flow_limit<T>(self: &mut InterchainTokenService, _: &OperatorCap, token_ids: TokenId, limit: Option<u64>) {
    let value = self.value_mut!(b"set_flow_limit");

    value.set_flow_limit<T>(
        token_ids,
        limit,
    );
}

Function transfer_distributorship

The current distributor can use this function to update the distributorship. If an empty Option is provided then the distributor will be removed forever.

public fun transfer_distributorship<T>(self: &mut interchain_token_service::InterchainTokenService, channel: &channel::Channel, token_id: token_id::TokenId, new_distributor: option::Option<address>)
Implementation
public fun transfer_distributorship<T>(
    self: &mut InterchainTokenService,
    channel: &Channel,
    token_id: TokenId,
    new_distributor: Option<address>,
) {
    let value = self.value_mut!(b"transfer_distributorship");

    value.transfer_distributorship<T>(
        channel,
        token_id,
        new_distributor,
    );
}

Function transfer_operatorship

The current operator can use this function to update the operatorship. If an empty Option is provided then the operator will be removed forever.

public fun transfer_operatorship<T>(self: &mut interchain_token_service::InterchainTokenService, channel: &channel::Channel, token_id: token_id::TokenId, new_operator: option::Option<address>)
Implementation
public fun transfer_operatorship<T>(
    self: &mut InterchainTokenService,
    channel: &Channel,
    token_id: TokenId,
    new_operator: Option<address>,
) {
    let value = self.value_mut!(b"transfer_operatorship");

    value.transfer_operatorship<T>(
        channel,
        token_id,
        new_operator,
    );
}

Function remove_treasury_cap

This can be used by the deployer who added the treasury cap to reclaim mint/burn permission from ITS for that token_id. Doing so will render the coin unusable by ITS until restore_treasury_cap is called.

public fun remove_treasury_cap<T>(self: &mut interchain_token_service::InterchainTokenService, treasury_cap_reclaimer: treasury_cap_reclaimer::TreasuryCapReclaimer<T>): coin::TreasuryCap<T>
Implementation
public fun remove_treasury_cap<T>(self: &mut InterchainTokenService, treasury_cap_reclaimer: TreasuryCapReclaimer<T>): TreasuryCap<T> {
    let value = self.value_mut!(b"remove_treasury_cap");

    value.remove_treasury_cap<T>(treasury_cap_reclaimer)
}

Function restore_treasury_cap

This can only be called for coins that have had remove_treasury_cap called on them, to restore their functionality

public fun restore_treasury_cap<T>(self: &mut interchain_token_service::InterchainTokenService, treasury_cap: coin::TreasuryCap<T>, token_id: token_id::TokenId, ctx: &mut tx_context::TxContext): treasury_cap_reclaimer::TreasuryCapReclaimer<T>
Implementation
public fun restore_treasury_cap<T>(
    self: &mut InterchainTokenService,
    treasury_cap: TreasuryCap<T>,
    token_id: TokenId,
    ctx: &mut TxContext,
): TreasuryCapReclaimer<T> {
    let value = self.value_mut!(b"restore_treasury_cap");

    value.restore_treasury_cap<T>(
        treasury_cap,
        token_id,
        ctx,
    )
}

Function add_trusted_chains

public fun add_trusted_chains(self: &mut interchain_token_service::InterchainTokenService, _owner_cap: &owner_cap::OwnerCap, chain_names: vector<ascii::String>)
Implementation
public fun add_trusted_chains(self: &mut InterchainTokenService, _owner_cap: &OwnerCap, chain_names: vector<String>) {
    let value = self.value_mut!(b"add_trusted_chains");

    value.add_trusted_chains(chain_names);
}

Function remove_trusted_chains

public fun remove_trusted_chains(self: &mut interchain_token_service::InterchainTokenService, _owner_cap: &owner_cap::OwnerCap, chain_names: vector<ascii::String>)
Implementation
public fun remove_trusted_chains(self: &mut InterchainTokenService, _owner_cap: &OwnerCap, chain_names: vector<String>) {
    let value = self.value_mut!(b"remove_trusted_chains");

    value.remove_trusted_chains(chain_names);
}

Function registered_coin_type

public fun registered_coin_type(self: &interchain_token_service::InterchainTokenService, token_id: token_id::TokenId): &type_name::TypeName
Implementation
public fun registered_coin_type(self: &InterchainTokenService, token_id: TokenId): &TypeName {
    self.package_value().registered_coin_type(token_id)
}

Function channel_address

public fun channel_address(self: &interchain_token_service::InterchainTokenService): address
Implementation
public fun channel_address(self: &InterchainTokenService): address {
    self.package_value().channel_address()
}

Function registered_coin_data

public fun registered_coin_data<T>(self: &interchain_token_service::InterchainTokenService, token_id: token_id::TokenId): &coin_data::CoinData<T>
Implementation
public fun registered_coin_data<T>(self: &InterchainTokenService, token_id: TokenId): &CoinData<T> {
    self.package_value().coin_data<T>(token_id)
}

Function package_value

public(friend) fun package_value(self: &interchain_token_service::InterchainTokenService): &interchain_token_service_v0::InterchainTokenService_v0
Implementation
public(package) fun package_value(self: &InterchainTokenService): &InterchainTokenService_v0 {
    self.inner.load_value<InterchainTokenService_v0>()
}

Function register_transaction

public(friend) fun register_transaction(self: &mut interchain_token_service::InterchainTokenService, discovery: &mut discovery::RelayerDiscovery, transaction: transaction::Transaction)
Implementation
public(package) fun register_transaction(
    self: &mut InterchainTokenService,
    discovery: &mut RelayerDiscovery,
    transaction: Transaction,
) {
    let value = self.value_mut!(b"register_transaction");

    value.set_relayer_discovery_id(discovery);

    discovery.register_transaction(
        value.channel(),
        transaction,
    );
}

Function version_control

fun version_control(): version_control::VersionControl
Implementation
fun version_control(): VersionControl {
    version_control::new(vector[
        // Version 0
        vector[
            b"deploy_remote_interchain_token",
            b"send_interchain_transfer",
            b"receive_interchain_transfer",
            b"receive_interchain_transfer_with_data",
            b"receive_deploy_interchain_token",
            b"give_unregistered_coin",
            b"mint_as_distributor",
            b"mint_to_as_distributor",
            b"burn_as_distributor",
            b"add_trusted_chains",
            b"remove_trusted_chains",
            b"register_transaction",
            b"set_flow_limit",
            b"set_flow_limit_as_token_operator",
            b"transfer_distributorship",
            b"transfer_operatorship",
            b"allow_function",
            b"disallow_function",
        ].map!(|function_name| function_name.to_ascii_string()),
        // Version 1
        vector[
            b"register_coin_from_info",
            b"register_coin_from_metadata",
            b"register_custom_coin",
            b"link_coin",
            b"register_coin_metadata",
            b"deploy_remote_interchain_token",
            b"send_interchain_transfer",
            b"receive_interchain_transfer",
            b"receive_interchain_transfer_with_data",
            b"receive_deploy_interchain_token",
            b"receive_link_coin",
            b"give_unregistered_coin",
            b"give_unlinked_coin",
            b"remove_unlinked_coin",
            b"mint_as_distributor",
            b"mint_to_as_distributor",
            b"burn_as_distributor",
            b"add_trusted_chains",
            b"remove_trusted_chains",
            b"register_transaction",
            b"set_flow_limit",
            b"set_flow_limit_as_token_operator",
            b"transfer_distributorship",
            b"transfer_operatorship",
            b"remove_treasury_cap",
            b"restore_treasury_cap",
            b"allow_function",
            b"disallow_function",
            b"migrate_coin_metadata",
        ].map!(|function_name| function_name.to_ascii_string()),
    ])
}