axelar-cgp-sui

Module 0xa5::coin_management

use 0x1::option;
use 0x2::balance;
use 0x2::clock;
use 0x2::coin;
use 0x2::tx_context;
use 0xa1::channel;
use 0xa5::flow_limit;

Struct CoinManagement

Struct that stores information about the InterchainTokenService Coin.

struct CoinManagement<T> has store
Fields
treasury_cap: option::Option<coin::TreasuryCap<T>>
balance: option::Option<balance::Balance<T>>
distributor: option::Option<address>
operator: option::Option<address>
flow_limit: flow_limit::FlowLimit
dust: u256

Constants

#[error]
const EDistributorNeedsTreasuryCap: vector<u8> = b"trying to add a distributor to a `CoinManagement` that does not have a `TreasuryCap`";

#[error]
const ENotOperator: vector<u8> = b"channel provided is not the operator";

Function new_with_cap

Create a new CoinManagement with a TreasuryCap. This type of CoinManagement allows minting and burning of coins.

public fun new_with_cap<T>(treasury_cap: coin::TreasuryCap<T>): coin_management::CoinManagement<T>
Implementation
public fun new_with_cap<T>(treasury_cap: TreasuryCap<T>): CoinManagement<T> {
    CoinManagement<T> {
        treasury_cap: option::some(treasury_cap),
        balance: option::none(),
        distributor: option::none(),
        operator: option::none(),
        flow_limit: flow_limit::new(),
        dust: 0,
    }
}

Function new_locked

Create a new CoinManagement with a Balance. The stored Balance can be used to take and put coins.

public fun new_locked<T>(): coin_management::CoinManagement<T>
Implementation
public fun new_locked<T>(): CoinManagement<T> {
    CoinManagement<T> {
        treasury_cap: option::none(),
        balance: option::some(balance::zero()),
        distributor: option::none(),
        operator: option::none(),
        flow_limit: flow_limit::new(),
        dust: 0,
    }
}

Function add_distributor

Adds the distributor address to the CoinManagement. Only works for a CoinManagement with a TreasuryCap.

public fun add_distributor<T>(self: &mut coin_management::CoinManagement<T>, distributor: address)
Implementation
public fun add_distributor<T>(
    self: &mut CoinManagement<T>,
    distributor: address,
) {
    assert!(has_capability(self), EDistributorNeedsTreasuryCap);
    self.distributor.fill(distributor);
}

Function add_operator

Adds the distributor address to the CoinManagement. Only works for a CoinManagement with a TreasuryCap.

public fun add_operator<T>(self: &mut coin_management::CoinManagement<T>, operator: address)
Implementation
public fun add_operator<T>(self: &mut CoinManagement<T>, operator: address) {
    self.operator.fill(operator);
}

Function take_balance

Takes the given amount of Coins from user. Returns the amount that the InterchainTokenService is supposed to give on other chains.

public(friend) fun take_balance<T>(self: &mut coin_management::CoinManagement<T>, to_take: balance::Balance<T>, clock: &clock::Clock): u64
Implementation
public(package) fun take_balance<T>(
    self: &mut CoinManagement<T>,
    to_take: Balance<T>,
    clock: &Clock,
): u64 {
    self.flow_limit.add_flow_out(to_take.value(), clock);
    let amount = to_take.value();
    if (has_capability(self)) {
        self.burn(to_take);
    } else {
        self.balance.borrow_mut().join(to_take);
    };
    amount
}

Function give_coin

Withdraws or mints the given amount of coins. Any leftover amount from previous transfers is added to the coin here.

public(friend) fun give_coin<T>(self: &mut coin_management::CoinManagement<T>, amount: u64, clock: &clock::Clock, ctx: &mut tx_context::TxContext): coin::Coin<T>
Implementation
public(package) fun give_coin<T>(
    self: &mut CoinManagement<T>,
    amount: u64,
    clock: &Clock,
    ctx: &mut TxContext,
): Coin<T> {
    self.flow_limit.add_flow_in(amount, clock);
    if (has_capability(self)) {
        self.mint(amount, ctx)
    } else {
        coin::take(self.balance.borrow_mut(), amount, ctx)
    }
}

Function mint

public(friend) fun mint<T>(self: &mut coin_management::CoinManagement<T>, amount: u64, ctx: &mut tx_context::TxContext): coin::Coin<T>
Implementation
public(package) fun mint<T>(
    self: &mut CoinManagement<T>,
    amount: u64,
    ctx: &mut TxContext,
): Coin<T> {
    self.treasury_cap.borrow_mut().mint(amount, ctx)
}

Function burn

public(friend) fun burn<T>(self: &mut coin_management::CoinManagement<T>, balance: balance::Balance<T>)
Implementation
public(package) fun burn<T>(self: &mut CoinManagement<T>, balance: Balance<T>) {
    self.treasury_cap.borrow_mut().supply_mut().decrease_supply(balance);
}

Function set_flow_limit

Adds a rate limit to the CoinManagement.

public(friend) fun set_flow_limit<T>(self: &mut coin_management::CoinManagement<T>, channel: &channel::Channel, flow_limit: u64)
Implementation
public(package) fun set_flow_limit<T>(
    self: &mut CoinManagement<T>,
    channel: &Channel,
    flow_limit: u64,
) {
    assert!(self.operator.contains(&channel.to_address()), ENotOperator);
    self.set_flow_limit_internal(flow_limit);
}

Function set_flow_limit_internal

Adds a rate limit to the CoinManagement.

public(friend) fun set_flow_limit_internal<T>(self: &mut coin_management::CoinManagement<T>, flow_limit: u64)
Implementation
public(package) fun set_flow_limit_internal<T>(
    self: &mut CoinManagement<T>,
    flow_limit: u64,
) {
    self.flow_limit.set_flow_limit(flow_limit);
}

Function is_distributor

Checks if the given address is a distributor.

public fun is_distributor<T>(self: &coin_management::CoinManagement<T>, distributor: address): bool
Implementation
public fun is_distributor<T>(
    self: &CoinManagement<T>,
    distributor: address,
): bool {
    &distributor == self.distributor.borrow()
}

Function has_capability

Returns true if the coin management has a TreasuryCap.

public fun has_capability<T>(self: &coin_management::CoinManagement<T>): bool
Implementation
public fun has_capability<T>(self: &CoinManagement<T>): bool {
    self.treasury_cap.is_some()
}