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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/hashgraph/hedera-sdk-go/v2"
     8  )
     9  
    10  func main() {
    11  	var client *hedera.Client
    12  	var err error
    13  
    14  	// Retrieving network type from environment variable HEDERA_NETWORK
    15  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    16  	if err != nil {
    17  		panic(fmt.Sprintf("%v : error creating client", err))
    18  	}
    19  
    20  	// Retrieving operator ID from environment variable OPERATOR_ID
    21  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    22  	if err != nil {
    23  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    24  	}
    25  
    26  	// Retrieving operator key from environment variable OPERATOR_KEY
    27  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    28  	if err != nil {
    29  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    30  	}
    31  
    32  	// Setting the client operator ID and key
    33  	client.SetOperator(operatorAccountID, operatorKey)
    34  
    35  	fmt.Println("Crypto Transfer Example")
    36  
    37  	fmt.Printf("Transferring 1 hbar from %v to 0.0.3\n", client.GetOperatorAccountID())
    38  
    39  	transactionResponse, err := hedera.NewTransferTransaction().
    40  		// Hbar has to be negated to denote we are taking out from that account
    41  		AddHbarTransfer(client.GetOperatorAccountID(), hedera.NewHbar(-1)).
    42  		// If the amount of these 2 transfers is not the same, the transaction will throw an error
    43  		AddHbarTransfer(hedera.AccountID{Account: 3}, hedera.NewHbar(1)).
    44  		SetTransactionMemo("go sdk example send_hbar/main.go").
    45  		Execute(client)
    46  
    47  	if err != nil {
    48  		panic(fmt.Sprintf("%v : error executing transfer", err))
    49  	}
    50  
    51  	// Retrieve the receipt to make sure the transaction went through
    52  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    53  
    54  	if err != nil {
    55  		panic(fmt.Sprintf("%v : error retrieving transfer receipt", err))
    56  	}
    57  
    58  	fmt.Printf("crypto transfer status: %v\n", transactionReceipt.Status)
    59  }