This tutorial will walk you through a TopicCreateTransaction to create a new consensus topic and a TopicMessageSubmitTransaction to publish your first message using the Hedera Consensus Service (HCS). The service provides developers a decentralized tool for logging, ordering, and timestamping messages or transactions on the Hedera network.
What you will accomplish
By the end of this tutorial, you will be able to:
Create new consensus topics using HCS.
Publish your first message using HCS.
Query the transaction via Mirror Node API.
View your transaction on a Mirror Node Explorer.
Prerequisites
Before you begin, you should have completed the following tutorials:
Step 1: Navigate to the hcs example in the project directory
From the root directory of the hedera-future-world-language project CD (change directories) to the topic create transaction example.
cdhcs
If you completed a previous example in the series you can use to go back to the root directory and cd into this example.
cd../hcs
If you want to get back to the root directory, you can CD out from any directory with this command
cd../
You can follow along through the code walkthrough or skip ahead to execute the program here.
Step 2: Guided Code Walkthrough
Open the HCS create a topic script (e.g., /hcs/script-hcs-topic.js) in a code editor llike VS Code, IntelliJ, or a Gitpod instance.. The imports at the top include modules for interacting with the Hedera network via the SDK. The @hashgraph/sdk enables account management and transactions like creating a topic while the dotenv package loads environment variables from the .env file, such as the operator account ID, private key, and name variables.
script-hcs-topic.js
import { Client, PrivateKey, AccountId, TokenCreateTransaction, TokenType,} from'@hashgraph/sdk';import dotenv from'dotenv';import { createLogger,} from'../util/util.js';constlogger=awaitcreateLogger({ scriptId:'htsFt', scriptCategory:'task',});let client;asyncfunctionscriptHtsFungibleToken() {logger.logStart('Hello Future World - HTS Fungible Token - start');// Read in environment variables from `.env` file in parent directorydotenv.config({ path:'../.env' });logger.log('Read .env file');// Initialise the operator accountconstoperatorIdStr=process.env.OPERATOR_ACCOUNT_ID;constoperatorKeyStr=process.env.OPERATOR_ACCOUNT_PRIVATE_KEY;if (!operatorIdStr ||!operatorKeyStr) {thrownewError('Must set OPERATOR_ACCOUNT_ID and OPERATOR_ACCOUNT_PRIVATE_KEY environment variables'); }constoperatorId=AccountId.fromString(operatorIdStr);constoperatorKey=PrivateKey.fromStringECDSA(operatorKeyStr); client =Client.forTestnet().setOperator(operatorId, operatorKey);logger.log('Using account:', operatorIdStr);}
To set up your Hedera Testnet client, create the client and configure the operator using your Testnet account ID and private key. The operator account covers transaction and query fees in HBAR, with all transactions requiring a signature from the operator's private key for authorization.
script-hcs-topic.js
// The client operator ID and key is the account that will be automatically set to pay for the transaction fees for each transactionclient =Client.forTestnet().setOperator(operatorId, operatorKey);//Set the default maximum transaction fee (in Hbar)client.setDefaultMaxTransactionFee(newHbar(100));//Set the maximum payment for queries (in Hbar)client.setDefaultMaxQueryPayment(newHbar(50));
Client client =Client.forTestnet().setOperator(operatorId, operatorKey);//Set the default maximum transaction fee (in HBAR)client.setDefaultMaxTransactionFee(newHbar(100));//Set the default maximum payment for queries (in HBAR)client.setDefaultMaxQueryPayment(newHbar(50));
script-hcs-topic.go
// The client operator ID and key is the account that will be automatically set to pay for the transaction fees for each transactionclient := hedera.ClientForTestnet()client.SetOperator(operatorId, operatorKey)// Set the default maximum transaction fee (in HBAR)client.SetDefaultMaxTransactionFee(hedera.HbarFrom(100, hedera.HbarUnits.Hbar))// Set the default maximum payment for queries (in HBAR)client.SetDefaultMaxQueryPayment(hedera.HbarFrom(50, hedera.HbarUnits.Hbar))
To avoid encountering the INSUFFICIENT_TX_FEE error while executing transactions, you can also specify the maximum transaction fee limit through the .setDefaultMaxTransactionFee() method and the maximum query payment through the .setDefaultMaxQueryPayment() method to control costs, ensuring your client operates within your desired financial limits on the Hedera Testnet.
π¨ How to resolve the INSUFFICIENT_TX_FEE error
To resolve this error, you must adjust the max transaction fee to a higher value suitable for your needs.
Here is a simple example addition to your code:
Copy
constmaxTransactionFee=newHbar(XX); // replace XX with desired fee in Hbar
In this example, you can set maxTransactionFee to any value greater than 5 HBAR (or 500,000,000 tinybars) to avoid the "INSUFFICIENT_TX_FEE" error for transactions greater than 5 HBAR. Please replace XX with the desired value.
To implement this new max transaction fee, you use the setDefaultMaxTransactionFee() method as shown below:
To create your first topic, you will use the TopicCreateTransaction(), set its properties, and submit it to the Hedera network. In this guide, you will create a public topic by not setting any properties on the topic. This means that anyone can send messages to your topic. Refer to the transaction and query fees table for the full list of base transaction fees.
After the transaction is completed, you'll need to extract the topicID from the transaction receipt, as it's required for submitting messages to the topic. This step has already been completed for you, no further modification is necessary.
script-hcs-topic.js
// Create a new HCS topicconsttopicCreateTx=awaitnewTopicCreateTransaction().setTopicMemo(`Hello Future World topic - ${logger.version}`)// Freeze the transaction to prepare for signing.freezeWith(client);// Get the transaction ID of the transaction.// The SDK automatically generates and assigns a transaction ID when the transaction is createdconsttopicCreateTxId=topicCreateTx.transactionId;logger.log("The topic create transaction ID: ",topicCreateTxId.toString());// Sign the transaction with the account key that will be paying for this transactionconsttopicCreateTxSigned=awaittopicCreateTx.sign(operatorKey);// Submit the transaction to the Hedera TestnetconsttopicCreateTxSubmitted=awaittopicCreateTxSigned.execute(client);// Get the transaction receiptconsttopicCreateTxReceipt=awaittopicCreateTxSubmitted.getReceipt(client);// Get the topic IDconsttopicId=topicCreateTxReceipt.topicId;logger.log('topicId:',topicId.toString());
ScriptHcsTopic.java
// Create a Hedera Consensus Service (HCS) topicTopicCreateTransaction topicCreateTx =newTopicCreateTransaction().setTopicMemo("Hello Future World topic - xyz")// Freeze the transaction to prepare for signing.freezeWith(client);// Get the transaction ID of the transaction.// The SDK automatically generates and assigns a transaction ID when the transaction is createdTransactionId topicCreateTxId =topicCreateTx.getTransactionId();System.out.println("The topic create transaction ID: "+topicCreateTxId.toString());// Sign the transaction with the account key that will be paying for this transactionTopicCreateTransaction topicCreateTxSigned =topicCreateTx.sign(operatorKey);// Submit the transaction to the Hedera TestnetTransactionResponse topicCreateTxSubmitted =topicCreateTxSigned.execute(client);// Get the transaction receiptTransactionReceipt topicCreateTxReceipt =topicCreateTxSubmitted.getReceipt(client);// Get the topic IDTopicId topicId =topicCreateTxReceipt.topicId;System.out.println("Topic ID:"+topicId.toString());
script-hcs-topic.go
// Create a Hedera Consensus Service (HCS) topictopicCreateTx, _ := hedera.NewTopicCreateTransaction().SetTopicMemo("Hello Future World topic - xyz").// Freeze the transaction to prepare for signingFreezeWith(client);// Get the transaction ID of the transaction.// The SDK automatically generates and assigns a transaction ID when the transaction is createdtopicCreateTxId := topicCreateTx.GetTransactionID();fmt.Printf("The topic create transaction ID: %s\n", topicCreateTxId.String())// Sign the transaction with the private key of the treasury account (operator key)topicCreateTxSigned := topicCreateTx.Sign(operatorKey)// Submit the transaction to the Hedera TestnettopicCreateTxSubmitted, _ := topicCreateTxSigned.Execute(client)// Get the transaction receipttopicCreateTxReceipt, _ := topicCreateTxSubmitted.GetReceipt(client)// Get the token IDtopicId := topicCreateTxReceipt.TopicIDfmt.Printf("Topic ID: %s\n", topicId.String())
Publish a Message to the New Topic
Now you are ready to submit your first message to the topic. You will use the TopicMessageSubmitTransaction() class to submit your first message to the topic. This involves setting the topicId and the message content, signing the transaction, and submitting it to the Hedera network. Messages can be any data up to 1024 bytes, and each message costs $0.0001 to send. This means you can send 10,000 messages for just $1 on the Hedera network. For the message content, use a string in the format "Hello HCS!".
script-hcs-topic.js
// Publish a message to the HCS topicconsttopicMsgSubmitTx=awaitnewTopicMessageSubmitTransaction()//Set the transaction memo with the hello future world ID.setTransactionMemo(`Hello Future World topic message - ${logger.version}`).setTopicId(topicId)//Set the topic message contents.setMessage('Hello HCS!')// Freeze the transaction to prepare for signing.freezeWith(client);// Get the transaction ID of the transaction. The SDK automatically generates and assigns a transaction ID when the transaction is createdconsttopicMsgSubmitTxId=topicMsgSubmitTx.transactionId;logger.log('The message submit create transaction ID: ',topicMsgSubmitTxId.toString());// Sign the transaction with the account key that will be paying for this transactionconsttopicMsgSubmitTxSigned=awaittopicMsgSubmitTx.sign(operatorKey);// Submit the transaction to the Hedera TestnetconsttopicMsgSubmitTxSubmitted=awaittopicMsgSubmitTxSigned.execute(client);// Get the transaction receiptconsttopicMsgSubmitTxReceipt=awaittopicMsgSubmitTxSubmitted.getReceipt(client);// Get the topic message sequence numberconsttopicMsgSeqNum=topicMsgSubmitTxReceipt.topicSequenceNumber;logger.log('topicMsgSeqNum:',topicMsgSeqNum.toString());
ScriptHcsTopic.java
// Publish a message to the Hedera Consensus Service (HCS) topicSystem.out.println("π£ Publish message to HCS topic");TopicMessageSubmitTransaction topicMsgSubmitTx =newTopicMessageSubmitTransaction()//Set the transaction memo with the hello future world ID.setTransactionMemo("Hello Future World topic message - xyz").setTopicId(topicId)//Set the topic message contents.setMessage("Hello HCS!")// Freeze the transaction to prepare for signing.freezeWith(client);// Get the transaction ID of the transaction.// The SDK automatically generates and assigns a transaction ID when the transaction is createdTransactionId topicMsgSubmitTxId =topicMsgSubmitTx.getTransactionId();System.out.println("The topic message submit transaction ID: "+topicMsgSubmitTxId.toString() );// Sign the transaction with the account key that will be paying for this transactionTopicMessageSubmitTransaction topicMsgSubmitTxSigned =topicMsgSubmitTx.sign(operatorKey);// Submit the transaction to the Hedera TestnetTransactionResponse topicMsgSubmitTxSubmitted =topicMsgSubmitTxSigned.execute(client);// Get the transaction receiptTransactionReceipt topicMsgSubmitTxReceipt =topicMsgSubmitTxSubmitted.getReceipt(client);// Get the topic message sequence numberLong topicMsgSeqNum =topicMsgSubmitTxReceipt.topicSequenceNumber;System.out.println("Topic Message Sequence Number:"+topicMsgSeqNum.toString());
script-hcs-topic.go
// Publish a message to the Hedera Consensus Service (HCS) topictopicMsgSubmitTx, _ := hedera.NewTopicMessageSubmitTransaction().//Set the transaction memo with the hello future world IDSetTransactionMemo("Hello Future World topic message - xyz").SetTopicID(*topicId).// Set the topic message contentsSetMessage([]byte("Hello HCS!")).// Freeze the transaction to prepare for signingFreezeWith(client);// Get the transaction ID of the transaction.// The SDK automatically generates and assigns a transaction ID when the transaction is createdtopicMsgSubmitTxId := topicMsgSubmitTx.GetTransactionID();fmt.Printf("The topic message submit transaction ID: %s\n", topicMsgSubmitTxId.String())// Sign the transaction with the private key of the treasury account (operator key)topicMsgSubmitTxSigned := topicMsgSubmitTx.Sign(operatorKey)// Submit the transaction to the Hedera TestnettopicMsgSubmitTxSubmitted, _ := topicMsgSubmitTxSigned.Execute(client)// Get the transaction receipttopicMsgSubmitTxReceipt, _ := topicMsgSubmitTxSubmitted.GetReceipt(client)// Get the topic message sequence numbertopicMsgSeqNum := topicMsgSubmitTxReceipt.TopicSequenceNumberfmt.Printf("Topic Message Sequence Number: %v\n", topicMsgSeqNum)
The total cost to create a topic and send a message to it is approximately $0.0101, making it an affordable way to interact with the Hedera network.
Query the Topic Data from the Mirror Node API
To query the specified topic ID and verify the messages within the topic, you will want to use the Mirror Node API with the path /api/v1/topics/{topicId}/messages for this task.
Specify topicId within the URL path
Specify base64 as the encoding query parameter
Specify 5 as the limit query parameter
Specify asc as the order query parameter
Specify 1 as the sequencenumber query parameter
The constructed topicVerifyMirrorNodeApiUrl should look like this:
You can perform the same Mirror Node API query as topicVerifyMirrorNodeApiUrl above. This is what the relevant part of the Swagger page would look like when doing so:
β‘ You can learn more about the Mirror Nodes via its documentation: REST API.
Step 3: Run and Verify Topic on HashScan
In the terminal, cd into the hcs directory and run the topic create transaction script:
To verify that both the TopicCreateTransaction and TopicMessageSubmitTransaction have been successful, check the transaction details on HashScan. Open View the topic on HashScan URL from the console output in your browser and check that:
The topic exists, and its topic ID matches topicId output by the script. (1)
There is one entry in the topic, and its message is Hello HCS - followed by your name/ nickname. (2)