0xa5::coin_management
CoinManagement
new_with_cap
new_locked
add_distributor
add_operator
take_balance
give_coin
mint
burn
set_flow_limit
set_flow_limit_internal
is_distributor
has_capability
use 0x1::option;
use 0x2::balance;
use 0x2::clock;
use 0x2::coin;
use 0x2::tx_context;
use 0xa1::channel;
use 0xa5::flow_limit;
CoinManagement
Struct that stores information about the InterchainTokenService Coin.
struct CoinManagement<T> has store
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
#[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";
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>
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,
}
}
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>
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,
}
}
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)
public fun add_distributor<T>(
self: &mut CoinManagement<T>,
distributor: address,
) {
assert!(has_capability(self), EDistributorNeedsTreasuryCap);
self.distributor.fill(distributor);
}
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)
public fun add_operator<T>(self: &mut CoinManagement<T>, operator: address) {
self.operator.fill(operator);
}
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
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
}
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>
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)
}
}
mint
public(friend) fun mint<T>(self: &mut coin_management::CoinManagement<T>, amount: u64, ctx: &mut tx_context::TxContext): coin::Coin<T>
public(package) fun mint<T>(
self: &mut CoinManagement<T>,
amount: u64,
ctx: &mut TxContext,
): Coin<T> {
self.treasury_cap.borrow_mut().mint(amount, ctx)
}
burn
public(friend) fun burn<T>(self: &mut coin_management::CoinManagement<T>, balance: balance::Balance<T>)
public(package) fun burn<T>(self: &mut CoinManagement<T>, balance: Balance<T>) {
self.treasury_cap.borrow_mut().supply_mut().decrease_supply(balance);
}
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)
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);
}
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)
public(package) fun set_flow_limit_internal<T>(
self: &mut CoinManagement<T>,
flow_limit: u64,
) {
self.flow_limit.set_flow_limit(flow_limit);
}
is_distributor
Checks if the given address is a distributor
.
public fun is_distributor<T>(self: &coin_management::CoinManagement<T>, distributor: address): bool
public fun is_distributor<T>(
self: &CoinManagement<T>,
distributor: address,
): bool {
&distributor == self.distributor.borrow()
}
has_capability
Returns true if the coin management has a TreasuryCap
.
public fun has_capability<T>(self: &coin_management::CoinManagement<T>): bool
public fun has_capability<T>(self: &CoinManagement<T>): bool {
self.treasury_cap.is_some()
}