0xa5::address_tracker
Q: why addresses are stored as Strings? Q: why chains are Strings?
InterchainAddressTracker
trusted_address
is_trusted_address
new
set_trusted_address
remove_trusted_address
use 0x1::ascii;
use 0x2::table;
use 0x2::tx_context;
use 0xa5::events;
InterchainAddressTracker
The interchain address tracker stores the trusted addresses for each chain.
struct InterchainAddressTracker has store
#[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";
trusted_address
Get the trusted address for a chain.
public(friend) fun trusted_address(self: &address_tracker::InterchainAddressTracker, chain_name: ascii::String): &ascii::String
public(package) fun trusted_address(
self: &InterchainAddressTracker,
chain_name: String,
): &String {
assert!(self.trusted_addresses.contains(chain_name), ENoAddress);
&self.trusted_addresses[chain_name]
}
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
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
}
new
Create a new interchain address tracker.
public(friend) fun new(ctx: &mut tx_context::TxContext): address_tracker::InterchainAddressTracker
public(package) fun new(ctx: &mut TxContext): InterchainAddressTracker {
InterchainAddressTracker {
trusted_addresses: table::new(ctx),
}
}
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)
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);
}
remove_trusted_address
public(friend) fun remove_trusted_address(self: &mut address_tracker::InterchainAddressTracker, chain_name: ascii::String)
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);
}