Skip to content

Latest commit

 

History

History
688 lines (315 loc) · 20.1 KB

coin.md

File metadata and controls

688 lines (315 loc) · 20.1 KB

Module 0x3::coin

This module provides the foundation for typesafe Coins.

Struct Coin

Main structure representing a coin. Note the CoinType must have key ability. if the CoinType has store ability, the Coin is a public coin, the user can operate it directly by coin module's function. Otherwise, the Coin is a private coin, the user can only operate it by CoinType module's function. The Coin has no ability, it is a hot potato type, only can handle by Coin module.

struct Coin<CoinType: key>

Resource CoinInfo

Information about a specific coin type. Stored in the global Object storage. CoinInfo is a named Object, the coin_type is the unique key.

struct CoinInfo<CoinType: key> has store, key

Resource CoinMetadata

Coin metadata is copied from CoinInfo, and stored as dynamic field of CoinRegistry

struct CoinMetadata has store, key

Resource CoinRegistry

The registry of all coin types.

struct CoinRegistry has key

Struct MintEvent

Event emitted when coin minted.

struct MintEvent has copy, drop, store

Struct BurnEvent

Event emitted when coin burned.

struct BurnEvent has copy, drop, store

Constants

Maximum possible aggregatable coin value.

const MAX_U64: u128 = 18446744073709551615;

Maximum possible coin supply.

const MAX_U128: u128 = 340282366920938463463374607431768211455;

const MAX_U256: u256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935;

CoinType is already registered as a coin

CoinType is not registered as a coin

Global CoinInfos should exist

const ErrorCoinInfosNotFound: u64 = 8;

Name of the coin is too long

const ErrorCoinNameTooLong: u64 = 6;

CoinRegister is already initialized

Symbol of the coin is too long

const ErrorCoinSymbolTooLong: u64 = 7;

The function is deprecated

const ErrorDeprecated: u64 = 10;

Cannot destroy non-zero coins

const ErrorDestroyOfNonZeroCoin: u64 = 4;

Not enough coins to extract

const ErrorInsufficientBalance: u64 = 3;

Coin amount cannot be zero

const ErrorZeroCoinAmount: u64 = 5;

const MAX_COIN_NAME_LENGTH: u64 = 32;

const MAX_COIN_SYMBOL_LENGTH: u64 = 10;

Function genesis_init

public(friend) fun genesis_init(__genesis_account: &signer)

Function init_coin_registry

Initialize the CoinRegistry, this function is for framework upgrade.

entry fun init_coin_registry()

Function coin_address

A helper function that returns the address of CoinType.

public fun coin_address<CoinType: key>(): address

Function check_coin_info_registered

A helper function that check the CoinType is registered, if not, abort.

public fun check_coin_info_registered<CoinType: key>()

Function is_registered

Returns true if the type CoinType is an registered coin.

public fun is_registered<CoinType: key>(): bool

Function coin_info_id

Return the ObjectID of Object<CoinInfo>

public fun coin_info_id<CoinType: key>(): object::ObjectID

Function name

Returns the name of the coin.

public fun name<CoinType: key>(coin_info: &coin::CoinInfo<CoinType>): string::String

Function name_by_type

Returns the name of the coin by the type CoinType

public fun name_by_type<CoinType: key>(): string::String

Function symbol

Returns the symbol of the coin, usually a shorter version of the name.

public fun symbol<CoinType: key>(coin_info: &coin::CoinInfo<CoinType>): string::String

Function symbol_by_type

Returns the symbol of the coin by the type CoinType

public fun symbol_by_type<CoinType: key>(): string::String

Function decimals

Returns the number of decimals used to get its user representation. For example, if decimals equals 2, a balance of 505 coins should be displayed to a user as 5.05 (505 / 10 ** 2).

public fun decimals<CoinType: key>(coin_info: &coin::CoinInfo<CoinType>): u8

Function decimals_by_type

Returns the decimals of the coin by the type CoinType

public fun decimals_by_type<CoinType: key>(): u8

Function supply

Returns the amount of coin in existence.

public fun supply<CoinType: key>(coin_info: &coin::CoinInfo<CoinType>): u256

