-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
66 lines (50 loc) · 2.28 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { AccountId, Client, Hbar, PrivateKey, TopicCreateTransaction, TopicMessageQuery, TopicMessageSubmitTransaction } from "@hashgraph/sdk";
require('dotenv').config();
// let myAccountId: AccountId;
// let myPrivateKey: PrivateKey;
//Grab your Hedera testnet account ID and private key from your .env file
const myAccountId = AccountId.fromString(process.env.TEST_ACCOUNT_ID ?? "");
const myPrivateKey = PrivateKey.fromString(process.env.TEST_PRIVATE_KEY ?? "");
// If we weren't able to grab it, we should throw a new error
if (!myAccountId || !myPrivateKey) {
throw new Error("Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present");
}
console.log("Your account ID: " + myAccountId.toString());
console.log("Your private key: " + myPrivateKey.toString());
//Create your Hedera Testnet client
const client = Client.forTestnet();
//Set your account as the client's operator
client.setOperator(myAccountId, myPrivateKey);
//Set the default maximum transaction fee (in Hbar)
client.setDefaultMaxTransactionFee(new Hbar(100));
//Set the maximum payment for queries (in Hbar)
client.setMaxQueryPayment(new Hbar(50));
// Create a new topic
let txResponse = await new TopicCreateTransaction().execute(client);
// Grab the newly generated topic ID
const receipt = await txResponse.getReceipt(client);
const 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
if (topicId !== null) {
new TopicMessageQuery()
.setTopicId(topicId)
.subscribe(client, null, (message) => {
let messageAsString = Buffer.from(message.contents).toString();
console.log(
`${message.consensusTimestamp.toDate()} Received: ${messageAsString}`
);
});
// Send message to the topic
let sendResponse = await new TopicMessageSubmitTransaction({
topicId: topicId,
message: "Hello, HCS! Execute order 66.",
}).execute(client);
// Get the receipt of the transaction
const getReceipt = await sendResponse.getReceipt(client);
// Get the status of the transaction
const transactionStatus = getReceipt.status;
console.log("The message transaction status " + transactionStatus.toString());
}