|
| 1 | +import com.hedera.hashgraph.sdk.*; |
| 2 | +import java.net.http.HttpClient; |
| 3 | +import java.net.http.HttpRequest; |
| 4 | +import java.net.http.HttpResponse; |
| 5 | +import java.net.URI; |
| 6 | +import com.google.gson.Gson; |
| 7 | +import com.google.gson.JsonObject; |
| 8 | +import com.google.gson.JsonArray; |
| 9 | + |
| 10 | +public class CreateTopicDemo { |
| 11 | + public static void main(String[] args ) throws Exception { |
| 12 | + // load your operator credentials |
| 13 | + String operatorId = System.getenv("OPERATOR_ID"); |
| 14 | + String operatorKey = System.getenv("OPERATOR_KEY"); |
| 15 | + |
| 16 | + // initialize the client for testnet |
| 17 | + Client client = Client.forTestnet() |
| 18 | + .setOperator(AccountId.fromString(operatorId), PrivateKey.fromString(operatorKey)); |
| 19 | + |
| 20 | + // build & execute the topic creation transaction |
| 21 | + TopicCreateTransaction transaction = new TopicCreateTransaction() |
| 22 | + .setTopicMemo("My first HCS topic"); |
| 23 | + |
| 24 | + TransactionResponse txResponse = transaction.execute(client); |
| 25 | + TransactionReceipt receipt = txResponse.getReceipt(client); |
| 26 | + TopicId topicId = receipt.topicId; |
| 27 | + |
| 28 | + System.out.println("\nTopic created: " + topicId); |
| 29 | + |
| 30 | + // build & execute the message submission transaction |
| 31 | + String message = "Hello, Hedera!"; |
| 32 | + |
| 33 | + TopicMessageSubmitTransaction messageTransaction = new TopicMessageSubmitTransaction() |
| 34 | + .setTopicId(topicId) |
| 35 | + .setMessage(message); |
| 36 | + |
| 37 | + messageTransaction.execute(client); |
| 38 | + |
| 39 | + System.out.println("\nMessage submitted: " + message); |
| 40 | + |
| 41 | + // wait for Mirror Node to populate data |
| 42 | + System.out.println("\nWaiting for Mirror Node to update..."); |
| 43 | + Thread.sleep(6000); |
| 44 | + |
| 45 | + // query messages using Mirror Node |
| 46 | + String mirrorNodeUrl = "https://testnet.mirrornode.hedera.com/api/v1/topics/" + topicId + "/messages"; |
| 47 | + |
| 48 | + HttpClient httpClient = HttpClient.newHttpClient( ); |
| 49 | + HttpRequest request = HttpRequest.newBuilder() |
| 50 | + .uri(URI.create(mirrorNodeUrl)) |
| 51 | + .build(); |
| 52 | + |
| 53 | + HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString( )); |
| 54 | + Gson gson = new Gson(); |
| 55 | + JsonObject data = gson.fromJson(response.body(), JsonObject.class); |
| 56 | + |
| 57 | + if (data.has("messages") && data.getAsJsonArray("messages").size() > 0) { |
| 58 | + JsonArray messages = data.getAsJsonArray("messages"); |
| 59 | + JsonObject latestMessage = messages.get(messages.size() - 1).getAsJsonObject(); |
| 60 | + String encodedMessage = latestMessage.get("message").getAsString(); |
| 61 | + String messageContent = new String(java.util.Base64.getDecoder().decode(encodedMessage)).trim(); |
| 62 | + |
| 63 | + System.out.println("\nLatest message: " + messageContent + "\n"); |
| 64 | + } else { |
| 65 | + System.out.println("No messages found yet in Mirror Node"); |
| 66 | + } |
| 67 | + |
| 68 | + client.close(); |
| 69 | + } |
| 70 | +} |
0 commit comments