github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/delete_file/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 be used with the new file
    36  	newKey, err := hedera.GeneratePrivateKey()
    37  	if err != nil {
    38  		panic(fmt.Sprintf("%v : error generating PrivateKey", err))
    39  	}
    40  
    41  	fmt.Println("Creating a file to delete:")
    42  
    43  	// First create a file
    44  	freezeTransaction, err := hedera.NewFileCreateTransaction().
    45  		// Mock contents
    46  		SetContents([]byte("The quick brown fox jumps over the lazy dog")).
    47  		// All keys at the top level of a key list must sign to create or modify the file. Any one of
    48  		// the keys at the top level key list can sign to delete the file.
    49  		SetKeys(newKey.PublicKey()).
    50  		SetTransactionMemo("go sdk example delete_file/main.go").
    51  		SetMaxTransactionFee(hedera.HbarFrom(8, hedera.HbarUnits.Hbar)).
    52  		FreezeWith(client)
    53  
    54  	if err != nil {
    55  		panic(fmt.Sprintf("%v : error freezing transaction", err))
    56  	}
    57  	transactionResponse, err := freezeTransaction.Sign(newKey).Execute(client)
    58  
    59  	if err != nil {
    60  		panic(fmt.Sprintf("%v : error creating file", err))
    61  	}
    62  
    63  	// Get the receipt to make sure transaction went through
    64  	receipt, err := transactionResponse.GetReceipt(client)
    65  	if err != nil {
    66  		panic(fmt.Sprintf("%v : error retrieving file creation receipt", err))
    67  	}
    68  
    69  	// Retrieve file ID from the receipt
    70  	newFileID := *receipt.FileID
    71  
    72  	fmt.Printf("file = %v\n", newFileID)
    73  	fmt.Println("deleting created file")
    74  
    75  	// To delete a file you must do the following:
    76  	deleteTransaction, err := hedera.NewFileDeleteTransaction().
    77  		// Set file ID
    78  		SetFileID(newFileID).
    79  		FreezeWith(client)
    80  
    81  	if err != nil {
    82  		panic(fmt.Sprintf("%v : error freezing file delete transaction", err))
    83  	}
    84  
    85  	// Sign with the key we used to create the file
    86  	deleteTransaction.Sign(newKey)
    87  
    88  	// Execute the file delete transaction
    89  	deleteTransactionResponse, err := deleteTransaction.Execute(client)
    90  	if err != nil {
    91  		panic(fmt.Sprintf("%v : error executing file delete transaction", err))
    92  	}
    93  
    94  	// Check that it went through
    95  	deleteTransactionReceipt, err := deleteTransactionResponse.GetReceipt(client)
    96  	if err != nil {
    97  		panic(fmt.Sprintf("%v : error retrieving file deletion receipt", err))
    98  	}
    99  
   100  	fmt.Printf("file delete transaction status: %v\n", deleteTransactionReceipt.Status)
   101  
   102  	// Querying for file info on a deleted file will result in FILE_DELETED
   103  	// Good way to check if file was actually deleted
   104  	fileInfo, err := hedera.NewFileInfoQuery().
   105  		// Only file ID required
   106  		SetFileID(newFileID).
   107  		Execute(client)
   108  
   109  	if err != nil {
   110  		panic(fmt.Sprintf("%v : error executing file info query", err))
   111  	}
   112  
   113  	fmt.Printf("file %v was deleted: %v\n", newFileID, fileInfo.IsDeleted)
   114  }