-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTopicExample.js
63 lines (48 loc) · 1.56 KB
/
createTopicExample.js
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
const {
Client,
PrivateKey,
Hbar,
TopicCreateTransaction,
TopicMessageSubmitTransaction,
TopicMessageQuery,
} = require('@hashgraph/sdk');
const dotenv = require('dotenv');
dotenv.config({ path: '.env' });
async function createTopicExample() {
const myAccountId = process.env.OPERATOR_ACCOUNT_ID;
// using the DER private key
const myPrivateKey = process.env.OPERATOR_ACCOUNT_PRIVATE_KEY_DER;
if (!myAccountId || !myPrivateKey) {
throw new Error('Environment variables not found');
} else {
console.log('Account found successfully');
console.log('Account ID', myAccountId);
console.log('Private Key', myPrivateKey);
}
// Now we have a valid operator
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
// setting hbar limits
client.setDefaultMaxTransactionFee(new Hbar(100));
client.setDefaultMaxQueryPayment(new Hbar(50));
// transaction time
const transaction = new TopicCreateTransaction();
const txResponse = await transaction.execute(client);
const receipt = await txResponse.getReceipt(client);
const newTopicId = receipt.topicId;
console.log('the new topic ID is ' + newTopicId);
await new TopicMessageSubmitTransaction({
topicId: newTopicId,
message: 'Hello World!',
}).execute(client);
// // getting topic messages
// new TopicMessageQuery()
// .setTopicId(newTopicId)
// .setStartTime(0)
// .subscribe(
// client,
// (message) => console.log(Buffer.from(message.contents, 'utf8').toString())
// );
client.close();
}
createTopicExample();