github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/delete_account/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  	// Generate the key to use with the new account
    36  	newKey, err := hedera.GeneratePrivateKey()
    37  	if err != nil {
    38  		panic(fmt.Sprintf("%v : error generating PrivateKey", err))
    39  	}
    40  
    41  	fmt.Println("Creating an account to delete:")
    42  	fmt.Printf("private = %v\n", newKey)
    43  	fmt.Printf("public = %v\n", newKey.PublicKey())
    44  
    45  	// First create an account
    46  	transactionResponse, err := hedera.NewAccountCreateTransaction().
    47  		// This key will be required to delete the account later
    48  		SetKey(newKey.PublicKey()).
    49  		// Initial balance
    50  		SetInitialBalance(hedera.NewHbar(2)).
    51  		SetTransactionMemo("go sdk example delete_account/main.go").
    52  		Execute(client)
    53  
    54  	if err != nil {
    55  		panic(fmt.Sprintf("%v : error creating account", err))
    56  	}
    57  
    58  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    59  	if err != nil {
    60  		panic(fmt.Sprintf("%v : error retrieving account creation receipt", err))
    61  	}
    62  
    63  	newAccountID := *transactionReceipt.AccountID
    64  
    65  	fmt.Printf("account = %v\n", newAccountID)
    66  	fmt.Println("deleting created account")
    67  
    68  	// To delete an account you must do the following:
    69  	deleteTransaction, err := hedera.NewAccountDeleteTransaction().
    70  		// Set the account to be deleted
    71  		SetAccountID(newAccountID).
    72  		// Set an account ID to transfer the balance of the deleted account to
    73  		SetTransferAccountID(hedera.AccountID{Account: 3}).
    74  		SetTransactionMemo("go sdk example delete_account/main.go").
    75  		FreezeWith(client)
    76  
    77  	if err != nil {
    78  		panic(fmt.Sprintf("%v : error freezing account delete transaction", err))
    79  	}
    80  
    81  	// Manually sign the transaction with the private key of the account to be deleted
    82  	deleteTransaction = deleteTransaction.Sign(newKey)
    83  
    84  	// Execute the transaction
    85  	deleteTransactionResponse, err := deleteTransaction.Execute(client)
    86  
    87  	if err != nil {
    88  		panic(fmt.Sprintf("%v : error deleting account", err))
    89  	}
    90  
    91  	deleteTransactionReceipt, err := deleteTransactionResponse.GetReceipt(client)
    92  	if err != nil {
    93  		panic(fmt.Sprintf("%v : error retrieving account deletion receipt", err))
    94  	}
    95  
    96  	fmt.Printf("account delete transaction status: %v\n", deleteTransactionReceipt.Status)
    97  }