-
Notifications
You must be signed in to change notification settings - Fork 65
/
main.go
119 lines (95 loc) · 3.48 KB
/
main.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"os"
"time"
"github.com/hashgraph/hedera-sdk-go/v2"
)
const content = `Programming is the process of creating a set of instructions that tell a computer how to perform a task. Programming can be done using a variety of computer programming languages, such as JavaScript, Python, and C++`
func main() {
var client *hedera.Client
var err error
// Retrieving network type from environment variable HEDERA_NETWORK
client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
if err != nil {
panic(fmt.Sprintf("%v : error creating client", err))
}
// Retrieving operator ID from environment variable OPERATOR_ID
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(fmt.Sprintf("%v : error converting string to AccountID", err))
}
// Retrieving operator key from environment variable OPERATOR_KEY
operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
}
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.SetOperator(operatorAccountID, operatorKey)
// Make a new topic
transactionResponse, err := hedera.NewTopicCreateTransaction().
SetTransactionMemo("go sdk example create_pub_sub/main.go").
SetAdminKey(client.GetOperatorPublicKey()).
Execute(client)
if err != nil {
panic(fmt.Sprintf("%v : error creating topic", err))
}
// Get the receipt
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
panic(fmt.Sprintf("%v : error getting topic create receipt", err))
}
// get the topic id from receipt
topicID := *transactionReceipt.TopicID
fmt.Printf("topicID: %v\n", topicID)
time.Sleep(3 * time.Second)
start := time.Now()
// Setup a mirror client to print out messages as we receive them
_, err = hedera.NewTopicMessageQuery().
// For which topic ID
SetTopicID(topicID).
// When to start
SetStartTime(time.Unix(0, 0)).
Subscribe(client, func(message hedera.TopicMessage) {
print("Received message ", message.SequenceNumber, "\r")
})
if err != nil {
panic(fmt.Sprintf("%v : error subscribing to the topic", err))
}
// Loop submit transaction with "content" as message, wait a bit to make sure it propagates
for {
_, err = hedera.NewTopicMessageSubmitTransaction().
// The message we are submitting
SetMessage([]byte(content)).
// To which topic ID
SetTopicID(topicID).
Execute(client)
if err != nil {
panic(fmt.Sprintf("%v : error submitting topic", err))
}
// Setting up how long the loop wil run
if uint64(time.Since(start).Seconds()) > 16 {
break
}
// Sleep to make sure everything propagates
time.Sleep(5 * time.Second)
}
// Clean up by deleting the topic, etc
transactionResponse, err = hedera.NewTopicDeleteTransaction().
// Which topic ID
SetTopicID(topicID).
// Making sure it works right away, without propagation, by setting the same node as topic create
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
// Setting the max fee just in case
SetMaxTransactionFee(hedera.NewHbar(5)).
Execute(client)
if err != nil {
panic(fmt.Sprintf("%v : error deleting topic", err))
}
// Get the receipt to make sure everything went through
_, err = transactionResponse.GetReceipt(client)
if err != nil {
panic(fmt.Sprintf("%v : error getting receipt for topic deletion", err))
}
}