Overview
Basics
Token reuses the TreasuryCap
defined in the sui::coin
module and therefore has the same initialization process. The coin::create_currency
method guarantees the uniqueness of the TreasuryCap
and forces the creation of a CoinMetadata
object.
Minting and burning are performed using the Coin-like methods. Both require the TreasuryCap
:
token::mint
- mint a Tokentoken::burn
- burn a Token
See Create a Coin guide for more details on how to create a Coin.
Public Actions
Token has a set of public and protected actions which can be used to manage the token. Public actions are available to everyone and don't require any authorization. They have similar APIs to Coin's but operate on the Token
type:
token::keep
- send a Token to the transaction sendertoken::join
- join two Tokenstoken::split
- split a Token into two, specify the amount to splittoken::zero
- create an empty (zero balance) Tokentoken::destroy_zero
- destroy a Token with zero balance
See Coin Token Comparison for Coin and Token methods comparison.
Protected Actions
Protected actions are ones that issue an ActionRequest - a hot-potato struct which must be resolved for the transaction to succeed. There're 3 main ways to resolve an ActionRequest, most common of which is via the TokenPolicy.
token::transfer
- transfer a Token to a specified addresstoken::to_coin
- convert a Token to a Cointoken::from_coin
- convert a Coin to a Tokentoken::spend
- spend a Token on a specified address
The methods above are included in the base implementation, however it is possible to create ActionRequest`s for custom actions.
Token Policy & Rules
Protected actions are disabled by default and can be enabled in a TokenPolicy. Additionally, the token owner can set custom restrictions called Rules which must be satisfied for a specific action to succeed.