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 3, 2020
1 parent 1584c22 commit a608011
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 168 deletions.
9 changes: 5 additions & 4 deletions examples/consensus_pub_sub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func main() {
panic(err)
}

operatorPrivateKey, err := hedera.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
operatorPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))

if err != nil {
panic(err)
}

client.SetOperator(operatorAccountID, operatorPrivateKey)

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

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

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

mirrorNodeAddress := os.Getenv("MIRROR_NODE_ADDRESS")

mirrorClient, err := hedera.NewMirrorClient(mirrorNodeAddress)
mirrorClient := hedera.ClientForTestnet()
mirrorClient.SetMirrorNetwork([]string{mirrorNodeAddress})
if err != nil {
panic(err)
}
Expand Down
39 changes: 28 additions & 11 deletions examples/create_account/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,44 @@ import (
)

func main() {
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
var client *hedera.Client
var err error

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.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(err)
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")

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

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

client.SetOperator(operatorAccountID, operatorKey)
}

newKey, err := hedera.GenerateEd25519PrivateKey()
newKey, err := hedera.GeneratePrivateKey()
if err != nil {
panic(err)
}

fmt.Printf("private = %v\n", newKey)
fmt.Printf("public = %v\n", newKey.PublicKey())

client := hedera.ClientForTestnet().
SetOperator(operatorAccountID, operatorPrivateKey)

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

newAccountID := transactionReceipt.GetAccountID()
newAccountID := *transactionReceipt.AccountID

fmt.Printf("account = %v\n", newAccountID)
}
46 changes: 33 additions & 13 deletions examples/create_account_with_manual_signing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,39 @@ import (
)

func main() {
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
var client *hedera.Client
var err error

if os.Getenv("HEDERA_NETWORK") == "previewnet" {
client = hedera.ClientForPreviewnet()
} else {
client, err = hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE"))

if err != nil {
println("not error", err.Error())
client = hedera.ClientForTestnet()
}
}

operatorPrivateKey, err := hedera.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(err)
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")
var operatorKey hedera.PrivateKey

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

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

client.SetOperator(operatorAccountID, operatorKey)
}

newKey, err := hedera.GenerateEd25519PrivateKey()
newKey, err := hedera.GeneratePrivateKey()
if err != nil {
panic(err)
}
Expand All @@ -27,21 +49,19 @@ func main() {
fmt.Printf("private = %v\n", newKey)
fmt.Printf("public = %v\n", newKey.PublicKey())

client := hedera.ClientForTestnet()

transaction, err := hedera.NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetInitialBalance(hedera.ZeroHbar).
SetTransactionID(hedera.NewTransactionID(operatorAccountID)).
SetTransactionID(hedera.TransactionIDGenerate(client.GetOperatorID())).
SetTransactionMemo("sdk example create_account__with_manual_signing/main.go").
Build(client)
FreezeWith(client)

if err != nil {
panic(err)
}

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

if err != nil {
Expand All @@ -53,7 +73,7 @@ func main() {
panic(err)
}

newAccountId := transactionReceipt.GetAccountID()
newAccountId := *transactionReceipt.AccountID

fmt.Printf("account = %v\n", newAccountId)
}
48 changes: 33 additions & 15 deletions examples/create_account_with_threshold_keys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,46 @@ import (
)

func main() {
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
var client *hedera.Client
var err error

if os.Getenv("HEDERA_NETWORK") == "previewnet" {
client = hedera.ClientForPreviewnet()
} else {
client, err = hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE"))

if err != nil {
println("not error", err.Error())
client = hedera.ClientForTestnet()
}
}

operatorPrivateKey, err := hedera.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(err)
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")
var operatorKey hedera.PrivateKey

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

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

client.SetOperator(operatorAccountID, operatorKey)
}

keys := make([]hedera.Ed25519PrivateKey, 3)
pubKeys := make([]hedera.PublicKey, 3)
keys := make([]hedera.PrivateKey, 3)
pubKey := hedera.PublicKey{}

fmt.Println("threshold key example")
fmt.Println("Keys: ")

for i := range keys {
newKey, err := hedera.GenerateEd25519PrivateKey()
newKey, err := hedera.GeneratePrivateKey()
if err != nil {
panic(err)
}
Expand All @@ -38,17 +60,13 @@ func main() {
pubKeys[i] = newKey.PublicKey()
}

client := hedera.ClientForTestnet()

// A threshold key with a threshold of 2 and length of 3 requires
// at least 2 of the 3 keys to sign anything modifying the account
thresholdKey := hedera.NewThresholdKey(2).
AddAll(pubKeys)

fmt.Printf("threshold key %v\n", thresholdKey)
//fmt.Printf("threshold key %v\n", thresholdKey)

transaction, err := hedera.NewAccountCreateTransaction().
SetKey(thresholdKey).
SetKey(pubKeys).
SetInitialBalance(hedera.NewHbar(6)).
SetTransactionID(hedera.NewTransactionID(operatorAccountID)).
SetTransactionMemo("sdk example create_account_with_threshold_keys/main.go").
Expand Down
46 changes: 32 additions & 14 deletions examples/create_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,41 @@ import (
)

func main() {
client := hedera.ClientForTestnet()

operatorPrivateKey, err := hedera.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(err)
var client *hedera.Client
var err error

if os.Getenv("HEDERA_NETWORK") == "previewnet" {
client = hedera.ClientForPreviewnet()
} else {
client, err = hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE"))

if err != nil {
println("not error", err.Error())
client = hedera.ClientForTestnet()
}
}

operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(err)
}
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")
var operatorKey hedera.PrivateKey

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.NewFileCreateTransaction().
resp, err := hedera.NewFileCreateTransaction().
// A file is not implicitly owned by anyone, even the operator
AddKey(operatorPrivateKey.PublicKey()).
SetKeys(client.GetOperatorKey()).
SetContents([]byte("Hello, World")).
SetTransactionMemo("go sdk example create_file/main.go").
SetMaxTransactionFee(hedera.HbarFrom(8, hedera.HbarUnits.Hbar)).
Expand All @@ -34,11 +52,11 @@ func main() {
panic(err)
}

transactionReceipt, err := transactionID.GetReceipt(client)
receipt, err := resp.GetReceipt(client)

if err != nil {
panic(err)
}

fmt.Printf("file = %v\n", transactionReceipt.GetFileID())
fmt.Printf("file = %v\n", *receipt.FileID)
}
Loading

0 comments on commit a608011

Please sign in to comment.