github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_allowance_adjust_transaction_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  /*-
    15   *
    16   * Hedera Go SDK
    17   *
    18   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    19   *
    20   * Licensed under the Apache License, Version 2.0 (the "License");
    21   * you may not use this file except in compliance with the License.
    22   * You may obtain a copy of the License at
    23   *
    24   *      http://www.apache.org/licenses/LICENSE-2.0
    25   *
    26   * Unless required by applicable law or agreed to in writing, software
    27   * distributed under the License is distributed on an "AS IS" BASIS,
    28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    29   * See the License for the specific language governing permissions and
    30   * limitations under the License.
    31   *
    32   */
    33  
    34  func TestUnitAccountAllowanceAdjustTransactionGet(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	key, err := PrivateKeyGenerate()
    38  	require.NoError(t, err)
    39  	key2, err := PrivateKeyGenerate()
    40  	require.NoError(t, err)
    41  
    42  	nodeAccountIDs := []AccountID{{Account: 10}}
    43  	transactionID := TransactionIDGenerate(AccountID{Account: 123})
    44  	tokenID := TokenID{Token: 3}
    45  	NftID := NftID{tokenID, 1}
    46  	tx, err := NewAccountAllowanceAdjustTransaction().
    47  		AddHbarAllowance(nodeAccountIDs[0], HbarFromTinybar(1)).
    48  		AddTokenAllowance(tokenID, nodeAccountIDs[0], 1).
    49  		AddTokenNftAllowance(NftID, nodeAccountIDs[0]).
    50  		SetTransactionID(transactionID).SetNodeAccountIDs(nodeAccountIDs).
    51  		SetMaxTransactionFee(HbarFromTinybar(100)).SetRegenerateTransactionID(true).
    52  		SetTransactionMemo("go sdk unit test").SetTransactionValidDuration(time.Second * 120).
    53  		SetMaxRetry(1).SetMaxBackoff(time.Second * 120).SetMinBackoff(time.Second * 1).
    54  		Freeze()
    55  	sign, err := key2.SignTransaction(&tx.Transaction)
    56  	require.NoError(t, err)
    57  	tx.AddSignature(key.PublicKey(), sign)
    58  	tx.AddSignature(key2.PublicKey(), sign)
    59  
    60  	expectedHbarAllowances := []*HbarAllowance{
    61  		{
    62  			SpenderAccountID: &nodeAccountIDs[0],
    63  			OwnerAccountID:   nil,
    64  			Amount:           1,
    65  		},
    66  	}
    67  
    68  	expectedTokenAllowances := []*TokenAllowance{
    69  		{
    70  			TokenID:          &tokenID,
    71  			SpenderAccountID: &nodeAccountIDs[0],
    72  			OwnerAccountID:   nil,
    73  			Amount:           1,
    74  		},
    75  	}
    76  
    77  	expectedTokenNftAllowances := []*TokenNftAllowance{
    78  		{
    79  			TokenID:          &tokenID,
    80  			SpenderAccountID: &nodeAccountIDs[0],
    81  			OwnerAccountID:   nil,
    82  			SerialNumbers:    []int64{1},
    83  			AllSerials:       false,
    84  		},
    85  	}
    86  
    87  	require.NoError(t, err)
    88  	require.Equal(t, expectedHbarAllowances, tx.GetHbarAllowances())
    89  	require.Equal(t, expectedTokenAllowances, tx.GetTokenAllowances())
    90  	require.Equal(t, expectedTokenNftAllowances, tx.GetTokenNftAllowances())
    91  	require.Equal(t, transactionID, tx.GetTransactionID())
    92  	require.Equal(t, nodeAccountIDs, tx.GetNodeAccountIDs())
    93  	require.Equal(t, HbarFromTinybar(100), tx.GetMaxTransactionFee())
    94  	require.Equal(t, true, tx.GetRegenerateTransactionID())
    95  	require.Equal(t, "go sdk unit test", tx.GetTransactionMemo())
    96  	require.Equal(t, time.Second*120, tx.GetTransactionValidDuration())
    97  	require.Equal(t, 1, tx.GetMaxRetry())
    98  	require.Equal(t, time.Second*120, tx.GetMaxBackoff())
    99  	require.Equal(t, time.Second*1, tx.GetMinBackoff())
   100  	require.Equal(t, fmt.Sprint("AccountAllowanceAdjustTransaction"), tx.getName())
   101  }
   102  
   103  func TestUnitAccountAllowanceAdjustTransactionGrantHbarAllowance(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	tx := NewAccountAllowanceAdjustTransaction().
   107  		GrantHbarAllowance(AccountID{Account: 3}, AccountID{Account: 4}, HbarFromTinybar(1))
   108  	expectedHbarAllowances := []*HbarAllowance{
   109  		{
   110  			SpenderAccountID: &AccountID{Account: 4},
   111  			OwnerAccountID:   &AccountID{Account: 3},
   112  			Amount:           1,
   113  		},
   114  	}
   115  	require.Equal(t, expectedHbarAllowances, tx.GetHbarAllowances())
   116  }
   117  func TestUnitAccountAllowanceAdjustTransactionRevokeHbarAllowance(t *testing.T) {
   118  	t.Parallel()
   119  
   120  	tx := NewAccountAllowanceAdjustTransaction().
   121  		RevokeHbarAllowance(AccountID{Account: 3}, AccountID{Account: 4}, HbarFromTinybar(1))
   122  	expectedHbarAllowances := []*HbarAllowance{
   123  		{
   124  			SpenderAccountID: &AccountID{Account: 4},
   125  			OwnerAccountID:   &AccountID{Account: 3},
   126  			Amount:           -1,
   127  		},
   128  	}
   129  	require.Equal(t, expectedHbarAllowances, tx.GetHbarAllowances())
   130  }
   131  
   132  func TestUnitAccountAllowanceAdjustTransactionGrantTokenAllowance(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	tokenID := TokenID{Token: 3}
   136  	tx := NewAccountAllowanceAdjustTransaction().
   137  		GrantTokenAllowance(tokenID, AccountID{Account: 3}, AccountID{Account: 4}, 1)
   138  	expectedTokenAllowances := []*TokenAllowance{
   139  		{
   140  			TokenID:          &tokenID,
   141  			SpenderAccountID: &AccountID{Account: 4},
   142  			OwnerAccountID:   &AccountID{Account: 3},
   143  			Amount:           1,
   144  		},
   145  	}
   146  	require.Equal(t, expectedTokenAllowances, tx.GetTokenAllowances())
   147  }
   148  
   149  func TestUnitAccountAllowanceAdjustTransactionRevokeTokenAllowance(t *testing.T) {
   150  	t.Parallel()
   151  
   152  	tokenID := TokenID{Token: 3}
   153  	tx := NewAccountAllowanceAdjustTransaction().
   154  		RevokeTokenAllowance(tokenID, AccountID{Account: 3}, AccountID{Account: 4}, 1)
   155  	expectedTokenAllowances := []*TokenAllowance{
   156  		{
   157  			TokenID:          &tokenID,
   158  			SpenderAccountID: &AccountID{Account: 4},
   159  			OwnerAccountID:   &AccountID{Account: 3},
   160  			Amount:           -1,
   161  		},
   162  	}
   163  	require.Equal(t, expectedTokenAllowances, tx.GetTokenAllowances())
   164  }
   165  
   166  func TestUnitAccountAllowanceAdjustTransactionGrantTokenNftAllowance(t *testing.T) {
   167  	t.Parallel()
   168  
   169  	tokenID := TokenID{Token: 3}
   170  	NftID := NftID{tokenID, 1}
   171  	tx := NewAccountAllowanceAdjustTransaction().
   172  		GrantTokenNftAllowance(NftID, AccountID{Account: 3}, AccountID{Account: 4})
   173  	expectedTokenNftAllowances := []*TokenNftAllowance{
   174  		{
   175  			SpenderAccountID:  &AccountID{Account: 4},
   176  			OwnerAccountID:    &AccountID{Account: 3},
   177  			TokenID:           &tokenID,
   178  			SerialNumbers:     []int64{1},
   179  			AllSerials:        false,
   180  			DelegatingSpender: nil,
   181  		},
   182  	}
   183  	require.Equal(t, expectedTokenNftAllowances, tx.GetTokenNftAllowances())
   184  }
   185  
   186  func TestUnitAccountAllowanceAdjustTransactionRevokeTokenNftAllowance(t *testing.T) {
   187  	t.Parallel()
   188  
   189  	tokenID := TokenID{Token: 3}
   190  	NftID := NftID{tokenID, 1}
   191  	tx := NewAccountAllowanceAdjustTransaction().
   192  		RevokeTokenNftAllowance(NftID, AccountID{Account: 3}, AccountID{Account: 4})
   193  	expectedTokenNftAllowances := []*TokenNftAllowance{
   194  		{
   195  			SpenderAccountID:  &AccountID{Account: 4},
   196  			OwnerAccountID:    &AccountID{Account: 3},
   197  			TokenID:           &tokenID,
   198  			SerialNumbers:     []int64{1},
   199  			AllSerials:        false,
   200  			DelegatingSpender: nil,
   201  		},
   202  	}
   203  	require.Equal(t, expectedTokenNftAllowances, tx.GetTokenNftAllowances())
   204  }
   205  
   206  func TestUnitAccountAllowanceAdjustTransactionAddAllTokenNftAllowance(t *testing.T) {
   207  	t.Parallel()
   208  
   209  	tokenID := TokenID{Token: 3}
   210  	tx := NewAccountAllowanceAdjustTransaction().
   211  		AddAllTokenNftAllowance(tokenID, AccountID{Account: 3})
   212  	expectedTokenNftAllowances := []*TokenNftAllowance{
   213  		{
   214  			SpenderAccountID:  &AccountID{Account: 3},
   215  			OwnerAccountID:    nil,
   216  			TokenID:           &tokenID,
   217  			SerialNumbers:     []int64{},
   218  			AllSerials:        true,
   219  			DelegatingSpender: nil,
   220  		},
   221  	}
   222  	require.Equal(t, expectedTokenNftAllowances, tx.GetTokenNftAllowances())
   223  }