Your First Coin
This tutorial introduces how you can compile, deploy, and mint your own coin, named MoonCoin.
Step 1: Pick an SDK
Install your preferred SDK from the below list:
Step 2: Install the CLI
Install the precompiled binary for the Aptos CLI.
Step 3: Run the example
Clone the aptos-core
repo:
Typescript
Python
Rust
Navigate to the TypeScript SDK directory:
Install the necessary dependencies:
Run the TypeScript your_coin
example:
Step 3.1: Build the package
The example run will pause with the following output:
At this point, open another terminal and change directories to the MoonCoin package's directory:
Next, build the package using the CLI:
The --named-addresses
is a list of address mappings that must be translated in order for the package to be compiled to be stored in Alice's account. Notice how MoonCoin
is set to Alice's address printed above. Also --save-metadata
is required to publish the package.
Step 3.2: Completing the example
Returning to the previous prompt, press ENTER as the package is now ready to be published.
The application will complete, printing:
Step 4: MoonCoin in depth
Step 4.1: Building and publishing the MoonCoin package
Move contracts are effectively a set of Move modules known as a package. When deploying or upgrading a new package, the compiler must be invoked with --save-metadata
to publish the package. In the case of MoonCoin, the following output files are critical:
build/Examples/package-metadata.bcs
: Contains the metadata associated with the package.build/Examples/bytecode_modules/moon_coin.mv
: Contains the bytecode for themoon_coin.move
module.
These are read by the example and published to the Aptos blockchain:
Typescript
Python
Rust
Step 4.2: Understanding the MoonCoin module
The MoonCoin module defines the MoonCoin
struct, or the distinct type of coin type. In addition, it contains a function called init_module
. The init_module
function is called when the module is published. In this case, MoonCoin initializes the MoonCoin
coin type as a ManagedCoin
, which is maintained by the owner of the account.
MANAGEDCOIN FRAMEWORK
ManagedCoin
is a simple coin management framework for coins directly managed by users. It provides convenience wrappers around mint
and burn
.
Step 4.3: Understanding coins
Coins have several primitives:
Minting: Creating new coins.
Burning: Deleting coins.
Freezing: Preventing an account from storing coins in
CoinStore
.Registering: Creating a
CoinStore
resource on an account for storing coins.Transferring: Withdrawing and depositing coins into
CoinStore
.
TIP
The entity that creates a new coin gains the capabilities for minting, burning, and freezing.
In order to transfer, withdraw, or deposit coins, you must have a CoinStore
registered for the specific coin. In this tutorial, this is CoinStore<MoonCoin>
.
Step 4.3.1: Initializing a coin
Once a coin type has been published to the Aptos blockchain, the entity that published that coin type can initialize it:
This ensures that this coin type has never been initialized before. Notice the check on lines 10 and 15 to ensure that the caller to initialize
is the same one that actually published this module, and that there is no CoinInfo
stored on their account. If both those conditions check, then a CoinInfo
is stored and the caller obtains capabilities for burning, freezing, and minting.
TIP
MoonCoin calls this initialize
function automatically upon package publishing.
Step 4.3.2: Registering a coin
To use a coin, an entity must register a CoinStore
for it on their account:
As this is a public fun
and not a public entry fun
, coins will need to provide their own means for registering or users can construct Move scripts
to call the function.
MoonCoin uses ManagedCoin
that provides an entry function wrapper: managed_coin::register
.
Step 4.3.3: Minting a coin
Minting coins requires the mint capability that was produced during initialization. the function mint
(see below) takes in that capability and an amount, and returns back a Coin<T>
struct containing that amount of coins. If the coin tracks supply, it will be updated.
ManagedCoin
makes this easier by providing a entry function managed_coin::mint
.
Step 4.3.4: Transferring a coin
Aptos provides several building blocks to support coin transfers:
coin::deposit<CoinType>
: Allows any entity to deposit a coin into an account that has already calledcoin::register<CoinType>
.coin::withdraw<CoinType>
: Allows any entity to extract a coin amount from their account.coin::transfer<CoinType>
: Leverages withdraw and deposit to perform an end-to-end transfer.
IMPORTANT
Aptos does not emit transfer events, but instead it leverages withdraw and deposit events.
Last updated