Chromia
Start Building

07 / 16Module 2: The blockchain dappIntermediate

Asset Registration and Minting

This lesson covers how to register a digital asset and mint its starting supply. Each account can register exactly one asset and mint an initial amount of it.

Asset Registration Operation

The operation below registers a new asset and mints it:

src/main.rell
import lib.ft4.external.assets.{ get_asset_balances };import lib.ft4.assets.{ asset, Unsafe, balance };operation register_and_mint_asset(    asset_name: text,    symbol: text,    decimals: integer,    amount: big_integer,    icon_url: text) {    val owner_account = auth.authenticate();    require(get_asset_balances(owner_account.id, 10, null).data.empty(), "One asset allowed");    functions.register_and_mint_asset(owner_account, asset_name, symbol, decimals, amount, icon_url);}

What the operation does:

  • Verifies that the caller is the account owner
  • Enforces the limit of one asset per account
  • Registers the asset and mints its initial supply

The registration and minting logic itself lives in the functions namespace:

src/main.rell
namespace functions {    function register_and_mint_asset(        owner_account: account,        asset_name: name,        symbol: text,        decimals: integer,        amount: big_integer,        icon_url: text    ) {        // Derive asset ID        val asset_id = (asset_name, chain_context.blockchain_rid).hash();        // Check if asset exists        val asset = asset @ ? { .id == asset_id };        if (not empty(asset)) return;        // Register and mint        val asset_created = Unsafe.register_asset(asset_name, symbol, decimals, chain_context.blockchain_rid, icon_url);        Unsafe.mint(owner_account, asset_created, amount);    }}

Step by step, this code:

  1. Creates a unique ID for the asset
  2. Looks for an asset that already exists
  3. Registers the new asset
  4. Mints the initial supply into the owner's account
Written for this site from Chromia’s public course. Code samples are the course’s own.