github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/token_reject/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  	// create treasury account
    36  	treasuryKey, _ := hedera.PrivateKeyGenerateEd25519()
    37  	accountCreate, _ := hedera.NewAccountCreateTransaction().
    38  		SetKey(treasuryKey).
    39  		SetMaxAutomaticTokenAssociations(100).
    40  		Execute(client)
    41  	receipt, err := accountCreate.SetValidateStatus(true).GetReceipt(client)
    42  	if err != nil {
    43  		panic(fmt.Sprintf("%v : Error creating account", err))
    44  	}
    45  
    46  	treasury := *receipt.AccountID
    47  
    48  	// create receiver account with unlimited auto associations
    49  	receiverKey, _ := hedera.PrivateKeyGenerateEd25519()
    50  	accountCreate, _ = hedera.NewAccountCreateTransaction().
    51  		SetKey(receiverKey).
    52  		SetMaxAutomaticTokenAssociations(-1).
    53  		Execute(client)
    54  	receipt, err = accountCreate.SetValidateStatus(true).GetReceipt(client)
    55  	if err != nil {
    56  		panic(fmt.Sprintf("%v : Error creating account", err))
    57  	}
    58  
    59  	receiver := *receipt.AccountID
    60  
    61  	// create fungible token
    62  	frozenTokenCreate, _ := hedera.NewTokenCreateTransaction().
    63  		SetTokenName("ffff").
    64  		SetTokenSymbol("F").
    65  		SetTokenMemo("asdf").
    66  		SetDecimals(18).
    67  		SetInitialSupply(1_000_000).
    68  		SetTreasuryAccountID(treasury).
    69  		SetAdminKey(client.GetOperatorPublicKey()).FreezeWith(client)
    70  	tokenCreate, _ := frozenTokenCreate.Sign(treasuryKey).Execute(client)
    71  	receipt, err = tokenCreate.SetValidateStatus(true).GetReceipt(client)
    72  	if err != nil {
    73  		panic(fmt.Sprintf("%v : Error creating token", err))
    74  	}
    75  
    76  	tokenID := *receipt.TokenID
    77  
    78  	// create NFT
    79  	frozenTokenCreate, _ = hedera.NewTokenCreateTransaction().
    80  		SetTokenName("Example Collection").SetTokenSymbol("ABC").
    81  		SetTokenType(hedera.TokenTypeNonFungibleUnique).
    82  		SetDecimals(0).
    83  		SetInitialSupply(0).
    84  		SetMaxSupply(10).
    85  		SetTreasuryAccountID(treasury).
    86  		SetSupplyType(hedera.TokenSupplyTypeFinite).
    87  		SetSupplyKey(treasuryKey).
    88  		SetAdminKey(treasuryKey).FreezeWith(client)
    89  	tokenCreate, _ = frozenTokenCreate.Sign(treasuryKey).Execute(client)
    90  	receipt, err = tokenCreate.SetValidateStatus(true).GetReceipt(client)
    91  	if err != nil {
    92  		panic(fmt.Sprintf("%v : Error creating token", err))
    93  	}
    94  
    95  	nftID := *receipt.TokenID
    96  
    97  	// mint 3 NFTs
    98  	initialMetadataList := [][]byte{{1}, {2}, {3}}
    99  	frozenMint, _ := hedera.NewTokenMintTransaction().SetTokenID(nftID).SetMetadatas(initialMetadataList).FreezeWith(client)
   100  	mint, _ := frozenMint.Sign(treasuryKey).Execute(client)
   101  	receipt, err = mint.SetValidateStatus(true).GetReceipt(client)
   102  	if err != nil {
   103  		panic(fmt.Sprintf("%v : Error minting nfts", err))
   104  	}
   105  
   106  	serials := receipt.SerialNumbers
   107  
   108  	// transfer the tokens to the receiver account
   109  	frozenTransfer, _ := hedera.NewTransferTransaction().
   110  		AddNftTransfer(nftID.Nft(serials[0]), treasury, receiver).
   111  		AddNftTransfer(nftID.Nft(serials[1]), treasury, receiver).
   112  		AddNftTransfer(nftID.Nft(serials[2]), treasury, receiver).
   113  		AddTokenTransfer(tokenID, treasury, -1000000).
   114  		AddTokenTransfer(tokenID, receiver, 1000000).
   115  		FreezeWith(client)
   116  	tokenTransfer, _ := frozenTransfer.Sign(treasuryKey).Execute(client)
   117  	receipt, err = tokenTransfer.SetValidateStatus(true).GetReceipt(client)
   118  	if err != nil {
   119  		panic(fmt.Sprintf("%v : Error transferring tokens", err))
   120  	}
   121  
   122  	tokenBalance, err := hedera.NewAccountBalanceQuery().SetAccountID(receiver).Execute(client)
   123  	if err != nil {
   124  		panic(fmt.Sprintf("%v : Error getting balance", err))
   125  	}
   126  
   127  	fmt.Println("Fungible token balance for receiver account before reject: ", tokenBalance.Tokens.Get(tokenID))
   128  	fmt.Println("NFT balance for receiver account before reject: ", tokenBalance.Tokens.Get(nftID))
   129  
   130  	tokenBalance, err = hedera.NewAccountBalanceQuery().SetAccountID(treasury).Execute(client)
   131  	if err != nil {
   132  		panic(fmt.Sprintf("%v : Error getting balance", err))
   133  	}
   134  	fmt.Println("Fungible token balance for treasury account before reject: ", tokenBalance.Tokens.Get(tokenID))
   135  	fmt.Println("NFT balance for receiver treasury before reject: ", tokenBalance.Tokens.Get(nftID))
   136  	fmt.Println("-----------------------------------")
   137  
   138  	// reject the fungible tokens
   139  	frozenReject, _ := hedera.NewTokenRejectTransaction().
   140  		SetOwnerID(receiver).
   141  		SetTokenIDs(tokenID).
   142  		FreezeWith(client)
   143  	reject, _ := frozenReject.Sign(receiverKey).Execute(client)
   144  	receipt, err = reject.SetValidateStatus(true).GetReceipt(client)
   145  	if err != nil {
   146  		panic(fmt.Sprintf("%v : Error rejecting tokens", err))
   147  	}
   148  
   149  	// reject the NFTs
   150  	frozenRejectFlow, _ := hedera.NewTokenRejectFlow().
   151  		SetOwnerID(receiver).
   152  		SetNftIDs(nftID.Nft(serials[0]), nftID.Nft(serials[1]), nftID.Nft(serials[2])).
   153  		FreezeWith(client)
   154  	reject, _ = frozenRejectFlow.Sign(receiverKey).Execute(client)
   155  	receipt, err = reject.SetValidateStatus(true).GetReceipt(client)
   156  	if err != nil {
   157  		panic(fmt.Sprintf("%v : Error rejecting tokens", err))
   158  	}
   159  
   160  	tokenBalance, err = hedera.NewAccountBalanceQuery().SetAccountID(receiver).Execute(client)
   161  	if err != nil {
   162  		panic(fmt.Sprintf("%v : Error getting balance", err))
   163  	}
   164  
   165  	fmt.Println("Fungible token balance for receiver account after reject: ", tokenBalance.Tokens.Get(tokenID))
   166  	fmt.Println("NFT balance for receiver account after reject: ", tokenBalance.Tokens.Get(nftID))
   167  
   168  	tokenBalance, err = hedera.NewAccountBalanceQuery().SetAccountID(treasury).Execute(client)
   169  	if err != nil {
   170  		panic(fmt.Sprintf("%v : Error getting balance", err))
   171  	}
   172  	fmt.Println("Fungible token balance for treasury account after reject: ", tokenBalance.Tokens.Get(tokenID))
   173  	fmt.Println("NFT balance for receiver treasury after reject: ", tokenBalance.Tokens.Get(nftID))
   174  }