axelar-cgp-sui

Module 0xa5::address_tracker

Q: why addresses are stored as Strings? Q: why chains are Strings?

use 0x1::ascii;
use 0x2::table;
use 0x2::tx_context;
use 0xa5::events;

Struct InterchainAddressTracker

The interchain address tracker stores the trusted addresses for each chain.

struct InterchainAddressTracker has store
Fields
trusted_addresses: table::Table<ascii::String, ascii::String>

Constants

#[error]
const EEmptyChainName: vector<u8> = b"empty trusted chain name is unsupported";

#[error]
const EEmptyTrustedAddress: vector<u8> = b"empty trusted address is unsupported";

#[error]
const ENoAddress: vector<u8> = b"attempt to borrow a trusted address but it's not registered";

Function trusted_address

Get the trusted address for a chain.

public(friend) fun trusted_address(self: &address_tracker::InterchainAddressTracker, chain_name: ascii::String): &ascii::String
Implementation
public(package) fun trusted_address(
    self: &InterchainAddressTracker,
    chain_name: String,
): &String {
    assert!(self.trusted_addresses.contains(chain_name), ENoAddress);
    &self.trusted_addresses[chain_name]
}

Function is_trusted_address

Check if the given address is trusted for the given chain.

public(friend) fun is_trusted_address(self: &address_tracker::InterchainAddressTracker, chain_name: ascii::String, addr: ascii::String): bool
Implementation
public(package) fun is_trusted_address(
    self: &InterchainAddressTracker,
    chain_name: String,
    addr: String,
): bool {
    self.trusted_addresses.contains(chain_name) && self.trusted_addresses[chain_name] == addr
}

Function new

Create a new interchain address tracker.

public(friend) fun new(ctx: &mut tx_context::TxContext): address_tracker::InterchainAddressTracker
Implementation
public(package) fun new(ctx: &mut TxContext): InterchainAddressTracker {
    InterchainAddressTracker {
        trusted_addresses: table::new(ctx),
    }
}

Function set_trusted_address

Set the trusted address for a chain or adds it if it doesn’t exist.

public(friend) fun set_trusted_address(self: &mut address_tracker::InterchainAddressTracker, chain_name: ascii::String, trusted_address: ascii::String)
Implementation
public(package) fun set_trusted_address(
    self: &mut InterchainAddressTracker,
    chain_name: String,
    trusted_address: String,
) {
    assert!(chain_name.length() > 0, EEmptyChainName);
    assert!(trusted_address.length() > 0, EEmptyTrustedAddress);

    if (self.trusted_addresses.contains(chain_name)) {
        *&mut self.trusted_addresses[chain_name] = trusted_address;
    } else {
        self.trusted_addresses.add(chain_name, trusted_address);
    };
    events::trusted_address_set(chain_name, trusted_address);
}

Function remove_trusted_address

public(friend) fun remove_trusted_address(self: &mut address_tracker::InterchainAddressTracker, chain_name: ascii::String)
Implementation
public(package) fun remove_trusted_address(
    self: &mut InterchainAddressTracker,
    chain_name: String,
) {
    assert!(chain_name.length() > 0, EEmptyChainName);
    self.trusted_addresses.remove(chain_name);
    events::trusted_address_removed(chain_name);
}