-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedera_topic.go
83 lines (69 loc) · 2.02 KB
/
hedera_topic.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
package handler
import (
"github.com/gofiber/fiber/v2"
"github.com/hashgraph/hedera-sdk-go/v2"
"github.com/malahngoding/filaments/config"
)
// Createtopic api handler
func Createtopic(c *fiber.Ctx) error {
client := hedera.ClientForTestnet()
client.SetOperator(config.HederaID(), config.HederaPrivateKey())
transactionResponse, err := hedera.NewTopicCreateTransaction().
Execute(client)
if err != nil {
println(err.Error(), ": error creating topic")
return err
}
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error getting topic create receipt")
return err
}
topicID := *transactionReceipt.TopicID
return c.JSON(fiber.Map{
"messages": "Hello Future",
"payload": fiber.Map{"topicId": topicID.String()},
"status": "OK",
})
}
// SubmitMessageToTopic api handler
func SubmitMessageToTopic(c *fiber.Ctx) error {
client := hedera.ClientForTestnet()
client.SetOperator(config.HederaID(), config.HederaPrivateKey())
type ConsensusMessage struct {
TopicId string `json:"topicId" xml:"topicId" form:"topicId"`
Message string `json:"message" xml:"message" form:"message"`
Memo string `json:"memo" xml:"memo" form:"memo"`
}
sc := new(ConsensusMessage)
if err := c.BodyParser(sc); err != nil {
return err
}
hashTopicId, err := hedera.TopicIDFromString(sc.TopicId)
if err != nil {
return err
}
submitMessage, err := hedera.NewTopicMessageSubmitTransaction().
SetMessage([]byte(sc.Message)).
SetTopicID(hashTopicId).
SetTransactionMemo(sc.Memo).
Execute(client)
if err != nil {
println(err.Error(), ": error submitting to topic")
return err
}
//Get the receipt of the transaction
receipt, err := submitMessage.GetReceipt(client)
if err != nil {
println(err.Error(), ": error submitting to topic")
return err
}
transactionStatus := receipt.Status
return c.JSON(fiber.Map{
"messages": "Hello Future",
"payload": fiber.Map{
"messages": "The message transaction status " + transactionStatus.String(),
},
"status": "OK",
})
}