github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_create_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 var testMetadata = []byte{1, 2, 3, 4, 5} 39 40 func TestUnitTokenCreateTransactionValidate(t *testing.T) { 41 t.Parallel() 42 43 client, err := _NewMockClient() 44 client.SetLedgerID(*NewLedgerIDTestnet()) 45 require.NoError(t, err) 46 client.SetAutoValidateChecksums(true) 47 accountID, err := AccountIDFromString("0.0.123-esxsf") 48 require.NoError(t, err) 49 50 supple := TokenSupplyTypeFinite 51 52 println("token supply type", supple.String()) 53 54 typ := TokenTypeFungibleCommon 55 56 println("token type", typ.String()) 57 58 tokenCreate := NewTokenCreateTransaction(). 59 SetAutoRenewAccount(accountID). 60 SetTreasuryAccountID(accountID) 61 62 err = tokenCreate.validateNetworkOnIDs(client) 63 require.NoError(t, err) 64 } 65 66 func TestUnitTokenCreateTransactionValidateWrong(t *testing.T) { 67 t.Parallel() 68 69 client, err := _NewMockClient() 70 client.SetLedgerID(*NewLedgerIDTestnet()) 71 require.NoError(t, err) 72 client.SetAutoValidateChecksums(true) 73 accountID, err := AccountIDFromString("0.0.123-rmkykd") 74 require.NoError(t, err) 75 76 tokenCreate := NewTokenCreateTransaction(). 77 SetAutoRenewAccount(accountID). 78 SetTreasuryAccountID(accountID) 79 80 err = tokenCreate.validateNetworkOnIDs(client) 81 assert.Error(t, err) 82 if err != nil { 83 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 84 } 85 } 86 87 func TestUnitTokenCreateTransactionGet(t *testing.T) { 88 t.Parallel() 89 90 checksum := "dmqui" 91 grpc := time.Second * 30 92 accountID := AccountID{Account: 3, checksum: &checksum} 93 94 newKey, err := PrivateKeyGenerateEd25519() 95 require.NoError(t, err) 96 97 nodeAccountID := []AccountID{{Account: 10}} 98 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 99 100 client, err := _NewMockClient() 101 client.SetLedgerID(*NewLedgerIDTestnet()) 102 require.NoError(t, err) 103 client.SetAutoValidateChecksums(true) 104 105 transaction, err := NewTokenCreateTransaction(). 106 SetTransactionID(transactionID). 107 SetNodeAccountIDs(nodeAccountID). 108 SetTokenName("ffff"). 109 SetTokenSymbol("F"). 110 SetTokenMemo("fnord"). 111 SetDecimals(3). 112 SetCustomFees([]Fee{}). 113 SetTokenType(TokenTypeFungibleCommon). 114 SetSupplyType(TokenSupplyTypeFinite). 115 SetMaxSupply(12312123123123). 116 SetInitialSupply(1000000). 117 SetTreasuryAccountID(accountID). 118 SetAdminKey(newKey). 119 SetFreezeKey(newKey). 120 SetWipeKey(newKey). 121 SetKycKey(newKey). 122 SetSupplyKey(newKey). 123 SetPauseKey(newKey). 124 SetMetadataKey(newKey). 125 SetTokenMetadata(testMetadata). 126 SetExpirationTime(time.Now()). 127 SetFreezeDefault(false). 128 SetAutoRenewPeriod(60 * time.Second). 129 SetAutoRenewAccount(accountID). 130 SetMaxTransactionFee(NewHbar(10)). 131 SetTransactionMemo(""). 132 SetTransactionValidDuration(60 * time.Second). 133 SetRegenerateTransactionID(false). 134 SetGrpcDeadline(&grpc). 135 SetMaxRetry(3). 136 SetMaxBackoff(time.Second * 30). 137 SetMinBackoff(time.Second * 10). 138 Freeze() 139 require.NoError(t, err) 140 141 transaction.GetTransactionID() 142 transaction.GetNodeAccountIDs() 143 144 err = transaction.validateNetworkOnIDs(client) 145 require.NoError(t, err) 146 _, err = transaction.Schedule() 147 require.NoError(t, err) 148 149 _, err = transaction.GetTransactionHash() 150 require.NoError(t, err) 151 152 transaction.GetTokenName() 153 transaction.GetTokenSymbol() 154 transaction.GetTokenMemo() 155 transaction.GetDecimals() 156 transaction.GetCustomFees() 157 transaction.GetTokenType() 158 transaction.GetSupplyType() 159 transaction.GetMaxSupply() 160 transaction.GetInitialSupply() 161 transaction.GetTreasuryAccountID() 162 transaction.GetAdminKey() 163 transaction.GetFreezeKey() 164 transaction.GetWipeKey() 165 transaction.GetKycKey() 166 transaction.GetSupplyKey() 167 transaction.GetPauseKey() 168 transaction.GetMetadataKey() 169 transaction.GetTokenMetadata() 170 transaction.GetExpirationTime() 171 transaction.GetMaxTransactionFee() 172 transaction.GetTransactionMemo() 173 transaction.GetRegenerateTransactionID() 174 _, err = transaction.GetSignatures() 175 require.NoError(t, err) 176 transaction.GetRegenerateTransactionID() 177 transaction.GetMaxTransactionFee() 178 transaction.GetRegenerateTransactionID() 179 byt, err := transaction.ToBytes() 180 require.NoError(t, err) 181 txFromBytes, err := TransactionFromBytes(byt) 182 require.NoError(t, err) 183 sig, err := newKey.SignTransaction(&transaction.Transaction) 184 require.NoError(t, err) 185 transaction.getName() 186 switch b := txFromBytes.(type) { 187 case TokenCreateTransaction: 188 b.AddSignature(newKey.PublicKey(), sig) 189 } 190 } 191 192 func TestUnitTokenCreateTransactionNothingSet(t *testing.T) { 193 t.Parallel() 194 195 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 196 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 197 198 transaction, err := NewTokenCreateTransaction(). 199 SetTransactionID(transactionID). 200 SetNodeAccountIDs(nodeAccountID). 201 Freeze() 202 require.NoError(t, err) 203 204 transaction.GetTransactionID() 205 transaction.GetNodeAccountIDs() 206 207 _, err = transaction.GetTransactionHash() 208 require.NoError(t, err) 209 210 transaction.GetTokenName() 211 transaction.GetTokenSymbol() 212 transaction.GetTokenMemo() 213 transaction.GetDecimals() 214 transaction.GetCustomFees() 215 transaction.GetTokenType() 216 transaction.GetSupplyType() 217 transaction.GetMaxSupply() 218 transaction.GetInitialSupply() 219 transaction.GetTreasuryAccountID() 220 transaction.GetAdminKey() 221 transaction.GetFreezeKey() 222 transaction.GetWipeKey() 223 transaction.GetKycKey() 224 transaction.GetSupplyKey() 225 transaction.GetPauseKey() 226 transaction.GetMetadataKey() 227 transaction.GetExpirationTime() 228 transaction.GetMaxTransactionFee() 229 transaction.GetTransactionMemo() 230 transaction.GetRegenerateTransactionID() 231 _, err = transaction.GetSignatures() 232 require.NoError(t, err) 233 transaction.GetRegenerateTransactionID() 234 transaction.GetMaxTransactionFee() 235 transaction.GetRegenerateTransactionID() 236 proto := transaction.build().GetTokenCreation() 237 require.Nil(t, proto.AutoRenewAccount) 238 require.Nil(t, proto.AdminKey) 239 require.Nil(t, proto.Expiry) 240 require.Nil(t, proto.FeeScheduleKey) 241 require.Nil(t, proto.FreezeKey) 242 require.Nil(t, proto.KycKey) 243 require.Nil(t, proto.FeeScheduleKey) 244 require.Nil(t, proto.PauseKey) 245 require.Nil(t, proto.SupplyKey) 246 require.Nil(t, proto.MetadataKey) 247 require.Nil(t, proto.Metadata) 248 require.Nil(t, proto.Treasury) 249 } 250 251 func TestUnitTokenCreateTransactionKeyCheck(t *testing.T) { 252 t.Parallel() 253 254 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 255 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 256 257 keys := make([]PrivateKey, 8) 258 var err error 259 260 for i := 0; i < len(keys); i++ { 261 keys[i], err = PrivateKeyGenerateEd25519() 262 require.NoError(t, err) 263 } 264 265 transaction, err := NewTokenCreateTransaction(). 266 SetTransactionID(transactionID). 267 SetNodeAccountIDs(nodeAccountID). 268 SetAdminKey(keys[0]). 269 SetFreezeKey(keys[1]). 270 SetWipeKey(keys[2]). 271 SetKycKey(keys[3]). 272 SetSupplyKey(keys[4]). 273 SetPauseKey(keys[5]). 274 SetFeeScheduleKey(keys[6]). 275 SetMetadataKey(keys[7]). 276 Freeze() 277 require.NoError(t, err) 278 279 transaction.GetTransactionID() 280 transaction.GetNodeAccountIDs() 281 282 proto := transaction.build().GetTokenCreation() 283 require.Equal(t, proto.AdminKey.String(), keys[0]._ToProtoKey().String()) 284 require.Equal(t, proto.FreezeKey.String(), keys[1]._ToProtoKey().String()) 285 require.Equal(t, proto.WipeKey.String(), keys[2]._ToProtoKey().String()) 286 require.Equal(t, proto.KycKey.String(), keys[3]._ToProtoKey().String()) 287 require.Equal(t, proto.SupplyKey.String(), keys[4]._ToProtoKey().String()) 288 require.Equal(t, proto.PauseKey.String(), keys[5]._ToProtoKey().String()) 289 require.Equal(t, proto.FeeScheduleKey.String(), keys[6]._ToProtoKey().String()) 290 require.Equal(t, proto.MetadataKey.String(), keys[7]._ToProtoKey().String()) 291 } 292 293 func TestUnitTokenCreateTransactionMock(t *testing.T) { 294 t.Parallel() 295 296 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 297 require.NoError(t, err) 298 299 call := func(request *services.Transaction) *services.TransactionResponse { 300 require.NotEmpty(t, request.SignedTransactionBytes) 301 signedTransaction := services.SignedTransaction{} 302 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 303 304 require.NotEmpty(t, signedTransaction.BodyBytes) 305 transactionBody := services.TransactionBody{} 306 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 307 308 require.NotNil(t, transactionBody.TransactionID) 309 transactionId := transactionBody.TransactionID.String() 310 require.NotEqual(t, "", transactionId) 311 312 sigMap := signedTransaction.GetSigMap() 313 require.NotNil(t, sigMap) 314 315 return &services.TransactionResponse{ 316 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 317 } 318 } 319 responses := [][]interface{}{{ 320 call, 321 }} 322 323 client, server := NewMockClientAndServer(responses) 324 defer server.Close() 325 326 checksum := "dmqui" 327 accountID := AccountID{Account: 3, checksum: &checksum} 328 329 freez, err := NewTokenCreateTransaction(). 330 SetNodeAccountIDs([]AccountID{{Account: 3}}). 331 SetTreasuryAccountID(accountID). 332 SetAdminKey(newKey). 333 SetFreezeKey(newKey). 334 SetWipeKey(newKey). 335 SetKycKey(newKey). 336 SetSupplyKey(newKey). 337 SetPauseKey(newKey). 338 SetTokenMetadata(testMetadata). 339 SetMetadataKey(newKey). 340 FreezeWith(client) 341 require.NoError(t, err) 342 343 _, err = freez.Sign(newKey).Execute(client) 344 require.NoError(t, err) 345 } 346 347 func TestUnitTokenCreateAutoRenewAccountSetting(t *testing.T) { 348 client, err := _NewMockClient() 349 autoRenewAccount := AccountID{Account: 3} 350 frozenTx, err := NewTokenCreateTransaction(). 351 SetAutoRenewAccount(autoRenewAccount). 352 SetAutoRenewPeriod(24 * time.Hour). 353 FreezeWith(client) 354 require.NoError(t, err) 355 require.Equal(t, autoRenewAccount, frozenTx.GetAutoRenewAccount()) 356 }