Skip to main content

Register assets

Asset registration in Postchain can be done 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:

  1. Ensure that the admin module is imported. If you have followed the imports from Setup a project, this is already done.

  2. Use the following command to register the asset:

    chr tx ft4.admin.register_asset TestAsset TST 6 https://url-to-asset-icon --secret .chromia/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.
    • .chromia/ft4-admin.keypair is the secret key pair used for registration.
  3. To verify if the asset is registered, execute the following command:

    chr query ft4.get_all_assets page_size=10 page_cursor=null
  4. If the registration is successful, the output should resemble the following:

    [
    "data": [
    [
    "blockchain_rid": x"CCEF62BF92CED034DD41D8230D30D2818FE6C3C96BD12E55B0BEC4D02B25E5A6",
    "decimals": 6,
    "icon_url": "https://url-to-asset-icon",
    "id": x"AB423B9C0A207B9E712FB245A738B1E29C49C5C4CB77F9E0CBA4E003F95C6CAF",
    "name": "TestAsset",
    "supply": 0L,
    "symbol": "TST",
    "type": "ft4"
    ]
    ],
    "next_cursor": null
    ]
    note

    The brid and id 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:

  1. Add the following code to the main.rell file:

    operation init() {
    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();

    // cCeck if the asset already exists
    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);
    }
  2. Deploy the updated code using the following command:

    chr node update
  3. Wait for the block with the specified height to be produced. The update command's output mentions the block height at which the new code will be activated. For example:

    Configuration added at height 50
  4. Once the new blockchain configuration is activated, execute the following command to register the additional assets:

    chr tx init --secret .chromia/ft4-admin.keypair --await
  5. 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": [
    [
    "blockchain_rid": x"CCEF62BF92CED034DD41D8230D30D2818FE6C3C96BD12E55B0BEC4D02B25E5A6",
    "decimals": 6,
    "icon_url": "https://url-to-asset-icon",
    "id": x"AB423B9C0A207B9E712FB245A738B1E29C49C5C4CB77F9E0CBA4E003F95C6CAF",
    "name": "TestAsset",
    "supply": 0L,
    "symbol": "TST",
    "type": "ft4"
    ],
    [
    "blockchain_rid": x"CCEF62BF92CED034DD41D8230D30D2818FE6C3C96BD12E55B0BEC4D02B25E5A6",
    "decimals": 10,
    "icon_url": "https://url-to-asset-2-icon",
    "id": x"5D99EDF63EF87ADD04966558E575712C9984F0DDDFB7039073BDD83227C4584D",
    "name": "TestAsset2",
    "supply": 0L,
    "symbol": "TST2",
    "type": "ft4"
    ],
    [
    "blockchain_rid": x"CCEF62BF92CED034DD41D8230D30D2818FE6C3C96BD12E55B0BEC4D02B25E5A6",
    "decimals": 18,
    "icon_url": "https://url-to-asset-3-icon",
    "id": x"03AEE687F19E8BE6899E3898D407C65838B85FAB1AF479E330A41EFC98404948",
    "name": "TestAsset3",
    "supply": 0L,
    "symbol": "TST3",
    "type": "ft4"
    ]
    ],
    "next_cursor": null
    ]
note

If you did not complete the first part of this guide, you will only get two objects inside the data array.