Module sui::balance
A storable handler for Balances in general. Is used in the Coin module to allow balance operations and can be used to implement custom coins with Supply and Balances.
- Struct
Supply
- Struct
Balance
- Constants
- Function
value
- Function
supply_value
- Function
create_supply
- Function
increase_supply
- Function
decrease_supply
- Function
zero
- Function
join
- Function
split
- Function
withdraw_all
- Function
destroy_zero
- Function
create_staking_rewards
- Function
destroy_storage_rebates
- Function
destroy_supply
use std::address;
use std::ascii;
use std::option;
use std::type_name;
use std::vector;
use sui::tx_context;
Struct Supply
A Supply of T. Used for minting and burning. Wrapped into a TreasuryCap in the Coin module.
public struct SupplyT has store
Fields
- value: u64
Struct Balance
Storable balance - an inner struct of a Coin type. Can be used to store coins which don't need the key ability.
public struct BalanceT has store
Fields
- value: u64
Constants
For when trying to destroy a non-zero balance.
const ENonZero: u64 = 0;
For when trying to withdraw more than there is.
const ENotEnough: u64 = 2;
System operation performed for a coin other than SUI
const ENotSUI: u64 = 4;
Sender is not @0x0 the system address.
const ENotSystemAddress: u64 = 3;
For when an overflow is happening on Supply operations.
const EOverflow: u64 = 1;
const SUI_TYPE_NAME: vector<u8> = vector[48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 50, 58, 58, 115, 117, 105, 58, 58, 83, 85, 73];
Function value
Get the amount stored in a Balance.
public fun valueT(self: &sui::balance::Balance<T>): u64
Function supply_value
Get the Supply value.
public fun supply_valueT(supply: &sui::balance::Supply<T>): u64
Implementation
public fun supply_value<T>(supply: &Supply<T>): u64 {
supply.value
}
Function create_supply
Create a new supply for type T.
public fun create_supplyT(_: T): sui::balance::Supply<T>
Implementation
public fun create_supply<T: drop>(_: T): Supply<T> {
Supply { value: 0 }
}
Function increase_supply
Increase supply by value and create a new Balance<T> with this value.
public fun increase_supplyT(self: &mut sui::balance::Supply<T>, value: u64): sui::balance::Balance<T>
Function decrease_supply
Burn a Balance
public fun decrease_supplyT(self: &mut sui::balance::Supply<T>, balance: sui::balance::Balance<T>): u64
Function zero
Create a zero Balance for type T.
public fun zeroT(): sui::balance::Balance<T>
Function join
Join two balances together.
public fun joinT(self: &mut sui::balance::Balance<T>, balance: sui::balance::Balance<T>): u64
Function split
Split a Balance and take a sub balance from it.
public fun splitT(self: &mut sui::balance::Balance<T>, value: u64): sui::balance::Balance<T>
Function withdraw_all
Withdraw all balance. After this the remaining balance must be 0.
public fun withdraw_allT(self: &mut sui::balance::Balance<T>): sui::balance::Balance<T>
Function destroy_zero
Destroy a zero Balance.
public fun destroy_zeroT(balance: sui::balance::Balance<T>)
Function create_staking_rewards
CAUTION: this function creates a Balance without increasing the supply. It should only be called by the epoch change system txn to create staking rewards, and nowhere else.
fun create_staking_rewardsT(value: u64, ctx: &sui::tx_context::TxContext): sui::balance::Balance<T>
Implementation
fun create_staking_rewards<T>(value: u64, ctx: &TxContext): Balance<T> {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
assert!(std::type_name::get<T>().into_string().into_bytes() == SUI_TYPE_NAME, ENotSUI);
Balance { value }
}
Function destroy_storage_rebates
CAUTION: this function destroys a Balance without decreasing the supply. It should only be called by the epoch change system txn to destroy storage rebates, and nowhere else.
fun destroy_storage_rebatesT(self: sui::balance::Balance<T>, ctx: &sui::tx_context::TxContext)
Implementation
fun destroy_storage_rebates<T>(self: Balance<T>, ctx: &TxContext) {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
assert!(std::type_name::get<T>().into_string().into_bytes() == SUI_TYPE_NAME, ENotSUI);
let Balance { value: _ } = self;
}
Function destroy_supply
Destroy a Supply preventing any further minting and burning.
public(package) fun destroy_supplyT(self: sui::balance::Supply<T>): u64