Search code examples
metadatahedera-hashgraph

How do I set metadata for a Fungible Token on Hedera Token Service (HTS)?


I have successfully created a FungibleCommon token on Hedera Token Service (HTS), and I am able to set the memo field without any issues. However, when I attempt to set the metadata for the token, the memo field always appears to be empty when the token is created.

I do not see any errors output, so this appears to be failing silently.

What causes this?

I'm using the latest SDK, and I'm not seeing any errors in my terminal. This is how I'm currently trying to do it:

const metadata = {
    "name": "My token name",
    "image": "http://path-to-my-image"
}
async function createFungibleToken() {
    let tokenCreateTx = await new TokenCreateTransaction()
        .setTokenName("Test")
        .setTokenSymbol("TEST")
        .setMetadata(metadata) // <--- HERE
        .setTokenType(TokenType.FungibleCommon)
        .setDecimals(2)
        .setInitialSupply(10000)
        .freezeWith(client);

    let tokenCreateSign = await tokenCreateTx.sign(privKey);
    let tokenCreateSubmit = await tokenCreateSign.execute(client);
    let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
    let tokenId = tokenCreateRx.tokenId;

    console.log(`Created token with ID: ${tokenId}`);
}

Is there a specific way to structure the metadata? What am I missing in the process?


Solution

  • This is most probably related to how you are structuring your metadata. If you’re creating a fungible token, make sure your metadata follows the guidelines specified in HIP-405. This standard outlines the proper structure for fungible token metadata to ensure it’s recognised and displayed properly across platforms.

    So your metadata should be structured like this:

    {
        "description": "description of token - RECOMMENDED - max. of 500 characters",
        "smallestUnitName" : "the name of the smallest denomination of these tokens, if the name exists - OPTIONAL",
        "smallestUnitSymbol" : "the nsymbol of the smallest denomination of these tokens, if the symbol exists - OPTIONAL",
        "creator": "creator(s) - RECOMMENDED",
        "creatorDID": "DID  - OPTIONAL ",
        "admin": "admin(s) - OPTIONAL",
        "lightLogo": "IPFS CID or path to the token's light background logo file - RECOMMENDED",
        "lightLogotype": "mime type - i.e. image/jpeg - CONDITIONALLY OPTIONAL ",
        "darkLogo": "IPFS CID or path to the token's dark background logo file - RECOMMENDED",
        "darkLogotype": "mime type - i.e. image/jpeg - CONDITIONALLY OPTIONAL ",
        "website": "link to website -  OPTIONAL", 
        "discussion": "link to discussion/discord -  OPTIONAL", 
        "whitepaper": "link to whitepaper -  OPTIONAL",
        "properties": "arbitrary additional data relevant to the token - OPTIONAL"
    }
    

    Following this standard, you could create a simple metadata.json file like this:

    {
        "description": "TEST Tokens ($TEST) to test functionality."
    }
    

    And then upload this file to IPFS using a service like Pinata or web3.storage. After uploading, you will receive a CID (e.g., Qm7tgf6t8...). This CID will be used to reference your metadata in the token creation transaction. Once you have your IPFS CID, you can use it in your token creation logic like this:

    async function createFungibleToken() {
        let tokenCreateTx = await new TokenCreateTransaction()
            .setTokenName("Test")
            .setTokenSymbol("TEST")
            .setMetadata(Buffer.from("your IPFS CID")) // Replace with your IPFS CID
            .setTokenType(TokenType.FungibleCommon)
            .setDecimals(2)
            .setInitialSupply(10000)
            .setTreasuryAccountId("your-account-id")
            .freezeWith(client);
    
        let tokenCreateSign = await tokenCreateTx.sign("your-private-key");
        let tokenCreateSubmit = await tokenCreateSign.execute(client);
        let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
        let tokenId = tokenCreateRx.tokenId;
    
        console.log(`- Created token with ID: ${tokenId} \n`);
    }
    

    Make sure to replace "your IPFS CID" with the actual CID obtained from IPFS. Also please not that while it is optional to use a metadata key during token creation, if no key is specified then the metadata will be immutable, so you won't be able to modify or add it later.