Chromia
Start Building

13 / 16Module 3: The frontend applicationIntermediate

Part 4: Registering and minting

This lesson shows you how to mint an asset with the FT4 token standard.

You create a new asset and mint it to an account by calling the register_and_mint_asset operation, which is defined in asset_management/rell/src/main.rell.

Calling an operation requires an FT4 session. The useFtSession hook from the @chromia/react package returns the current one.

asset_management/src/hooks/token-hooks.ts
import { useFtSession } from "@chromia/react";import { publicClientConfig as clientConfig } from "@/utils/generate-client-config";interface MintTokenParams {  ticker: string;  name: string;  amount: number;}function useMintToken() {  const { data: ftAccounts } = useFtAccounts({ clientConfig });  const { data: session } = useFtSession(    ftAccounts?.length ? { clientConfig, account: ftAccounts[0] } : null,  );  const registerAsset = useCallback(    async (token: MintTokenParams) => {      if (!session) return;      try {        await session          .transactionBuilder()          .add(            op(              "register_and_mint_asset",              token.name,              token.ticker,              8,              BigInt(token.amount),              "https://cdn-icons-png.flaticon.com/512/4863/4863873.png",            ),          )          .buildAndSend();        onSuccess?.(token);      } catch (error) {        console.error(error);        onError?.(token);      }    },    [session],  );  return mintToken;}

The snippet defines a custom hook, useMintToken, which obtains the current session through useFtSession.

You pass useMintToken a MintTokenParams object with these properties:

  • ticker: the asset's ticker symbol.
  • name: the asset's name.
  • amount: how much of the asset to mint.

To build a transaction, call the session object's transactionBuilder method, then append the register_and_mint_asset operation with add.

When you call buildAndSend, the configured keystore prompts for a signature on the transaction.

Written for this site from Chromia’s public course. Code samples are the course’s own.