Skip to main content

Module sui::coin

Defines the Coin type - platform wide representation of fungible tokens and coins. Coin can be described as a secure wrapper around Balance type.

use std::address;
use std::ascii;
use std::bcs;
use std::internal;
use std::option;
use std::string;
use std::type_name;
use std::u128;
use std::vector;
use sui::accumulator;
use sui::accumulator_settlement;
use sui::address;
use sui::bag;
use sui::balance;
use sui::bcs;
use sui::config;
use sui::deny_list;
use sui::dynamic_field;
use sui::dynamic_object_field;
use sui::event;
use sui::funds_accumulator;
use sui::hash;
use sui::hex;
use sui::object;
use sui::party;
use sui::protocol_config;
use sui::table;
use sui::transfer;
use sui::tx_context;
use sui::types;
use sui::url;
use sui::vec_map;
use sui::vec_set;

Struct Coin

A coin of type T worth value. Transferable and storable

public struct Coin<phantom T> has key, store
Click to open
Fields

Struct CoinMetadata

Each Coin type T created through create_currency function will have a unique instance of CoinMetadata that stores the metadata for this coin type.

public struct CoinMetadata<phantom T> has key, store
Click to open
Fields
id: sui::object::UID
decimals: u8
Number of decimal places the coin uses.
A coin with value N and decimals D should be shown as N / 10^D.
E.g., a coin with value 7002 and decimals 3 should be displayed as 7.002.
This is metadata for display usage only.
name: std::string::String
Name for the token
symbol: std::ascii::String
Symbol for the token
description: std::string::String
Description of the token
icon_url: std::option::Option<sui::url::Url>
URL for the token logo

Struct RegulatedCoinMetadata

Similar to CoinMetadata, but created only for regulated coins that use the DenyList.
This object is always immutable.

public struct RegulatedCoinMetadata<phantom T> has key
Click to open
Fields
id: sui::object::UID
coin_metadata_object: sui::object::ID
The ID of the coin's CoinMetadata object.
deny_cap_object: sui::object::ID
The ID of the coin's DenyCap object.

Struct TreasuryCap

Capability allowing the bearer to mint and burn coins of type T. Transferable

public struct TreasuryCap<phantom T> has key, store

Struct DenyCapV2

Capability allowing the bearer to deny addresses from using the currency's coins-- immediately preventing those addresses from interacting with the coin as an input to a transaction and at the start of the next preventing them from receiving the coin.
If allow_global_pause is true, the bearer can enable a global pause that behaves as if all addresses were added to the deny list.

public struct DenyCapV2<phantom T> has key, store
Click to open
Fields

Struct CurrencyCreated

public struct CurrencyCreated<phantom T> has copy, drop
Click to open
Fields
decimals: u8

Struct DenyCap

Capability allowing the bearer to freeze addresses, preventing those addresses from interacting with the coin as an input to a transaction.

public struct DenyCap<phantom T> has key, store
Click to open
Fields

Constants

A type passed to create_supply is not a one-time witness.

const EBadWitness: u64 = 0;

Invalid arguments are passed to a function.

const EInvalidArg: u64 = 1;

Trying to split a coin more times than its balance allows.

const ENotEnough: u64 = 2;
const EGlobalPauseNotAllowed: u64 = 3;

The index into the deny list vector for the sui::coin::Coin type.

const DENY_LIST_COIN_INDEX: u64 = 0;

Function total_supply

Return the total number of T's in circulation.

public fun total_supply<T>(cap: &sui::coin::TreasuryCap<T>): u64

Function treasury_into_supply

Unwrap TreasuryCap getting the Supply.

Operation is irreversible. Supply cannot be converted into a TreasuryCap due to different security guarantees (TreasuryCap can be created only once for a type)

public fun treasury_into_supply<T>(treasury: sui::coin::TreasuryCap<T>): sui::balance::Supply<T>

Function supply_immut

Get immutable reference to the treasury's Supply.

public fun supply_immut<T>(treasury: &sui::coin::TreasuryCap<T>): &sui::balance::Supply<T>

Function supply_mut

Get mutable reference to the treasury's Supply.

public fun supply_mut<T>(treasury: &mut sui::coin::TreasuryCap<T>): &mut sui::balance::Supply<T>

Function value

Public getter for the coin's value

