github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/token_update_nfts/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  /**
    11   * @summary E2E-HIP-657 https://hips.hedera.com/hip/hip-657
    12   * @description Update nfts metadata of non-fungible token with metadata key
    13   */
    14  func main() {
    15  	var client *hedera.Client
    16  	var err error
    17  
    18  	// Retrieving network type from environment variable HEDERA_NETWORK
    19  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    20  	if err != nil {
    21  		panic(fmt.Sprintf("%v : error creating client", err))
    22  	}
    23  
    24  	// Retrieving operator ID from environment variable OPERATOR_ID
    25  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    26  	if err != nil {
    27  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    28  	}
    29  
    30  	// Retrieving operator key from environment variable OPERATOR_KEY
    31  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    32  	if err != nil {
    33  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    34  	}
    35  
    36  	// Setting the client operator ID and key
    37  	client.SetOperator(operatorAccountID, operatorKey)
    38  
    39  	metadataKey, err := hedera.PrivateKeyGenerateEd25519()
    40  	if err != nil {
    41  		panic(fmt.Sprintf("%v : error creating metadata key", err))
    42  	}
    43  	fmt.Println("create metadata key: ", metadataKey.String())
    44  
    45  	var initialMetadataList = [][]byte{{2, 1}, {1, 2}}
    46  	var updatedMetadata = []byte{22, 22}
    47  
    48  	// Create token with metadata key
    49  	nftCreateTransaction, err := hedera.NewTokenCreateTransaction().
    50  		SetTokenName("HIP-542 Example Collection").SetTokenSymbol("HIP-542").
    51  		SetTokenType(hedera.TokenTypeNonFungibleUnique).SetDecimals(0).
    52  		SetInitialSupply(0).SetMaxSupply(10).
    53  		SetTreasuryAccountID(client.GetOperatorAccountID()).SetSupplyType(hedera.TokenSupplyTypeFinite).
    54  		SetAdminKey(operatorKey).SetFreezeKey(operatorKey).SetSupplyKey(operatorKey).SetMetadataKey(metadataKey).FreezeWith(client)
    55  	if err != nil {
    56  		panic(fmt.Sprintf("%v : error creating token transaction", err))
    57  	}
    58  
    59  	// Sign the transaction with the operator key
    60  	nftSignTransaction := nftCreateTransaction.Sign(operatorKey)
    61  
    62  	// Submit the transaction to the Hedera network
    63  	nftCreateSubmit, err := nftSignTransaction.Execute(client)
    64  	if err != nil {
    65  		panic(fmt.Sprintf("%v : error submitting transaction", err))
    66  	}
    67  
    68  	// Get transaction receipt information
    69  	nftCreateReceipt, err := nftCreateSubmit.GetReceipt(client)
    70  	if err != nil {
    71  		panic(fmt.Sprintf("%v : error receiving receipt", err))
    72  	}
    73  
    74  	// Get token id from the transaction
    75  	nftTokenID := *nftCreateReceipt.TokenID
    76  	fmt.Println("Created NFT with token id: ", nftTokenID)
    77  
    78  	tokenInfo, err := hedera.NewTokenInfoQuery().SetTokenID(nftTokenID).Execute(client)
    79  	if err != nil {
    80  		panic(fmt.Sprintf("%v : error getting token info", err))
    81  	}
    82  	fmt.Println("Token metadata key: ", tokenInfo.MetadataKey.String())
    83  
    84  	// Mint nfts
    85  	mintTransaction, _ := hedera.NewTokenMintTransaction().SetTokenID(nftTokenID).SetMetadatas(initialMetadataList).FreezeWith(client)
    86  	for _, v := range mintTransaction.GetMetadatas() {
    87  		fmt.Println("Set metadata: ", v)
    88  	}
    89  
    90  	mintTransactionSubmit, err := mintTransaction.Sign(operatorKey).Execute(client)
    91  	if err != nil {
    92  		panic(fmt.Sprintf("%v : error minting NFT", err))
    93  	}
    94  	receipt, err := mintTransactionSubmit.GetReceipt(client)
    95  	if err != nil {
    96  		panic(fmt.Sprintf("%v : error minting NFT", err))
    97  	}
    98  
    99  	// Check that metadata was set correctly
   100  	serials := receipt.SerialNumbers
   101  	fmt.Println(serials)
   102  	var metadataAfterMint = make([][]byte, len(initialMetadataList))
   103  	for i, v := range serials {
   104  		nftID := hedera.NftID{TokenID: nftTokenID, SerialNumber: v}
   105  		nftInfo, err := hedera.NewTokenNftInfoQuery().SetNftID(nftID).Execute(client)
   106  		if err != nil {
   107  			panic(fmt.Sprintf("%v : error getting token info", err))
   108  		}
   109  		fmt.Println(nftInfo)
   110  		metadataAfterMint[i] = nftInfo[0].Metadata
   111  	}
   112  	fmt.Println("Metadata after mint: ", metadataAfterMint)
   113  
   114  	// Create account owner of nft
   115  	accountCreateTransaction, err := hedera.NewAccountCreateTransaction().
   116  		SetKey(operatorKey).SetMaxAutomaticTokenAssociations(10). // If the account does not have any automatic token association slots open ONLY then associate the NFT to the account
   117  		Execute(client)
   118  	if err != nil {
   119  		panic(fmt.Sprintf("%v : error creating account", err))
   120  	}
   121  	receipt, err = accountCreateTransaction.GetReceipt(client)
   122  	if err != nil {
   123  		panic(fmt.Sprintf("%v : error receiving receipt", err))
   124  	}
   125  	newAccountId := receipt.AccountID
   126  
   127  	// Transfer the NFT to the new account
   128  	tokenTransferTransaction, err := hedera.NewTransferTransaction().AddNftTransfer(nftTokenID.Nft(serials[0]), operatorAccountID, *newAccountId).Execute(client)
   129  	if err != nil {
   130  		panic(fmt.Sprintf("%v : error transfering nft", err))
   131  	}
   132  	_, err = tokenTransferTransaction.GetReceipt(client)
   133  	if err != nil {
   134  		panic(fmt.Sprintf("%v : error getting receipt", err))
   135  	}
   136  
   137  	// update nfts metadata
   138  	metadataUpdateTransaction, err := hedera.NewTokenUpdateNftsTransaction().
   139  		SetTokenID(nftTokenID).
   140  		SetSerialNumbers(serials).
   141  		SetMetadata(updatedMetadata).
   142  		FreezeWith(client)
   143  	if err != nil {
   144  		panic(fmt.Sprintf("%v : error creating transaction", err))
   145  	}
   146  	fmt.Println("Updatad metadata: ", metadataUpdateTransaction.GetMetadata())
   147  	metadataUpdateSubmit, err := metadataUpdateTransaction.Sign(metadataKey).Execute(client)
   148  	if err != nil {
   149  		panic(fmt.Sprintf("%v : error submitting transaction", err))
   150  	}
   151  
   152  	receipt, err = metadataUpdateSubmit.GetReceipt(client)
   153  	if err != nil {
   154  		panic(fmt.Sprintf("%v : error receiving receipt", err))
   155  	}
   156  	fmt.Println("Metadata update status: ", receipt.Status)
   157  
   158  	// Check that metadata for the NFT was updated correctly
   159  	for _, v := range serials {
   160  		nftID := hedera.NftID{TokenID: nftTokenID, SerialNumber: v}
   161  		nftInfo, err := hedera.NewTokenNftInfoQuery().SetNftID(nftID).Execute(client)
   162  		if err != nil {
   163  			panic(fmt.Sprintf("%v : error getting token info", err))
   164  		}
   165  		fmt.Println("Metadata after update for serial number ", v, ": ", nftInfo[0].Metadata)
   166  	}
   167  }