github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/token_update_metadata/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  	// 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  	// Retrieving operator key from environment variable OPERATOR_KEY
    25  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    26  	if err != nil {
    27  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    28  	}
    29  	// Setting the client operator ID and key
    30  	client.SetOperator(operatorAccountID, operatorKey)
    31  
    32  	updateMutableTokenMetadata(client)
    33  
    34  	updateImmutableTokenMetadata(client)
    35  }
    36  
    37  func updateMutableTokenMetadata(client *hedera.Client) {
    38  	// Create admin key
    39  	adminKey, err := hedera.PrivateKeyGenerateEd25519()
    40  	if err != nil {
    41  		panic(fmt.Sprintf("%v : error creating admin key", err))
    42  	}
    43  	fmt.Println("create admin key: ", adminKey.String())
    44  
    45  	var initialMetadata = []byte{1, 2, 3}
    46  	var newMetadata = []byte{3, 4, 5, 6}
    47  
    48  	// Create the token
    49  	tx, err := hedera.NewTokenCreateTransaction().
    50  		SetTokenName("ffff").
    51  		SetTokenSymbol("F").
    52  		SetDecimals(3).
    53  		SetTokenType(hedera.TokenTypeFungibleCommon). // The same flow can be executed with a TokenTypeNonFungibleUnique (i.e. HIP-765)
    54  		SetTokenMetadata(initialMetadata).
    55  		SetInitialSupply(1000000).
    56  		SetTreasuryAccountID(client.GetOperatorAccountID()).
    57  		SetAdminKey(adminKey).
    58  		SetFreezeDefault(false).
    59  		FreezeWith(client)
    60  	if err != nil {
    61  		panic(fmt.Sprintf("%v : error creating token", err))
    62  	}
    63  	resp, err := tx.Sign(adminKey).Execute(client)
    64  	if err != nil {
    65  		panic(fmt.Sprintf("%v : error creating token", err))
    66  	}
    67  
    68  	receipt, err := resp.SetValidateStatus(true).GetReceipt(client)
    69  	if err != nil {
    70  		panic(fmt.Sprintf("%v : error creating token", err))
    71  	}
    72  	fmt.Println("created token: ", receipt.TokenID.String())
    73  
    74  	// Query the token info to get the metadata after creation
    75  	info, err := hedera.NewTokenInfoQuery().
    76  		SetTokenID(*receipt.TokenID).
    77  		Execute(client)
    78  	if err != nil {
    79  		panic(fmt.Sprintf("%v : error getting token info", err))
    80  	}
    81  	fmt.Println("token's metadata after creation: ", info.Metadata)
    82  
    83  	// Update the token's metadata
    84  	tx1, err := hedera.NewTokenUpdateTransaction().
    85  		SetTokenID(*receipt.TokenID).
    86  		SetTokenMetadata(newMetadata).
    87  		FreezeWith(client)
    88  	if err != nil {
    89  		panic(fmt.Sprintf("%v : error updating token", err))
    90  	}
    91  	resp, err = tx1.Sign(adminKey).
    92  		Execute(client)
    93  	if err != nil {
    94  		panic(fmt.Sprintf("%v : error updating token", err))
    95  	}
    96  	_, err = resp.SetValidateStatus(true).GetReceipt(client)
    97  	if err != nil {
    98  		panic(fmt.Sprintf("%v : error updating token", err))
    99  	}
   100  
   101  	// Query the token info to get the metadata after update
   102  	info, err = hedera.NewTokenInfoQuery().
   103  		SetTokenID(*receipt.TokenID).
   104  		Execute(client)
   105  	if err != nil {
   106  		panic(fmt.Sprintf("%v : error getting token info", err))
   107  	}
   108  	fmt.Println("token's metadata after update: ", info.Metadata)
   109  }
   110  
   111  func updateImmutableTokenMetadata(client *hedera.Client) {
   112  	// Create metadata key
   113  	metadataKey, err := hedera.PrivateKeyGenerateEd25519()
   114  	if err != nil {
   115  		panic(fmt.Sprintf("%v : error creating admin key", err))
   116  	}
   117  	fmt.Println("create metadata key: ", metadataKey.String())
   118  
   119  	var initialMetadata = []byte{1, 2, 3}
   120  	var newMetadata = []byte{3, 4, 5, 6}
   121  
   122  	// Create the token
   123  	resp, err := hedera.NewTokenCreateTransaction().
   124  		SetTokenName("ffff").
   125  		SetTokenSymbol("F").
   126  		SetDecimals(3).
   127  		SetTokenType(hedera.TokenTypeFungibleCommon). // The same flow can be executed with a TokenTypeNonFungibleUnique (i.e. HIP-765)
   128  		SetTokenMetadata(initialMetadata).
   129  		SetInitialSupply(1000000).
   130  		SetTreasuryAccountID(client.GetOperatorAccountID()).
   131  		SetMetadataKey(metadataKey).
   132  		SetFreezeDefault(false).
   133  		Execute(client)
   134  	if err != nil {
   135  		panic(fmt.Sprintf("%v : error creating token", err))
   136  	}
   137  
   138  	receipt, err := resp.SetValidateStatus(true).GetReceipt(client)
   139  	if err != nil {
   140  		panic(fmt.Sprintf("%v : error creating token", err))
   141  	}
   142  	fmt.Println("created token: ", receipt.TokenID.String())
   143  
   144  	// Query the token info to get the metadata after creation
   145  	info, err := hedera.NewTokenInfoQuery().
   146  		SetTokenID(*receipt.TokenID).
   147  		Execute(client)
   148  	if err != nil {
   149  		panic(fmt.Sprintf("%v : error getting token info", err))
   150  	}
   151  	fmt.Println("token's metadata after creation: ", info.Metadata)
   152  
   153  	// Update the token's metadata
   154  	tx, err := hedera.NewTokenUpdateTransaction().
   155  		SetTokenID(*receipt.TokenID).
   156  		SetTokenMetadata(newMetadata).
   157  		FreezeWith(client)
   158  	if err != nil {
   159  		panic(fmt.Sprintf("%v : error updating token", err))
   160  	}
   161  	resp, err = tx.Sign(metadataKey).
   162  		Execute(client)
   163  	if err != nil {
   164  		panic(fmt.Sprintf("%v : error updating token", err))
   165  	}
   166  	_, err = resp.SetValidateStatus(true).GetReceipt(client)
   167  	if err != nil {
   168  		panic(fmt.Sprintf("%v : error updating token", err))
   169  	}
   170  
   171  	// Query the token info to get the metadata after update
   172  	info, err = hedera.NewTokenInfoQuery().
   173  		SetTokenID(*receipt.TokenID).
   174  		Execute(client)
   175  	if err != nil {
   176  		panic(fmt.Sprintf("%v : error getting token info", err))
   177  	}
   178  	fmt.Println("token's metadata after update: ", info.Metadata)
   179  }