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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashgraph/hedera-sdk-go/v2"
     6  	"os"
     7  )
     8  
     9  func main() {
    10  	var client *hedera.Client
    11  	var err error
    12  
    13  	// Retrieving network type from environment variable HEDERA_NETWORK
    14  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    15  	if err != nil {
    16  		panic(fmt.Sprintf("%v : error creating client", err))
    17  	}
    18  
    19  	// Retrieving operator ID from environment variable OPERATOR_ID
    20  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    21  	if err != nil {
    22  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    23  	}
    24  
    25  	// Retrieving operator key from environment variable OPERATOR_KEY
    26  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    27  	if err != nil {
    28  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    29  	}
    30  
    31  	// Setting the client operator ID and key
    32  	client.SetOperator(operatorAccountID, operatorKey)
    33  
    34  	// Generate new key to use with new account
    35  	newKey, err := hedera.PrivateKeyGenerateEd25519()
    36  	if err != nil {
    37  		panic(fmt.Sprintf("%v : error generating PrivateKey", err))
    38  	}
    39  
    40  	fmt.Printf("private = %v\n", newKey)
    41  	fmt.Printf("public = %v\n", newKey.PublicKey())
    42  
    43  	// Create account
    44  	// The only required property here is `key`
    45  	transactionResponse, err := hedera.NewAccountCreateTransaction().
    46  		// The key that must sign each transfer out of the account.
    47  		SetKey(newKey.PublicKey()).
    48  		// If true, this account's key must sign any transaction depositing into this account (in
    49  		// addition to all withdrawals)
    50  		SetReceiverSignatureRequired(false).
    51  		// The maximum number of tokens that an Account can be implicitly associated with. Defaults to 0
    52  		// and up to a maximum value of 1000.
    53  		SetMaxAutomaticTokenAssociations(1).
    54  		// The memo associated with the account
    55  		SetTransactionMemo("go sdk example create_account/main.go").
    56  		// The account is charged to extend its expiration date every this many seconds. If it doesn't
    57  		// have enough balance, it extends as long as possible. If it is empty when it expires, then it
    58  		// is deleted.
    59  		SetStakedAccountID(hedera.AccountID{Account: 3}).
    60  		SetInitialBalance(hedera.NewHbar(20)).
    61  		Execute(client)
    62  	if err != nil {
    63  		panic(fmt.Sprintf("%v : error executing account create transaction", err))
    64  	}
    65  
    66  	// Get receipt to see if transaction succeeded, and has the account ID
    67  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    68  	if err != nil {
    69  		panic(fmt.Sprintf("%v : error getting receipt", err))
    70  	}
    71  
    72  	accountID := *transactionReceipt.AccountID
    73  
    74  	info, err := hedera.NewAccountInfoQuery().
    75  		SetAccountID(accountID).
    76  		Execute(client)
    77  	if err != nil {
    78  		panic(fmt.Sprintf("%v : error retrieving account info", err))
    79  	}
    80  
    81  	println("Staked account id:", info.StakingInfo.StakedAccountID.String())
    82  
    83  	freezeUpdate, err := hedera.NewAccountUpdateTransaction().
    84  		SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
    85  		SetAccountID(accountID).
    86  		// Should use ClearStakedNodeID(), but it doesn't work for now.
    87  		SetStakedNodeID(0).
    88  		FreezeWith(client)
    89  	if err != nil {
    90  		panic(fmt.Sprintf("%v : error freezing account update transaction", err))
    91  	}
    92  
    93  	freezeUpdate.Sign(newKey)
    94  
    95  	transactionResponse, err = freezeUpdate.Execute(client)
    96  	if err != nil {
    97  		panic(fmt.Sprintf("%v : error executing account update transaction", err))
    98  	}
    99  
   100  	transactionReceipt, err = transactionResponse.GetReceipt(client)
   101  	if err != nil {
   102  		panic(fmt.Sprintf("%v : error getting receipt}", err))
   103  	}
   104  
   105  	info, err = hedera.NewAccountInfoQuery().
   106  		SetAccountID(accountID).
   107  		Execute(client)
   108  	if err != nil {
   109  		panic(fmt.Sprintf("%v : error retrieving account info", err))
   110  	}
   111  
   112  	println("Staked node id after update:", *info.StakingInfo.StakedNodeID)
   113  }