Skip to content

Commit

Permalink
fix: fixing examples to work with sdk v2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 authored and janaakhterov committed Nov 4, 2020
1 parent 59aaef2 commit a3c17a2
Show file tree
Hide file tree
Showing 20 changed files with 351 additions and 235 deletions.
4 changes: 2 additions & 2 deletions account_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func NewAccountCreateTransaction() *AccountCreateTransaction {

// SetKey sets the key that must sign each transfer out of the account. If RecieverSignatureRequired is true, then it
// must also sign any transfer into the account.
func (transaction *AccountCreateTransaction) SetKey(publicKey PublicKey) *AccountCreateTransaction {
func (transaction *AccountCreateTransaction) SetKey(key Key) *AccountCreateTransaction {
transaction.requireNotFrozen()
transaction.pb.Key = publicKey.toProtoKey()
transaction.pb.Key = key.toProtoKey()
return transaction
}

Expand Down
6 changes: 3 additions & 3 deletions client-config-with-operator.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"1.testnet.hedera.com:50211" : "0.0.4",
"2.testnet.hedera.com:50211" : "0.0.5",
"3.testnet.hedera.com:50211" : "0.0.6"
},ill
},
"operator": {
"accountId": "0.0.56313",
"privateKey": "302e020100300506032b657004220420c581ebedb27097be2e22b4df5a2117fdc1c1e41ac7b43ece2eff5acfa6973739"
"accountId": "0.0.3",
"privateKey": "302e020100300506032b65700422eff5acfa6973739"
}
}
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (client *Client) SetMaxQueryPayment(payment Hbar) *Client {
// be returned.
func (client *Client) Ping(nodeID AccountID) error {
_, err := NewAccountBalanceQuery().
SetAccountID(client.GetOperatorID()).
SetNodeAccountIDs([]AccountID{nodeID}).
Execute(client)
return err
Expand Down
102 changes: 65 additions & 37 deletions examples/consensus_pub_sub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,40 @@ import (
"github.com/hashgraph/hedera-sdk-go"
)

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() {
client := hedera.ClientForTestnet()
var client *hedera.Client
var err error

operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
if os.Getenv("HEDERA_NETWORK") == "previewnet" {
client = hedera.ClientForPreviewnet()
} else {
client, err = hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = hedera.ClientForTestnet()
}
}

operatorPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")

if err != nil {
panic(err)
}
if configOperatorID != "" && configOperatorKey != "" {
operatorAccountID, err := hedera.AccountIDFromString(configOperatorID)
if err != nil {
panic(err)
}

client.SetOperator(operatorAccountID, operatorPrivateKey)
operatorKey, err := hedera.PrivateKeyFromString(configOperatorKey)
if err != nil {
panic(err)
}

client.SetOperator(operatorAccountID, operatorKey)
}

transactionID, err := hedera.NewTopicCreateTransaction().
transactionResponse, err := hedera.NewTopicCreateTransaction().
SetTransactionMemo("go sdk example create_pub_sub/main.go").
// SetMaxTransactionFee(hedera.HbarFrom(8, hedera.HbarUnits.Hbar)).
Execute(client)
Expand All @@ -33,7 +50,7 @@ func main() {
panic(err)
}

transactionReceipt, err := transactionID.GetReceipt(client)
transactionReceipt, err := transactionResponse.GetReceipt(client)

if err != nil {
panic(err)
Expand All @@ -43,47 +60,58 @@ func main() {

fmt.Printf("topicID: %v\n", topicID)

mirrorNodeAddress := os.Getenv("MIRROR_NODE_ADDRESS")
wait := true
start := time.Now()

_, err = hedera.NewTopicMessageQuery().
SetTopicID(topicID).
SetStartTime(time.Unix(0, 0)).
Subscribe(client, func(message hedera.TopicMessage) {
if string(message.Contents) == content {
wait = false
}
})

mirrorClient := hedera.ClientForTestnet()
mirrorClient.SetMirrorNetwork([]string{mirrorNodeAddress})
if err != nil {
panic(err)
}

_, err = hedera.NewMirrorConsensusTopicQuery().
messageQuery, err := hedera.NewTopicMessageSubmitTransaction().
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
SetMessage([]byte(content)).
SetTopicID(topicID).
Subscribe(
mirrorClient,
func(resp hedera.MirrorConsensusTopicResponse) {
fmt.Printf("received message: %v\n", string(resp.Message))
},
func(err error) {
fmt.Println(err.Error())
})

FreezeWith(client)
if err != nil {
panic(err)
}

for i := 0; true; i++ {
id, err := hedera.NewConsensusMessageSubmitTransaction().
SetTopicID(topicID).
SetMessage([]byte(fmt.Sprintf("Hello HCS from Go! Message %v", i))).
Execute(client)
println(string(messageQuery.GetMessage()))

if err != nil {
panic(err)
messageQuery.Execute(client)

for {
if !wait || uint64(time.Since(start).Seconds()) > 30 {
break
}

_, err = id.GetReceipt(client)
time.Sleep(2500)
}

if err != nil {
panic(err)
}
transactionResponse, err = hedera.NewTopicDeleteTransaction().
SetTopicID(topicID).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
SetMaxTransactionFee(hedera.NewHbar(5)).
Execute(client)
if err != nil {
panic(err)
}

fmt.Printf("Sent Message %v\n", i)
_, err = transactionResponse.GetReceipt(client)
if err != nil {
panic(err)
}

time.Sleep(2500 * time.Millisecond)
if wait {
panic("Message was not received within 30 seconds")
}
}
89 changes: 63 additions & 26 deletions examples/consensus_pub_sub_chunked/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,111 @@ import (
)

func main() {
client := hedera.ClientForTestnet()
var client *hedera.Client
var err error

operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
}
if os.Getenv("HEDERA_NETWORK") == "previewnet" {
client = hedera.ClientForPreviewnet()
} else {
client, err = hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE"))

operatorPrivateKey, err := hedera.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(err)
if err != nil {
client = hedera.ClientForTestnet()
}
}

client.SetOperator(operatorAccountID, operatorPrivateKey)
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")

if configOperatorID != "" && configOperatorKey != "" {
operatorAccountID, err := hedera.AccountIDFromString(configOperatorID)
if err != nil {
panic(err)
}

transactionID, err := hedera.NewConsensusTopicCreateTransaction().
operatorKey, err := hedera.PrivateKeyFromString(configOperatorKey)
if err != nil {
panic(err)
}

client.SetOperator(operatorAccountID, operatorKey)
}

transactionResponse, err := hedera.NewTopicCreateTransaction().
SetTransactionMemo("go sdk example create_pub_sub_chunked/main.go").
Execute(client)

if err != nil {
panic(err)
}

transactionReceipt, err := transactionID.GetReceipt(client)
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
panic(err)
}

topicID := transactionReceipt.GetConsensusTopicID()
topicID := *transactionReceipt.TopicID

fmt.Printf("for topic %v\n", topicID)

fmt.Printf("wait to propagate...\n")
time.Sleep(10 * time.Second)

mirrorClient, err := hedera.NewMirrorClient(os.Getenv("MIRROR_NODE_ADDRESS"))
wait := true
start := time.Now()

_, err = hedera.NewTopicMessageQuery().
SetTopicID(topicID).
SetStartTime(time.Unix(0, 0)).
Subscribe(client, func(message hedera.TopicMessage) {
if string(message.Contents) == bigContents {
wait = false
}
})

if err != nil {
panic(err)
}

_, err = hedera.NewMirrorConsensusTopicQuery().
_, err = hedera.NewTopicMessageSubmitTransaction().
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
SetMessage([]byte(bigContents)).
SetMaxChunks(4).
SetTopicID(topicID).
Subscribe(mirrorClient, func(response hedera.MirrorConsensusTopicResponse) {
fmt.Printf("at %v ( seq = %v ) received topic message of %v bytes \n", response.ConsensusTimestamp, response.SequenceNumber, len(response.Message))
}, func(err error) {
panic(err)
})

Execute(client)
if err != nil {
panic(err)
}

_, err = hedera.NewConsensusMessageSubmitTransaction().
for {
if !wait || uint64(time.Since(start).Seconds()) > 30 {
break
}

time.Sleep(2500)
}

transactionResponse, err = hedera.NewTopicDeleteTransaction().
SetTopicID(topicID).
SetMessage([]byte(bigContents)).
SetMaxChunks(4). // default is 10
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
SetMaxTransactionFee(hedera.NewHbar(5)).
Execute(client)
if err != nil {
panic(err)
}

_, err = transactionResponse.GetReceipt(client)
if err != nil {
panic(err)
}

for {
time.Sleep(1 * time.Second)
if wait {
panic("Message was not received within 30 seconds")
}
}

// 14k+ stuff to upload
var bigContents = `
const bigContents = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam augue sem, ut mattis dui laoreet a. Curabitur consequat est euismod, scelerisque metus et, tristique dui. Nulla commodo mauris ut faucibus ultricies. Quisque venenatis nisl nec augue tempus, at efficitur elit eleifend. Duis pharetra felis metus, sed dapibus urna vehicula id. Duis non venenatis turpis, sit amet ornare orci. Donec non interdum quam. Sed finibus nunc et risus finibus, non sagittis lorem cursus. Proin pellentesque tempor aliquam. Sed congue nisl in enim bibendum, condimentum vehicula nisi feugiat.
Suspendisse non sodales arcu. Suspendisse sodales, lorem ac mollis blandit, ipsum neque porttitor nulla, et sodales arcu ante fermentum tellus. Integer sagittis dolor sed augue fringilla accumsan. Cras vitae finibus arcu, sit amet varius dolor. Etiam id finibus dolor, vitae luctus velit. Proin efficitur augue nec pharetra accumsan. Aliquam lobortis nisl diam, vel fermentum purus finibus id. Etiam at finibus orci, et tincidunt turpis. Aliquam imperdiet congue lacus vel facilisis. Phasellus id magna vitae enim dapibus vestibulum vitae quis augue. Morbi eu consequat enim. Maecenas neque nulla, pulvinar sit amet consequat sed, tempor sed magna. Mauris lacinia sem feugiat faucibus aliquet. Etiam congue non turpis at commodo. Nulla facilisi.
Expand Down
4 changes: 2 additions & 2 deletions examples/create_account/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
fmt.Printf("private = %v\n", newKey)
fmt.Printf("public = %v\n", newKey.PublicKey())

transactionID, err := hedera.NewAccountCreateTransaction().
transactionResponse, err := hedera.NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetTransactionMemo("go sdk example create_account/main.go").
Execute(client)
Expand All @@ -55,7 +55,7 @@ func main() {
panic(err)
}

transactionReceipt, err := transactionID.GetReceipt(client)
transactionReceipt, err := transactionResponse.GetReceipt(client)

if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions examples/create_account_with_manual_signing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func main() {
panic(err)
}

transactionID, err := transaction.
transactionResponse, err := transaction.
Sign(operatorKey).
Execute(client)

if err != nil {
panic(err)
}

transactionReceipt, err := transactionID.GetReceipt(client)
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit a3c17a2

Please sign in to comment.