github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/staking/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  	transactionResponse, err := hedera.NewAccountCreateTransaction().
    44  		// The key that must sign each transfer out of the account.
    45  		SetKey(newKey.PublicKey()).
    46  		// If true, this account's key must sign any transaction depositing into this account (in
    47  		// addition to all withdrawals)
    48  		SetReceiverSignatureRequired(false).
    49  		// The maximum number of tokens that an Account can be implicitly associated with. Defaults to 0
    50  		// and up to a maximum value of 1000.
    51  		SetMaxAutomaticTokenAssociations(1).
    52  		// The memo associated with the account
    53  		SetTransactionMemo("go sdk example create_account/main.go").
    54  		// The account is charged to extend its expiration date every this many seconds. If it doesn't
    55  		// have enough balance, it extends as long as possible. If it is empty when it expires, then it
    56  		// is deleted.
    57  		SetStakedAccountID(hedera.AccountID{Account: 3}).
    58  		SetInitialBalance(hedera.NewHbar(20)).
    59  		Execute(client)
    60  	if err != nil {
    61  		panic(fmt.Sprintf("%v : error executing account create transaction", err))
    62  	}
    63  
    64  	// Get receipt to see if transaction succeeded, and has the account ID
    65  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    66  	if err != nil {
    67  		panic(fmt.Sprintf("%v : error getting receipt", err))
    68  	}
    69  
    70  	accountID := *transactionReceipt.AccountID
    71  
    72  	println("AccountID of staking account:", accountID.String())
    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 Node Account ID:", info.StakingInfo.StakedAccountID.String())
    82  }