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:
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:
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:
- Creates a unique ID for the asset
- Looks for an asset that already exists
- Registers the new asset
- 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.