github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_fee_schedule_update_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 "time" 29 30 "github.com/hashgraph/hedera-protobufs-go/services" 31 protobuf "google.golang.org/protobuf/proto" 32 33 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestUnitTokenFeeScheduleUpdateTransactionValidate(t *testing.T) { 39 t.Parallel() 40 41 client, err := _NewMockClient() 42 client.SetLedgerID(*NewLedgerIDTestnet()) 43 require.NoError(t, err) 44 client.SetAutoValidateChecksums(true) 45 accountID, err := AccountIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 tokenID, err := TokenIDFromString("0.0.123-esxsf") 48 fee := NewCustomFixedFee().SetDenominatingTokenID(tokenID).SetFeeCollectorAccountID(accountID) 49 require.NoError(t, err) 50 51 tokenFeeUpdate := NewTokenFeeScheduleUpdateTransaction(). 52 SetCustomFees([]Fee{fee}). 53 SetTokenID(tokenID) 54 55 err = tokenFeeUpdate.validateNetworkOnIDs(client) 56 require.NoError(t, err) 57 } 58 59 func TestUnitTokenFeeScheduleUpdateTransactionValidateWrong(t *testing.T) { 60 t.Parallel() 61 62 client, err := _NewMockClient() 63 client.SetLedgerID(*NewLedgerIDTestnet()) 64 require.NoError(t, err) 65 client.SetAutoValidateChecksums(true) 66 accountID, err := AccountIDFromString("0.0.123-rmkykd") 67 require.NoError(t, err) 68 tokenID, err := TokenIDFromString("0.0.123-rmkykd") 69 fee := NewCustomFixedFee().SetDenominatingTokenID(tokenID).SetFeeCollectorAccountID(accountID) 70 require.NoError(t, err) 71 72 tokenFeeUpdate := NewTokenFeeScheduleUpdateTransaction(). 73 SetCustomFees([]Fee{fee}). 74 SetTokenID(tokenID) 75 76 err = tokenFeeUpdate.validateNetworkOnIDs(client) 77 assert.Error(t, err) 78 if err != nil { 79 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 80 } 81 } 82 83 func TestUnitTokenFeeScheduleUpdateTransactionGet(t *testing.T) { 84 t.Parallel() 85 86 tokenID := TokenID{Token: 7} 87 88 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 89 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 90 91 transaction, err := NewTokenFeeScheduleUpdateTransaction(). 92 SetTransactionID(transactionID). 93 SetNodeAccountIDs(nodeAccountID). 94 SetTokenID(tokenID). 95 SetCustomFees([]Fee{}). 96 SetMaxTransactionFee(NewHbar(10)). 97 SetTransactionMemo(""). 98 SetTransactionValidDuration(60 * time.Second). 99 SetRegenerateTransactionID(false). 100 Freeze() 101 require.NoError(t, err) 102 103 transaction.GetTransactionID() 104 transaction.GetNodeAccountIDs() 105 106 _, err = transaction.GetTransactionHash() 107 require.NoError(t, err) 108 109 transaction.GetTokenID() 110 transaction.GetCustomFees() 111 transaction.GetMaxTransactionFee() 112 transaction.GetTransactionMemo() 113 transaction.GetRegenerateTransactionID() 114 _, err = transaction.GetSignatures() 115 require.NoError(t, err) 116 transaction.GetRegenerateTransactionID() 117 transaction.GetMaxTransactionFee() 118 transaction.GetRegenerateTransactionID() 119 } 120 121 func TestUnitTokenFeeScheduleUpdateTransactionNothingSet(t *testing.T) { 122 t.Parallel() 123 124 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 125 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 126 127 transaction, err := NewTokenFeeScheduleUpdateTransaction(). 128 SetTransactionID(transactionID). 129 SetNodeAccountIDs(nodeAccountID). 130 Freeze() 131 require.NoError(t, err) 132 133 transaction.GetTransactionID() 134 transaction.GetNodeAccountIDs() 135 136 _, err = transaction.GetTransactionHash() 137 require.NoError(t, err) 138 139 transaction.GetTokenID() 140 transaction.GetCustomFees() 141 transaction.GetMaxTransactionFee() 142 transaction.GetTransactionMemo() 143 transaction.GetRegenerateTransactionID() 144 _, err = transaction.GetSignatures() 145 require.NoError(t, err) 146 transaction.GetRegenerateTransactionID() 147 transaction.GetMaxTransactionFee() 148 transaction.GetRegenerateTransactionID() 149 } 150 151 func TestUnitTokenFeeScheduleUpdateTransactionProtoCheck(t *testing.T) { 152 t.Parallel() 153 154 tokenID := TokenID{Token: 7} 155 156 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 157 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 158 159 transaction, err := NewTokenFeeScheduleUpdateTransaction(). 160 SetTransactionID(transactionID). 161 SetNodeAccountIDs(nodeAccountID). 162 SetTokenID(tokenID). 163 SetCustomFees([]Fee{NewCustomFixedFee().SetHbarAmount(NewHbar(4))}). 164 SetMaxTransactionFee(NewHbar(10)). 165 SetTransactionMemo(""). 166 SetTransactionValidDuration(60 * time.Second). 167 SetRegenerateTransactionID(false). 168 Freeze() 169 require.NoError(t, err) 170 171 transaction.GetTransactionID() 172 transaction.GetNodeAccountIDs() 173 174 _, err = transaction.GetTransactionHash() 175 require.NoError(t, err) 176 177 proto := transaction.build().GetTokenFeeScheduleUpdate() 178 require.Equal(t, proto.TokenId.String(), tokenID._ToProtobuf().String()) 179 require.Equal(t, proto.CustomFees[0].Fee.(*services.CustomFee_FixedFee).FixedFee.String(), 180 NewCustomFixedFee().SetHbarAmount(NewHbar(4))._ToProtobuf().Fee.(*services.CustomFee_FixedFee).FixedFee.String()) 181 } 182 183 func TestUnitTokenFeeScheduleUpdateTransactionCoverage(t *testing.T) { 184 t.Parallel() 185 186 checksum := "dmqui" 187 grpc := time.Second * 30 188 token := TokenID{Token: 3, checksum: &checksum} 189 nodeAccountID := []AccountID{{Account: 10}} 190 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 191 192 newKey, err := PrivateKeyGenerateEd25519() 193 require.NoError(t, err) 194 195 client, err := _NewMockClient() 196 client.SetLedgerID(*NewLedgerIDTestnet()) 197 require.NoError(t, err) 198 client.SetAutoValidateChecksums(true) 199 200 transaction, err := NewTokenFeeScheduleUpdateTransaction(). 201 SetTransactionID(transactionID). 202 SetNodeAccountIDs(nodeAccountID). 203 SetTokenID(token). 204 SetCustomFees([]Fee{NewCustomFixedFee().SetHbarAmount(NewHbar(4))}). 205 SetGrpcDeadline(&grpc). 206 SetMaxTransactionFee(NewHbar(3)). 207 SetMaxRetry(3). 208 SetMaxBackoff(time.Second * 30). 209 SetMinBackoff(time.Second * 10). 210 SetTransactionMemo("no"). 211 SetTransactionValidDuration(time.Second * 30). 212 SetRegenerateTransactionID(false). 213 Freeze() 214 require.NoError(t, err) 215 216 err = transaction.validateNetworkOnIDs(client) 217 require.NoError(t, err) 218 transaction.GetTransactionID() 219 transaction.GetNodeAccountIDs() 220 transaction.GetMaxRetry() 221 transaction.GetMaxTransactionFee() 222 transaction.GetMaxBackoff() 223 transaction.GetMinBackoff() 224 transaction.GetRegenerateTransactionID() 225 byt, err := transaction.ToBytes() 226 require.NoError(t, err) 227 txFromBytes, err := TransactionFromBytes(byt) 228 require.NoError(t, err) 229 sig, err := newKey.SignTransaction(&transaction.Transaction) 230 require.NoError(t, err) 231 232 _, err = transaction.GetTransactionHash() 233 require.NoError(t, err) 234 transaction.GetMaxTransactionFee() 235 transaction.GetTransactionMemo() 236 transaction.GetRegenerateTransactionID() 237 transaction.GetTokenID() 238 transaction.GetCustomFees() 239 _, err = transaction.GetSignatures() 240 require.NoError(t, err) 241 transaction.getName() 242 switch b := txFromBytes.(type) { 243 case TokenFeeScheduleUpdateTransaction: 244 b.AddSignature(newKey.PublicKey(), sig) 245 } 246 } 247 248 func TestUnitTokenFeeScheduleUpdateTransactionMock(t *testing.T) { 249 t.Parallel() 250 251 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 252 require.NoError(t, err) 253 254 call := func(request *services.Transaction) *services.TransactionResponse { 255 require.NotEmpty(t, request.SignedTransactionBytes) 256 signedTransaction := services.SignedTransaction{} 257 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 258 259 require.NotEmpty(t, signedTransaction.BodyBytes) 260 transactionBody := services.TransactionBody{} 261 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 262 263 require.NotNil(t, transactionBody.TransactionID) 264 transactionId := transactionBody.TransactionID.String() 265 require.NotEqual(t, "", transactionId) 266 267 sigMap := signedTransaction.GetSigMap() 268 require.NotNil(t, sigMap) 269 270 return &services.TransactionResponse{ 271 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 272 } 273 } 274 responses := [][]interface{}{{ 275 call, 276 }} 277 278 client, server := NewMockClientAndServer(responses) 279 defer server.Close() 280 281 checksum := "dmqui" 282 token := TokenID{Token: 3, checksum: &checksum} 283 284 freez, err := NewTokenFeeScheduleUpdateTransaction(). 285 SetNodeAccountIDs([]AccountID{{Account: 3}}). 286 SetTokenID(token). 287 SetCustomFees([]Fee{NewCustomFixedFee().SetHbarAmount(NewHbar(4))}). 288 FreezeWith(client) 289 require.NoError(t, err) 290 291 _, err = freez.Sign(newKey).Execute(client) 292 require.NoError(t, err) 293 }