Function supply_by_type

Returns the amount of coin in existence by the type CoinType

public fun supply_by_type<CoinType: key>(): u256

Function icon_url

Returns the icon url of coin.

public fun icon_url<CoinType: key>(coin_info: &coin::CoinInfo<CoinType>): option::Option<string::String>

Function icon_url_by_type

Returns the icon url of coin by the type CoinType

public fun icon_url_by_type<CoinType: key>(): option::Option<string::String>

Function is_same_coin

Return true if the type CoinType1 is same with CoinType2

public fun is_same_coin<CoinType1, CoinType2>(): bool

Function destroy_zero

Destroys a zero-value coin. Calls will fail if the value in the passed-in coin is non-zero so it is impossible to "burn" any non-zero amount of Coin.

public fun destroy_zero<CoinType: key>(zero_coin: coin::Coin<CoinType>)

Function extract

Extracts amount from the passed-in coin, where the original coin is modified in place.

public fun extract<CoinType: key>(coin: &mut coin::Coin<CoinType>, amount: u256): coin::Coin<CoinType>

Function extract_all

Extracts the entire amount from the passed-in coin, where the original coin is modified in place.

public fun extract_all<CoinType: key>(coin: &mut coin::Coin<CoinType>): coin::Coin<CoinType>

Function merge

"Merges" the two given coins. The coin passed in as dst_coin will have a value equal to the sum of the two coins (dst_coin and source_coin).

public fun merge<CoinType: key>(dst_coin: &mut coin::Coin<CoinType>, source_coin: coin::Coin<CoinType>)

Function value

Returns the value passed in coin.

public fun value<CoinType: key>(coin: &coin::Coin<CoinType>): u256

Function zero

Create a new Coin<CoinType> with a value of 0.

public fun zero<CoinType: key>(): coin::Coin<CoinType>

Function coin_info

Borrow the CoinInfo

public fun coin_info<CoinType: key>(): &coin::CoinInfo<CoinType>

Function upsert_icon_url

This function is protected by private_generics, so it can only be called by the CoinType module.

#[private_generics(#[CoinType])]
public fun upsert_icon_url<CoinType: key>(coin_info_obj: &mut object::Object<coin::CoinInfo<CoinType>>, icon_url: string::String)

Function register_extend

Creates a new Coin with given CoinType This function is protected by private_generics, so it can only be called by the CoinType module.

#[private_generics(#[CoinType])]
public fun register_extend<CoinType: key>(name: string::String, symbol: string::String, icon_url: option::Option<string::String>, decimals: u8): object::Object<coin::CoinInfo<CoinType>>

Function init_metadata

This function for the old code to initialize the CoinMetadata

public fun init_metadata<CoinType: key>(coin_info: &object::Object<coin::CoinInfo<CoinType>>)

Function mint

Public coin can mint by anyone with the mutable Object<CoinInfo>

public fun mint<CoinType: store, key>(coin_info: &mut object::Object<coin::CoinInfo<CoinType>>, amount: u256): coin::Coin<CoinType>

Function mint_extend

Mint new Coin, this function is only called by the CoinType module, for the developer to extend custom mint logic

#[private_generics(#[CoinType])]
public fun mint_extend<CoinType: key>(coin_info: &mut object::Object<coin::CoinInfo<CoinType>>, amount: u256): coin::Coin<CoinType>

Function burn

Public coin can burn by anyone with the mutable Object<CoinInfo>

public fun burn<CoinType: store, key>(coin_info: &mut object::Object<coin::CoinInfo<CoinType>>, coin: coin::Coin<CoinType>)

Function burn_extend

Burn coin This function is only called by the CoinType module, for the developer to extend custom burn logic

#[private_generics(#[CoinType])]
public fun burn_extend<CoinType: key>(coin_info: &mut object::Object<coin::CoinInfo<CoinType>>, coin: coin::Coin<CoinType>)

Function unpack

public(friend) fun unpack<CoinType: key>(coin: coin::Coin<CoinType>): u256

Function pack

public(friend) fun pack<CoinType: key>(value: u256): coin::Coin<CoinType>