Skip to main content

Register assets

You can register assets in Postchain using the FT4 admin operation or by writing a custom operation. This section provides details on both methods.

Register assets with FT4 admin operation

To register an asset using the ft4.admin.register_asset operation, follow these steps:

  1. Ensure you have imported the admin module. If you followed the setup guide, this step is already covered.

  2. Register the asset with the following command:

    chr tx ft4.admin.register_asset TestAsset TST 6 https://url-to-asset-icon --secret .chromia/ft4-admin.keypair --await

    In this 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. Verify the asset registration with 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.

Register an asset with a custom operation

Using a custom operation allows you to register multiple assets or perform additional initialization. Here’s how to do it:

  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. Verify if the assets are successfully registered using 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 haven't completed the first part of this guide, only two objects will be present in the data array.

After registering assets, the next essential step is to handle account registration. This allows you to create new accounts within the system. Users typically register accounts using the account registration framework, while admins can leverage a special operation. Additionally, custom operations can be defined to cater to specific use cases.