Alva

Alva

programmer
github
telegram
email

Let's Move - Learn Move Sui (2): Complete the On-chain Deployment of the Coin Contract

letsmove

Let's Move is a Move learning incentive program for SUI, encouraging more people to learn the Move language.

Learning Log (alva-lin)

Task 2 - Complete the on-chain deployment of two Coin contracts (mainnet)#

Tasks

  • Complete the study of Coin-related knowledge

  • Complete the learning of My Coin and deploy it on the mainnet

  • Complete the learning of Faucet Coin and deploy it on the mainnet

  • Submit the package id for the My Coin and Faucet Coin contracts

  • Send My Coin to the address 0x7b8e0864967427679b4e129f79dc332a885c6087ec9e187b53451a9006ee15f2

Mainly refer to the article Managed Coin Case - Introduction to Sui Move

sui::coin Library#

Coin#

The Coin contract is primarily written using the structures and methods provided by the sui::coin library.

Coin has key and store capabilities, is considered an asset, and can be transferred between different addresses. Later, all Coins owned can be viewed in the block explorer.

create_currency#

In the parameters of the create_currency method

ParameterDescription
decimalsPrecision, the smallest unit that can be divided. 10^(-1*n)
symbolSymbol
nameName
descriptionDescription
icon_urlImage

The return value of the method is a tuple containing two values, TreasuryCap and CoinMetadata,

where TreasuryCap is an asset that is guaranteed to be a singleton object through the one-time witness pattern, and its type declaration is as follows

This total_supply value tracks the total issuance of the current currency T, so only one TreasuryCap is needed. The CoinMetadata simply stores the metadata of the current currency.

Coin Contract Code#

Thanks to the completeness of the sui framework library, the code required to create a token contract with mint (minting) and burn (destroying) functionalities is minimal.

The my_coin module first defines a structure named MY_COIN based on the Witness pattern. Then, in the init method parameters, a MY_COIN type witness resource is added, which is automatically created after the module is pushed.

In the init method, the coin::create_currency method is called to obtain the TreasuryCap and CoinMetadata resources, and the CoinMetadata is immediately frozen.

The TreasuryCap, as a credential to control the calling of the mint and burn methods, is sent to the publisher's address.

Testing#

The test code is as follows, simulating a multi-transaction scenario to perform mint and burn operations.

Deployment#

First, switch the network to the desired environment.

In the project folder, execute the following commands to publish the package or publish the module individually.

The output after publishing includes

  • Package: located in the Object Changes > Published Objects block

  • CoinMetadata: located in the Object Changes > Created Objects block

    Its ObjectType is 0x2::coin::CoinMetadata<<Package ID>::<Module>::<Witness Type>>

  • TreasuryCap: located in the Object Changes > Created Objects

    Its ObjectType is 0x2::coin::TreasuryCap<<Package ID>::<Module>::<Witness Type>>

Record the Package ID and TreasuryCap ID:

Minting and Burning#

Use the sui cli to call the corresponding module functions.

Note that the desired token amount = input value * 10^(-1*n), where n is the value of decimals in the previously published contract code.

In this article, the value of decimals is 2. If you want to mint 100 tokens, you need to input 10000.

In the output, you can see the transaction details.

Alternatively, you can obtain the transaction hash and check the transaction details on SuiScan or other sui block explorers.


In the output during minting, find the Object Changes > Created Objects block where the ObjectType is 0x2::coin::Coin<<package>, <module>, <type>>, and obtain the ObjectID.

This ObjectID is the CoinID, which should be stored as a variable.

If you cannot find the coin id in the command line output, you can also find it from the block explorer.

how to find coin id

Call the burn method to destroy the currency.

After the command executes successfully, you can see that the previously minted amount has been deleted from the wallet.

After the token is destroyed, it cannot be directly searched using the COIN ID in the block explorer; it can only be found indirectly from historical transactions.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.