-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCreateTopicDemo.java
More file actions
77 lines (60 loc) · 3.08 KB
/
CreateTopicDemo.java
File metadata and controls
77 lines (60 loc) · 3.08 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
import com.hedera.hashgraph.sdk.*;
import java.net.http.*;
import java.net.URI;
import com.google.gson.*;
public class CreateTopicDemo {
public static void main(String[] args ) throws Exception {
// .env-provided
AccountId operatorId = AccountId.fromString(System.getenv("OPERATOR_ID"));
PrivateKey operatorKey = PrivateKey.fromStringECDSA(System.getenv("OPERATOR_KEY"));
String network = System.getenv().getOrDefault("HEDERA_NETWORK", "local");
String mirrorNode = System.getenv().getOrDefault(
"MIRROR_NODE_URL",
"http://localhost:38081/api/v1"
);
if (operatorId == null || operatorKey == null) {
throw new IllegalStateException("OPERATOR_ID / OPERATOR_KEY not set");
}
Client client =
"local".equalsIgnoreCase(network)
? Client.forNetwork(java.util.Map.of("127.0.0.1:50211", new AccountId(3)))
: Client.forTestnet();
client.setOperator(operatorId, operatorKey);
// build & execute the topic creation transaction
TopicCreateTransaction transaction = new TopicCreateTransaction()
.setTopicMemo("My first HCS topic");
TransactionResponse txResponse = transaction.execute(client);
TransactionReceipt receipt = txResponse.getReceipt(client);
TopicId topicId = receipt.topicId;
System.out.println("\nTopic created: " + topicId);
// build & execute the message submission transaction
String message = "Hello, Hedera!";
TopicMessageSubmitTransaction messageTransaction = new TopicMessageSubmitTransaction()
.setTopicId(topicId)
.setMessage(message);
messageTransaction.execute(client);
System.out.println("\nMessage submitted: " + message);
// wait for Mirror Node to populate data
System.out.println("\nWaiting for Mirror Node to update...");
Thread.sleep(10000);
// query messages using Mirror Node
String mirrorNodeUrl = mirrorNode + "/topics/" + topicId + "/messages";
HttpClient httpClient = HttpClient.newHttpClient( );
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(mirrorNodeUrl))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString( ));
Gson gson = new Gson();
JsonObject data = gson.fromJson(response.body(), JsonObject.class);
if (data.has("messages") && data.getAsJsonArray("messages").size() > 0) {
JsonArray messages = data.getAsJsonArray("messages");
JsonObject latestMessage = messages.get(messages.size() - 1).getAsJsonObject();
String encodedMessage = latestMessage.get("message").getAsString();
String messageContent = new String(java.util.Base64.getDecoder().decode(encodedMessage)).trim();
System.out.println("\nLatest message: " + messageContent + "\n");
} else {
System.out.println("No messages found yet in Mirror Node");
}
client.close();
}
}