Register assets with a custom operation
The registration of an asset is required to create the corresponding asset record on the blockchain. This record contains basic information about the asset: asset name, ticker, decimals and the icon URL.
This process relies on the FT4 library containing the built-in operations for registering assets. It is important to note, that the registration doesn't mean that the asset is minted on the blockchain, in fact the minting must be conducted using a separate operation.
In the example below, we create a new operation with custom arguments that uses the admin.require_admin()
and
assets.Unsafe.register_asset()
operations from the FT4 library to register a new asset. To ensure that the asset we
want to register doesn’t already exist, we implement search logic using the asset_name
. If the asset doesn’t exist, we
register it; otherwise, nothing happens.
module;
import lib.ft4.admin;
import lib.ft4.core.assets;
operation register_asset(asset_name: name, symbol: text, decimals: integer, icon_url: text) {
admin.require_admin();
val asset_id = (asset_name, chain_context.blockchain_rid).hash();
val asset = assets.asset @ ? { .id == asset_id };
if (not empty(asset)) return;
assets.Unsafe.register_asset(asset_name, symbol, decimals, chain_context.blockchain_rid, icon_url);
}
For a complete code example, refer to the Cookbook repository