Register assets
Asset registration in Postchain can be done in two ways: using the FT4 admin operation or writing a custom operation. This topic provides details on how to register assets using both methods.
Registration with FT4 admin operation
To register an asset with the ft4.admin.register_asset
operation, follow these steps:
Ensure that the admin module is imported. If you have already imported
^.lib.ft4.ft4_basic_dev.*
there is no need for additional steps as it indirectly imports the admin module.Use the following command to register the asset:
chr tx ft4.admin.register_asset TestAsset TST 6 https://url-to-asset-icon --secret ft4-admin.keypair --await
In the above command:
TestAsset
is the name of the asset.TST
is the asset symbol.6
is the number of decimals for the asset.https://url-to-asset-icon
is the URL to the asset's icon.ft4-admin.keypair
is the secret key pair used for registration.
To verify if the asset is registered, execute the following command:
chr query ft4.get_all_assets -- '{page_size=10, page_cursor=null}'
If the registration is successful, the output should resemble the following:
{
data=[
{
brid=x"3E09A6E0787CE592CE517004FC76E7A67E7F820E37C2B0E64AE22B46679D588C",
decimals=6,
icon_url="https://url-to-asset-icon",
id=x"6C17294B5C28FF149315A19598FF3730B0809C83DEEE4FB3F1874CC2F8FCAAB3",
name="TestAsset",
supply=0L,
symbol="TST"
}
],
next_cursor=null
}noteThe
brid
andid
values might differ in your case.
Registering an asset with a custom operation
You can use a custom operation to register multiple assets or perform additional initialization for your dapp. Follow the steps below:
Add the following code to the
main.rell
file:operation init() {
ft4.admin.require_admin();
register_asset_if_needed("TestAsset2", "TST2", 10, "https://url-to-asset-2-icon");
register_asset_if_needed("TestAsset3", "TST3", 18, "https://url-to-asset-3-icon");
}
function register_asset_if_needed(asset_name: name, symbol: text, decimals: integer, icon_url: text) {
// derive id of the asset
val asset_id = (asset_name, chain_context.blockchain_rid).hash();
// check if asset already exist
if (ft4.assets.asset @ ? {
.id == asset_id
} ?? ) return;
ft4.assets.Unsafe.register_asset(asset_name, symbol, decimals, chain_context.blockchain_rid, icon_url);
}Deploy the updated code using the following command:
chr node update
Wait for the block with the specified height to be produced. The update command's output will mention the block height at which the new code will be activated. For example:
Configuration added at height 50
Once the new blockchain configuration is activated, execute the following command to register the additional assets:
chr tx init --secret ft4-admin.keypair --await
To verify if the assets are successfully registered, use the following command:
chr query ft4.get_all_assets -- '{page_size=10, page_cursor=null}'
The output should display the registered assets similar to the following:
{
data=[
{
brid=x"3E09A6E0787CE592CE517004FC76E7A67E7F820E37C2B0E64AE22B46679D588C",
decimals=6,
icon_url="https://url-to-asset-icon",
id=x"6C17294B5C28FF149315A19598FF3730B0809C83DEEE4FB3F1874CC2F8FCAAB3",
name="TestAsset",
supply=0L,
symbol="TST"
},
{
brid=x"3E09A6E0787CE592CE517004FC76E7A67E7F820E37C2B0E64AE22B46679D588C",
decimals=10,
icon_url="https://url-to-asset-2-icon",
id=x"AC334B78D502FD05DE1AED6129A2062C3DCDE30ED0D6BD9EF58BA588F889CF19",
name="TestAsset2",
supply=0L,
symbol="TST2"
},
{
brid=x"3E09A6E0787CE592CE517004FC76E7A67E7F820E37C2B0E64AE22B46679D588C",
decimals=18,
icon_url="https://url-to-asset-3-icon",
id=x"73C4A8E014B76B3EF3AF9131631F356DC21A6974560EA39A96E39B0F5E52533D",
name="TestAsset3",
supply=0L,
symbol="TST3"
}
],
next_cursor=null
}
If you still need to follow the first part of this guide, you'll only get two objects inside the data
array.