Skip to content

Instantly share code, notes, and snippets.

@michielmulders
Last active April 15, 2023 15:45
Show Gist options
  • Save michielmulders/31f51aa1475fdadf4184cc6a7d90a889 to your computer and use it in GitHub Desktop.
Save michielmulders/31f51aa1475fdadf4184cc6a7d90a889 to your computer and use it in GitHub Desktop.
Hedera Consensus Service (code example)
OPERATOR_ID=
OPERATOR_PVKEY=
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
TopicCreateTransaction,
TopicMessageQuery,
TopicMessageSubmitTransaction,
} = require("@hashgraph/sdk")
// Configure accounts and client, and generate needed keys
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
// Create a new public topic
let txResponse = await new TopicCreateTransaction().execute(client);
// Grab the topic ID
let receipt = await txResponse.getReceipt(client);
let topicId = receipt.topicId;
console.log(`Your topic ID is: ${topicId}`)
// Wait 5 seconds between consensus topic creation and subscription creation
await new Promise((resolve) => setTimeout(resolve, 5000));
// Subscribe to the topic
new TopicMessageQuery()
.setTopicId(topicId)
.subscribe(client, (message) => {
let messageAsString = Buffer.from(message.contents, "utf8").toString();
console.log(
`${message.consensusTimestamp.toDate()} - Sequence number: ${message.sequenceNumber} - ${messageAsString}`
)
})
// Submit a message
let sendResponse = await new TopicMessageSubmitTransaction({
topicId: topicId,
message: "Hello, HCS!"
}).execute(client);
// Get the receipt of the transaction
const getReceipt = await sendResponse.getReceipt(client);
// Get the status of the transaction
const txStatus = getReceipt.status;
console.log(`Status: ${txStatus.toString()}`)
client.close();
}
main();
{
"name": "hedera-consenus-service",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Michiel Mulders",
"license": "ISC",
"dependencies": {
"@hashgraph/sdk": "^2.22.0",
"dotenv": "^16.0.3"
}
}
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
TopicMessageQuery,
} = require("@hashgraph/sdk")
// Configure accounts and client, and generate needed keys
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
let topicId = "0.0.4049400";
let consensusTimestampMsg2 = "1681383435.529633099"
// Subscribe to the topic
new TopicMessageQuery()
.setTopicId(topicId)
.setStartTime(consensusTimestampMsg2)
// .setEndTime(timestamp)
// .setLimit(10)
.subscribe(client, (message) => {
console.log(`Seq number: ${message.sequenceNumber}`)
})
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment