github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/exempt_custom_fees/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashgraph/hedera-sdk-go/v2"
     6  	"os"
     7  )
     8  
     9  func main() {
    10  	client, err := hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    11  	if err != nil {
    12  		panic(fmt.Sprintf("%v : error creating client", err))
    13  	}
    14  
    15  	id, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    16  	if err != nil {
    17  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    18  	}
    19  
    20  	// Retrieving operator key from environment variable OPERATOR_KEY
    21  	key, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    22  	if err != nil {
    23  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    24  	}
    25  
    26  	// Setting the client operator ID and key
    27  	client.SetOperator(id, key)
    28  
    29  	/**     Example 1
    30  	 *
    31  	 * Step 1
    32  	 *
    33  	 * Create accounts A, B, and C
    34  	 */
    35  
    36  	firstAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	firstPublicKey := firstAccountPrivateKey.PublicKey()
    41  	firstAccount, err := hedera.NewAccountCreateTransaction().
    42  		SetKey(firstPublicKey).
    43  		SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
    44  		Execute(client)
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	receiptFirstAccount, err := firstAccount.GetReceipt(client)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	//Get the new account ID from the receipt
    54  	firstAccountId := *receiptFirstAccount.AccountID
    55  	fmt.Println("firstAccountId: ", firstAccountId)
    56  
    57  	secondAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	secondAccountPublicKey := secondAccountPrivateKey.PublicKey()
    62  	secondAccount, err := hedera.NewAccountCreateTransaction().
    63  		SetKey(secondAccountPublicKey).
    64  		SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
    65  		Execute(client)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  	receiptSecondAccount, err := secondAccount.GetReceipt(client)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  
    74  	//Get the new account ID from the receipt
    75  	secondAccountId := *receiptSecondAccount.AccountID
    76  	fmt.Println("secondAccountId: ", secondAccountId)
    77  
    78  	thirdAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	thirdAccountPublicKey := thirdAccountPrivateKey.PublicKey()
    83  	thirdAccount, err := hedera.NewAccountCreateTransaction().
    84  		SetKey(thirdAccountPublicKey).
    85  		SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
    86  		Execute(client)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	receiptThirdAccount, err := thirdAccount.GetReceipt(client)
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  
    95  	//Get the new account ID from the receipt
    96  	thirdAccountId := *receiptThirdAccount.AccountID
    97  	fmt.Println("thirdAccountId: ", thirdAccountId)
    98  
    99  	/**
   100  	 * Step 2
   101  	 *
   102  	 * 2. Create a fungible token that has three fractional fees
   103  	 * Fee #1 sends 1/100 of the transferred value to collector 0.0.A.
   104  	 * Fee #2 sends 2/100 of the transferred value to collector 0.0.B.
   105  	 * Fee #3 sends 3/100 of the transferred value to collector 0.0.C.
   106  	 */
   107  
   108  	fee1 := hedera.NewCustomFractionalFee().SetFeeCollectorAccountID(firstAccountId).SetNumerator(1).SetDenominator(100).SetAllCollectorsAreExempt(true)
   109  	fee2 := hedera.NewCustomFractionalFee().SetFeeCollectorAccountID(secondAccountId).SetNumerator(2).SetDenominator(100).SetAllCollectorsAreExempt(true)
   110  	fee3 := hedera.NewCustomFractionalFee().SetFeeCollectorAccountID(thirdAccountId).SetNumerator(3).SetDenominator(100).SetAllCollectorsAreExempt(true)
   111  	tokenCreateTransaction, err := hedera.NewTokenCreateTransaction().
   112  		SetTokenName("HIP-573 Token").SetTokenSymbol("H573").
   113  		SetTokenType(hedera.TokenTypeFungibleCommon).
   114  		SetTreasuryAccountID(id).SetAutoRenewAccount(id).
   115  		SetAdminKey(key.PublicKey()).SetFreezeKey(key.PublicKey()).
   116  		SetWipeKey(key.PublicKey()).SetInitialSupply(100000000). // Total supply = 100000000 / 10 ^ 2
   117  		SetDecimals(2).SetCustomFees([]hedera.Fee{fee1, fee2, fee3}).FreezeWith(client)
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  
   122  	transactionResponse, err := tokenCreateTransaction.Sign(key).
   123  		Sign(firstAccountPrivateKey).
   124  		Sign(secondAccountPrivateKey).
   125  		Sign(thirdAccountPrivateKey).Execute(client)
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  	receipt, err := transactionResponse.GetReceipt(client)
   130  	if err != nil {
   131  		panic(err)
   132  	}
   133  	tokenId := *receipt.TokenID
   134  	fmt.Println("Created token with token id: ", tokenId)
   135  
   136  	/**
   137  	 * Step 3
   138  	 *
   139  	 * Collector 0.0.B sends 10_000 units of the token to 0.0.A.
   140  	 */
   141  
   142  	const amount = 10_000
   143  	// First we transfer the amount from treasury account to second account
   144  	treasuryTokenTransferTransaction, err := hedera.NewTransferTransaction().
   145  		AddTokenTransfer(tokenId, id, -amount).AddTokenTransfer(tokenId, secondAccountId, amount).
   146  		FreezeWith(client)
   147  	if err != nil {
   148  		panic(err)
   149  	}
   150  
   151  	treasuryTokenTransferSubmit, err := treasuryTokenTransferTransaction.Sign(key).Execute(client)
   152  	if err != nil {
   153  		panic(err)
   154  	}
   155  	treasuryTransferReceipt, err := treasuryTokenTransferSubmit.GetReceipt(client)
   156  	if err != nil {
   157  		panic(err)
   158  	}
   159  
   160  	fmt.Println("Sending from treasury account to the second account - 'TransferTransaction' status: ", treasuryTransferReceipt.Status)
   161  
   162  	tokenTransferTx, err := hedera.NewTransferTransaction().
   163  		AddTokenTransfer(tokenId, secondAccountId, -amount).
   164  		AddTokenTransfer(tokenId, firstAccountId, amount).
   165  		FreezeWith(client)
   166  	if err != nil {
   167  		panic(err)
   168  	}
   169  
   170  	submitTransaction, err := tokenTransferTx.Sign(key).Sign(secondAccountPrivateKey).Execute(client)
   171  	if err != nil {
   172  		panic(err)
   173  	}
   174  
   175  	record, err := submitTransaction.GetRecord(client)
   176  	if err != nil {
   177  		panic(err)
   178  	}
   179  	fmt.Println("Transaction fee: ", record.TransactionFee)
   180  
   181  	/**
   182  	 * Step 5
   183  	 *
   184  	 * Show that the fee collector accounts in the custom fee list
   185  	 * of the token that was created was not charged a custom fee in the transfer
   186  	 */
   187  
   188  	firstAccountBalanceAfter, err := hedera.NewAccountBalanceQuery().
   189  		SetAccountID(firstAccountId).
   190  		Execute(client)
   191  	if err != nil {
   192  		panic(err)
   193  	}
   194  	fmt.Println("first's balance:", firstAccountBalanceAfter.Tokens.Get(tokenId))
   195  
   196  	secondAccountBalanceAfter, err := hedera.NewAccountBalanceQuery().
   197  		SetAccountID(secondAccountId).
   198  		Execute(client)
   199  	if err != nil {
   200  		panic(err)
   201  	}
   202  	fmt.Println("second's balance:", secondAccountBalanceAfter.Tokens.Get(tokenId))
   203  
   204  	thirdAccountBalanceAfter, err := hedera.NewAccountBalanceQuery().
   205  		SetAccountID(thirdAccountId).
   206  		Execute(client)
   207  	if err != nil {
   208  		panic(err)
   209  	}
   210  	fmt.Println("third's balance:", secondAccountBalanceAfter.Tokens.Get(tokenId))
   211  
   212  	if firstAccountBalanceAfter.Tokens.Get(tokenId) == amount && secondAccountBalanceAfter.Tokens.Get(tokenId) == 0 && thirdAccountBalanceAfter.Tokens.Get(tokenId) == 0 {
   213  		fmt.Println("Fee collector accounts were not charged after transfer transaction")
   214  	}
   215  
   216  	client.Close()
   217  
   218  }