github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/consensus_pub_sub/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/hashgraph/hedera-sdk-go/v2"
     9  )
    10  
    11  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++`
    12  
    13  func main() {
    14  	var client *hedera.Client
    15  	var err error
    16  
    17  	// Retrieving network type from environment variable HEDERA_NETWORK
    18  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    19  	if err != nil {
    20  		panic(fmt.Sprintf("%v : error creating client", err))
    21  	}
    22  
    23  	// Retrieving operator ID from environment variable OPERATOR_ID
    24  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    25  	if err != nil {
    26  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    27  	}
    28  
    29  	// Retrieving operator key from environment variable OPERATOR_KEY
    30  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    31  	if err != nil {
    32  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    33  	}
    34  
    35  	// Defaults the operator account ID and key such that all generated transactions will be paid for
    36  	// by this account and be signed by this key
    37  	client.SetOperator(operatorAccountID, operatorKey)
    38  
    39  	// Make a new topic
    40  	transactionResponse, err := hedera.NewTopicCreateTransaction().
    41  		SetTransactionMemo("go sdk example create_pub_sub/main.go").
    42  		SetAdminKey(client.GetOperatorPublicKey()).
    43  		Execute(client)
    44  
    45  	if err != nil {
    46  		panic(fmt.Sprintf("%v : error creating topic", err))
    47  	}
    48  
    49  	// Get the receipt
    50  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    51  
    52  	if err != nil {
    53  		panic(fmt.Sprintf("%v : error getting topic create receipt", err))
    54  	}
    55  
    56  	// get the topic id from receipt
    57  	topicID := *transactionReceipt.TopicID
    58  
    59  	fmt.Printf("topicID: %v\n", topicID)
    60  
    61  	time.Sleep(3 * time.Second)
    62  
    63  	start := time.Now()
    64  
    65  	// Setup a mirror client to print out messages as we receive them
    66  	_, err = hedera.NewTopicMessageQuery().
    67  		// For which topic ID
    68  		SetTopicID(topicID).
    69  		// When to start
    70  		SetStartTime(time.Unix(0, 0)).
    71  		Subscribe(client, func(message hedera.TopicMessage) {
    72  			print("Received message ", message.SequenceNumber, "\r")
    73  		})
    74  
    75  	if err != nil {
    76  		panic(fmt.Sprintf("%v : error subscribing to the topic", err))
    77  	}
    78  
    79  	// Loop submit transaction with "content" as message, wait a bit to make sure it propagates
    80  	for {
    81  		_, err = hedera.NewTopicMessageSubmitTransaction().
    82  			// The message we are submitting
    83  			SetMessage([]byte(content)).
    84  			// To which topic ID
    85  			SetTopicID(topicID).
    86  			Execute(client)
    87  
    88  		if err != nil {
    89  			panic(fmt.Sprintf("%v : error submitting topic", err))
    90  		}
    91  
    92  		// Setting up how long the loop wil run
    93  		if uint64(time.Since(start).Seconds()) > 16 {
    94  			break
    95  		}
    96  
    97  		// Sleep to make sure everything propagates
    98  		time.Sleep(5 * time.Second)
    99  	}
   100  
   101  	// Clean up by deleting the topic, etc
   102  	transactionResponse, err = hedera.NewTopicDeleteTransaction().
   103  		// Which topic ID
   104  		SetTopicID(topicID).
   105  		// Making sure it works right away, without propagation, by setting the same node as topic create
   106  		SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
   107  		// Setting the max fee just in case
   108  		SetMaxTransactionFee(hedera.NewHbar(5)).
   109  		Execute(client)
   110  	if err != nil {
   111  		panic(fmt.Sprintf("%v : error deleting topic", err))
   112  	}
   113  
   114  	// Get the receipt to make sure everything went through
   115  	_, err = transactionResponse.GetReceipt(client)
   116  	if err != nil {
   117  		panic(fmt.Sprintf("%v : error getting receipt for topic deletion", err))
   118  	}
   119  }