public fun value<T>(self: &sui::coin::Coin<T>): u64

Function balance

Get immutable reference to the balance of a coin.

public fun balance<T>(coin: &sui::coin::Coin<T>): &sui::balance::Balance<T>

Function balance_mut

Get a mutable reference to the balance of a coin.

public fun balance_mut<T>(coin: &mut sui::coin::Coin<T>): &mut sui::balance::Balance<T>

Function from_balance

Wrap a balance into a Coin to make it transferable.

public fun from_balance<T>(balance: sui::balance::Balance<T>, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<T>

Function into_balance

Destruct a Coin wrapper and keep the balance.

public fun into_balance<T>(coin: sui::coin::Coin<T>): sui::balance::Balance<T>

Function take

Take a Coin worth of value from Balance.
Aborts if value > balance.value

public fun take<T>(balance: &mut sui::balance::Balance<T>, value: u64, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<T>

Function put

Put a Coin<T> to the Balance<T>.

public fun put<T>(balance: &mut sui::balance::Balance<T>, coin: sui::coin::Coin<T>)

Function redeem_funds

Redeem a Withdrawal<Balance<T>> and create a Coin<T> from the withdrawn Balance.

public fun redeem_funds<T>(withdrawal: sui::funds_accumulator::Withdrawal<sui::balance::Balance<T>>, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<T>

Function send_funds

Send a coin to an address balance

public fun send_funds<T>(coin: sui::coin::Coin<T>, recipient: address)

Function join

Consume the coin c and add its value to self.
Aborts if c.value + self.value > U64_MAX

public entry fun join<T>(self: &mut sui::coin::Coin<T>, c: sui::coin::Coin<T>)

Function split

Split coin self to two coins, one with balance split_amount, and the remaining balance is left is self.

public fun split<T>(self: &mut sui::coin::Coin<T>, split_amount: u64, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<T>

Function divide_into_n

Split coin self into n - 1 coins with equal balances. The remainder is left in self. Return newly created coins.

public fun divide_into_n<T>(self: &mut sui::coin::Coin<T>, n: u64, ctx: &mut sui::tx_context::TxContext): vector<sui::coin::Coin<T>>

Function zero

Make any Coin with a zero value. Useful for placeholding bids/payments or preemptively making empty balances.

public fun zero<T>(ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<T>

Function destroy_zero

Destroy a coin with value zero

public fun destroy_zero<T>(c: sui::coin::Coin<T>)

Function create_currency

Create a new currency type T as and return the TreasuryCap for T to the caller. Can only be called with a one-time-witness type, ensuring that there's only one TreasuryCap per T.

public fun create_currency<T: drop>(witness: T, decimals: u8, symbol: vector<u8>, name: vector<u8>, description: vector<u8>, icon_url: std::option::Option<sui::url::Url>, ctx: &mut sui::tx_context::TxContext): (sui::coin::TreasuryCap<T>, sui::coin::CoinMetadata<T>)

Function create_regulated_currency_v2

This creates a new currency, via create_currency, but with an extra capability that allows for specific addresses to have their coins frozen. When an address is added to the deny list, it is immediately unable to interact with the currency's coin as input objects.
Additionally at the start of the next epoch, they will be unable to receive the currency's coin.
The allow_global_pause flag enables an additional API that will cause all addresses to be denied. Note however, that this doesn't affect per-address entries of the deny list and will not change the result of the "contains" APIs.

public fun create_regulated_currency_v2<T: drop>(witness: T, decimals: u8, symbol: vector<u8>, name: vector<u8>, description: vector<u8>, icon_url: std::option::Option<sui::url::Url>, allow_global_pause: bool, ctx: &mut sui::tx_context::TxContext): (sui::coin::TreasuryCap<T>, sui::coin::DenyCapV2<T>, sui::coin::CoinMetadata<T>)

Function migrate_regulated_currency_to_v2

Given the DenyCap for a regulated currency, migrate it to the new DenyCapV2 type.
All entries in the deny list will be migrated to the new format.
See create_regulated_currency_v2 for details on the new v2 of the deny list.

public fun migrate_regulated_currency_to_v2<T>(deny_list: &mut sui::deny_list::DenyList, cap: sui::coin::DenyCap<T>, allow_global_pause: bool, ctx: &mut sui::tx_context::TxContext): sui::coin::DenyCapV2<T>

Function mint

Create a coin worth value and increase the total supply in cap accordingly.

public fun mint<T>(cap: &mut sui::coin::TreasuryCap<T>, value: u64, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<T>

Function mint_balance

Mint some amount of T as a Balance and increase the total supply in cap accordingly.
Aborts if value + cap.total_supply >= U64_MAX

public fun mint_balance<T>(cap: &mut sui::coin::TreasuryCap<T>, value: u64): sui::balance::Balance<T>

Function burn

Destroy the coin c and decrease the total supply in cap accordingly.

public entry fun burn<T>(cap: &mut sui::coin::TreasuryCap<T>, c: sui::coin::Coin<T>): u64

Function deny_list_v2_add

Adds the given address to the deny list, preventing it from interacting with the specified coin type as an input to a transaction. Additionally at the start of the next epoch, the address will be unable to receive objects of this coin type.

public fun deny_list_v2_add<T>(deny_list: &mut sui::deny_list::DenyList, _deny_cap: &mut sui::coin::DenyCapV2<T>, addr: address, ctx: &mut sui::tx_context::TxContext)

Function deny_list_v2_remove

Removes an address from the deny list. Similar to deny_list_v2_add, the effect for input objects will be immediate, but the effect for receiving objects will be delayed until the next epoch.

public fun deny_list_v2_remove<T>(deny_list: &mut sui::deny_list::DenyList, _deny_cap: &mut sui::coin::DenyCapV2<T>, addr: address, ctx: &mut sui::tx_context::TxContext)

Function deny_list_v2_contains_current_epoch

Check if the deny list contains the given address for the current epoch. Denied addresses in the current epoch will be unable to receive objects of this coin type.

public fun deny_list_v2_contains_current_epoch<T>(deny_list: &sui::deny_list::DenyList, addr: address, ctx: &sui::tx_context::TxContext): bool

Function deny_list_v2_contains_next_epoch

Check if the deny list contains the given address for the next epoch. Denied addresses in the next epoch will immediately be unable to use objects of this coin type as inputs. At the start of the next epoch, the address will be unable to receive objects of this coin type.

public fun deny_list_v2_contains_next_epoch<T>(deny_list: &sui::deny_list::DenyList, addr: address): bool

Function deny_list_v2_enable_global_pause

Enable the global pause for the given coin type. This will immediately prevent all addresses from using objects of this coin type as inputs. At the start of the next epoch, all addresses will be unable to receive objects of this coin type.

public fun deny_list_v2_enable_global_pause<T>(deny_list: &mut sui::deny_list::DenyList, deny_cap: &mut sui::coin::DenyCapV2<T>, ctx: &mut sui::tx_context::TxContext)

Function deny_list_v2_disable_global_pause

Disable the global pause for the given coin type. This will immediately allow all addresses to resume using objects of this coin type as inputs. However, receiving objects of this coin type will still be paused until the start of the next epoch.

public fun deny_list_v2_disable_global_pause<T>(deny_list: &mut sui::deny_list::DenyList, deny_cap: &mut sui::coin::DenyCapV2<T>, ctx: &mut sui::tx_context::TxContext)

Function deny_list_v2_is_global_pause_enabled_current_epoch

Check if the global pause is enabled for the given coin type in the current epoch.

public fun deny_list_v2_is_global_pause_enabled_current_epoch<T>(deny_list: &sui::deny_list::DenyList, ctx: &sui::tx_context::TxContext): bool

Function deny_list_v2_is_global_pause_enabled_next_epoch

Check if the global pause is enabled for the given coin type in the next epoch.

public fun deny_list_v2_is_global_pause_enabled_next_epoch<T>(deny_list: &sui::deny_list::DenyList): bool

Function mint_and_transfer

Mint amount of Coin and send it to recipient. Invokes mint().

public entry fun mint_and_transfer<T>(c: &mut sui::coin::TreasuryCap<T>, amount: u64, recipient: address, ctx: &mut sui::tx_context::TxContext)

Function update_name

Update name of the coin in CoinMetadata

public entry fun update_name<T>(_treasury: &sui::coin::TreasuryCap<T>, metadata: &mut sui::coin::CoinMetadata<T>, name: std::string::String)

Function update_symbol

Update the symbol of the coin in CoinMetadata

public entry fun update_symbol<T>(_treasury: &sui::coin::TreasuryCap<T>, metadata: &mut sui::coin::CoinMetadata<T>, symbol: std::ascii::String)

Function update_description

Update the description of the coin in CoinMetadata

public entry fun update_description<T>(_treasury: &sui::coin::TreasuryCap<T>, metadata: &mut sui::coin::CoinMetadata<T>, description: std::string::String)

Function update_icon_url

Update the url of the coin in CoinMetadata

public entry fun update_icon_url<T>(_treasury: &sui::coin::TreasuryCap<T>, metadata: &mut sui::coin::CoinMetadata<T>, url: std::ascii::String)

Function get_decimals

public fun get_decimals<T>(metadata: &sui::coin::CoinMetadata<T>): u8

Function get_name

public fun get_name<T>(metadata: &sui::coin::CoinMetadata<T>): std::string::String

Function get_symbol

public fun get_symbol<T>(metadata: &sui::coin::CoinMetadata<T>): std::ascii::String

Function get_description

public fun get_description<T>(metadata: &sui::coin::CoinMetadata<T>): std::string::String

Function get_icon_url

public fun get_icon_url<T>(metadata: &sui::coin::CoinMetadata<T>): std::option::Option<sui::url::Url>

Function destroy_metadata

Destroy legacy CoinMetadata object

public(package) fun destroy_metadata<T>(metadata: sui::coin::CoinMetadata<T>)

Function deny_cap_id

public(package) fun deny_cap_id<T>(metadata: &sui::coin::RegulatedCoinMetadata<T>): sui::object::ID

Function new_deny_cap_v2

public(package) fun new_deny_cap_v2<T>(allow_global_pause: bool, ctx: &mut sui::tx_context::TxContext): sui::coin::DenyCapV2<T>

Function new_treasury_cap

public(package) fun new_treasury_cap<T>(ctx: &mut sui::tx_context::TxContext): sui::coin::TreasuryCap<T>

Function allow_global_pause

public(package) fun allow_global_pause<T>(cap: &sui::coin::DenyCapV2<T>): bool

Function new_coin_metadata

public(package) fun new_coin_metadata<T>(decimals: u8, name: std::string::String, symbol: std::ascii::String, description: std::string::String, icon_url: std::ascii::String, ctx: &mut sui::tx_context::TxContext): sui::coin::CoinMetadata<T>

Function update_coin_metadata

Internal function to refresh the CoinMetadata with new values in CoinRegistry borrowing.

public(package) fun update_coin_metadata<T>(metadata: &mut sui::coin::CoinMetadata<T>, name: std::string::String, symbol: std::ascii::String, description: std::string::String, icon_url: std::ascii::String)

Function supply

public fun supply<T>(treasury: &mut sui::coin::TreasuryCap<T>): &sui::balance::Supply<T>

Function create_regulated_currency

This creates a new currency, via create_currency, but with an extra capability that allows for specific addresses to have their coins frozen. Those addresses cannot interact with the coin as input objects.

public fun create_regulated_currency<T: drop>(witness: T, decimals: u8, symbol: vector<u8>, name: vector<u8>, description: vector<u8>, icon_url: std::option::Option<sui::url::Url>, ctx: &mut sui::tx_context::TxContext): (sui::coin::TreasuryCap<T>, sui::coin::DenyCap<T>, sui::coin::CoinMetadata<T>)

Function deny_list_add

Adds the given address to the deny list, preventing it from interacting with the specified coin type as an input to a transaction.

public fun deny_list_add<T>(deny_list: &mut sui::deny_list::DenyList, _deny_cap: &mut sui::coin::DenyCap<T>, addr: address, _ctx: &mut sui::tx_context::TxContext)

Function deny_list_remove

Removes an address from the deny list.
Aborts with ENotFrozen if the address is not already in the list.

public fun deny_list_remove<T>(deny_list: &mut sui::deny_list::DenyList, _deny_cap: &mut sui::coin::DenyCap<T>, addr: address, _ctx: &mut sui::tx_context::TxContext)

Function deny_list_contains

Returns true iff the given address is denied for the given coin type. It will return false if given a non-coin type.

public fun deny_list_contains<T>(deny_list: &sui::deny_list::DenyList, addr: address): bool