github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_allowance_approve_transaction_unit_test.go (about) 1 //go:build all || unit 2 // +build all unit 3 4 package hedera 5 6 /*- 7 * 8 * Hedera Go SDK 9 * 10 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 */ 25 26 import ( 27 "testing" 28 29 "github.com/hashgraph/hedera-protobufs-go/services" 30 "github.com/stretchr/testify/require" 31 "google.golang.org/protobuf/types/known/wrapperspb" 32 ) 33 34 var tokenID1 = TokenID{Token: 1} 35 var tokenID2 = TokenID{Token: 141} 36 var serialNumber1 = int64(3) 37 var serialNumber2 = int64(4) 38 var nftID1 = tokenID2.Nft(serialNumber1) 39 var nftID2 = tokenID2.Nft(serialNumber2) 40 var owner = AccountID{Account: 10} 41 var spenderAccountID1 = AccountID{Account: 7} 42 var spenderAccountID2 = AccountID{Account: 7890} 43 var nodeAccountID = []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 44 var hbarAmount = HbarFromTinybar(100) 45 var tokenAmount = int64(101) 46 47 func TestUnitAccountAllowanceApproveTransaction(t *testing.T) { 48 t.Parallel() 49 50 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 51 52 transaction, err := NewAccountAllowanceApproveTransaction(). 53 SetTransactionID(transactionID). 54 SetNodeAccountIDs(nodeAccountID). 55 ApproveHbarAllowance(owner, spenderAccountID1, hbarAmount). 56 ApproveTokenAllowance(tokenID1, owner, spenderAccountID1, tokenAmount). 57 ApproveTokenNftAllowance(nftID1, owner, spenderAccountID1). 58 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID1). 59 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID2). 60 AddAllTokenNftApproval(tokenID1, spenderAccountID1). 61 Freeze() 62 require.NoError(t, err) 63 64 data := transaction.build() 65 66 switch d := data.Data.(type) { 67 case *services.TransactionBody_CryptoApproveAllowance: 68 require.Equal(t, d.CryptoApproveAllowance.CryptoAllowances, []*services.CryptoAllowance{ 69 { 70 Spender: spenderAccountID1._ToProtobuf(), 71 Owner: owner._ToProtobuf(), 72 Amount: hbarAmount.AsTinybar(), 73 }, 74 }) 75 require.Equal(t, d.CryptoApproveAllowance.NftAllowances, []*services.NftAllowance{ 76 { 77 TokenId: tokenID2._ToProtobuf(), 78 Spender: spenderAccountID1._ToProtobuf(), 79 Owner: owner._ToProtobuf(), 80 SerialNumbers: []int64{serialNumber1, serialNumber2}, 81 ApprovedForAll: &wrapperspb.BoolValue{Value: false}, 82 DelegatingSpender: nil, 83 }, 84 { 85 TokenId: tokenID2._ToProtobuf(), 86 Spender: spenderAccountID2._ToProtobuf(), 87 Owner: owner._ToProtobuf(), 88 SerialNumbers: []int64{serialNumber2}, 89 ApprovedForAll: &wrapperspb.BoolValue{Value: false}, 90 DelegatingSpender: nil, 91 }, 92 { 93 TokenId: tokenID1._ToProtobuf(), 94 Spender: spenderAccountID1._ToProtobuf(), 95 Owner: nil, 96 SerialNumbers: []int64{}, 97 ApprovedForAll: &wrapperspb.BoolValue{Value: true}, 98 DelegatingSpender: nil, 99 }, 100 }) 101 require.Equal(t, d.CryptoApproveAllowance.TokenAllowances, []*services.TokenAllowance{ 102 { 103 TokenId: tokenID1._ToProtobuf(), 104 Owner: owner._ToProtobuf(), 105 Spender: spenderAccountID1._ToProtobuf(), 106 Amount: tokenAmount, 107 }, 108 }) 109 } 110 } 111 func TestUnitvalidateNetworkOnIDs(t *testing.T) { 112 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 113 114 transaction, err := NewAccountAllowanceApproveTransaction(). 115 SetTransactionID(transactionID). 116 SetNodeAccountIDs(nodeAccountID). 117 ApproveHbarAllowance(owner, spenderAccountID1, hbarAmount). 118 ApproveTokenAllowance(tokenID1, owner, spenderAccountID1, tokenAmount). 119 ApproveTokenNftAllowance(nftID1, owner, spenderAccountID1). 120 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID1). 121 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID2). 122 AddAllTokenNftApproval(tokenID1, spenderAccountID1). 123 Freeze() 124 require.NoError(t, err) 125 126 client, err := _NewMockClient() 127 client.SetLedgerID(*NewLedgerIDTestnet()) 128 require.NoError(t, err) 129 130 e := transaction.validateNetworkOnIDs(client) 131 require.NoError(t, e) 132 } 133 func TestUnitAccountAllowanceApproveTransactionGet(t *testing.T) { 134 t.Parallel() 135 136 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 137 138 transaction, err := NewAccountAllowanceApproveTransaction(). 139 SetTransactionID(transactionID). 140 SetNodeAccountIDs(nodeAccountID). 141 ApproveHbarAllowance(owner, spenderAccountID1, hbarAmount). 142 ApproveTokenAllowance(tokenID1, owner, spenderAccountID1, tokenAmount). 143 ApproveTokenNftAllowance(nftID1, owner, spenderAccountID1). 144 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID1). 145 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID2). 146 AddAllTokenNftApproval(tokenID1, spenderAccountID1). 147 Freeze() 148 require.NoError(t, err) 149 150 transaction.GetTransactionID() 151 transaction.GetNodeAccountIDs() 152 153 _, err = transaction.GetTransactionHash() 154 require.NoError(t, err) 155 transaction.GetTokenNftAllowances() 156 transaction.GetHbarAllowances() 157 transaction.GetTokenAllowances() 158 transaction.GetMaxTransactionFee() 159 transaction.GetTransactionMemo() 160 transaction.GetRegenerateTransactionID() 161 _, err = transaction.GetSignatures() 162 require.NoError(t, err) 163 } 164 165 func TestUnitAccountAllowanceApproveTransactionSetNothing(t *testing.T) { 166 t.Parallel() 167 168 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 169 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 170 171 transaction, err := NewAccountAllowanceApproveTransaction(). 172 SetTransactionID(transactionID). 173 SetNodeAccountIDs(nodeAccountID). 174 Freeze() 175 require.NoError(t, err) 176 177 transaction.GetTransactionID() 178 transaction.GetNodeAccountIDs() 179 180 _, err = transaction.GetTransactionHash() 181 require.NoError(t, err) 182 transaction.GetTokenNftAllowances() 183 transaction.GetHbarAllowances() 184 transaction.GetTokenAllowances() 185 transaction.GetMaxTransactionFee() 186 transaction.GetTransactionMemo() 187 transaction.GetRegenerateTransactionID() 188 _, err = transaction.GetSignatures() 189 require.NoError(t, err) 190 } 191 192 func TestUnitAccountAllowanceApproveTransactionFromProtobuf(t *testing.T) { 193 t.Parallel() 194 195 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 196 197 tx, err := NewAccountAllowanceApproveTransaction(). 198 SetTransactionID(transactionID). 199 SetNodeAccountIDs(nodeAccountID). 200 ApproveHbarAllowance(owner, spenderAccountID1, hbarAmount). 201 ApproveTokenAllowance(tokenID1, owner, spenderAccountID1, tokenAmount). 202 ApproveTokenNftAllowance(nftID1, owner, spenderAccountID1). 203 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID1). 204 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID2). 205 Freeze() 206 require.NoError(t, err) 207 208 txFromProto := _AccountAllowanceApproveTransactionFromProtobuf(tx.Transaction, tx.build()) 209 require.Equal(t, tx, txFromProto) 210 } 211 212 func TestUnitAccountAllowanceApproveTransactionScheduleProtobuf(t *testing.T) { 213 t.Parallel() 214 215 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 216 217 tx, err := NewAccountAllowanceApproveTransaction(). 218 SetTransactionID(transactionID). 219 SetNodeAccountIDs(nodeAccountID). 220 ApproveHbarAllowance(owner, spenderAccountID1, hbarAmount). 221 ApproveTokenAllowance(tokenID1, owner, spenderAccountID1, tokenAmount). 222 ApproveTokenNftAllowance(nftID1, owner, spenderAccountID1). 223 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID1). 224 ApproveTokenNftAllowance(nftID2, owner, spenderAccountID2). 225 Freeze() 226 require.NoError(t, err) 227 228 expected := &services.SchedulableTransactionBody{ 229 TransactionFee: 200000000, 230 Data: &services.SchedulableTransactionBody_CryptoApproveAllowance{ 231 CryptoApproveAllowance: &services.CryptoApproveAllowanceTransactionBody{ 232 CryptoAllowances: []*services.CryptoAllowance{ 233 { 234 Owner: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 10}}, 235 Spender: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 7}}, 236 Amount: 100, 237 }, 238 }, 239 NftAllowances: []*services.NftAllowance{ 240 { 241 TokenId: &services.TokenID{TokenNum: 141}, 242 Owner: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 10}}, 243 Spender: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 7}}, 244 SerialNumbers: []int64{3, 4}, 245 ApprovedForAll: &wrapperspb.BoolValue{Value: false}, 246 DelegatingSpender: nil, 247 }, 248 { 249 TokenId: &services.TokenID{TokenNum: 141}, 250 Owner: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 10}}, 251 Spender: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 7890}}, 252 SerialNumbers: []int64{4}, 253 ApprovedForAll: &wrapperspb.BoolValue{Value: false}, 254 DelegatingSpender: nil, 255 }, 256 }, 257 TokenAllowances: []*services.TokenAllowance{ 258 { 259 TokenId: &services.TokenID{TokenNum: 1}, 260 Owner: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 10}}, 261 Spender: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 7}}, 262 Amount: 101, 263 }, 264 }, 265 }, 266 }, 267 } 268 actual, err := tx.buildScheduled() 269 require.NoError(t, err) 270 require.Equal(t, expected.String(), actual.String()) 271 }