github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/utilities_for_test.go (about) 1 //go:build all || unit || e2e || testnets 2 // +build all unit e2e testnets 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 "fmt" 28 "os" 29 "testing" 30 "time" 31 32 "github.com/stretchr/testify/assert" 33 34 "github.com/stretchr/testify/require" 35 ) 36 37 var mockPrivateKey string = "302e020100300506032b6570042204203b054fade7a2b0869c6bd4a63b7017cbae7855d12acc357bea718e2c3e805962" 38 39 var accountIDForTransactionID = AccountID{Account: 3} 40 var validStartForTransacionID = time.Unix(124124, 151515) 41 42 var testTransactionID TransactionID = TransactionID{ 43 AccountID: &accountIDForTransactionID, 44 ValidStart: &validStartForTransacionID, 45 } 46 47 const testClientJSON string = `{ 48 "network": { 49 "35.237.200.180:50211": "0.0.3", 50 "35.186.191.247:50211": "0.0.4", 51 "35.192.2.25:50211": "0.0.5", 52 "35.199.161.108:50211": "0.0.6", 53 "35.203.82.240:50211": "0.0.7", 54 "35.236.5.219:50211": "0.0.8", 55 "35.197.192.225:50211": "0.0.9", 56 "35.242.233.154:50211": "0.0.10", 57 "35.240.118.96:50211": "0.0.11", 58 "35.204.86.32:50211": "0.0.12" 59 }, 60 "mirrorNetwork": "testnet" 61 }` 62 63 type IntegrationTestEnv struct { 64 Client *Client 65 OperatorKey PrivateKey 66 OperatorID AccountID 67 OriginalOperatorKey PublicKey 68 OriginalOperatorID AccountID 69 NodeAccountIDs []AccountID 70 } 71 72 func NewIntegrationTestEnv(t *testing.T) IntegrationTestEnv { 73 var env IntegrationTestEnv 74 var err error 75 76 if os.Getenv("HEDERA_NETWORK") == "previewnet" { // nolint 77 env.Client = ClientForPreviewnet() 78 } else if os.Getenv("HEDERA_NETWORK") == "localhost" { 79 network := make(map[string]AccountID) 80 network["127.0.0.1:50213"] = AccountID{Account: 3} 81 mirror := []string{"127.0.0.1:5600"} 82 env.Client = ClientForNetwork(network) 83 env.Client.SetMirrorNetwork(mirror) 84 } else if os.Getenv("HEDERA_NETWORK") == "testnet" { 85 env.Client = ClientForTestnet() 86 } else if os.Getenv("CONFIG_FILE") != "" { 87 env.Client, err = ClientFromConfigFile(os.Getenv("CONFIG_FILE")) 88 } else { 89 err = fmt.Errorf("Failed to construct client from environment variables") 90 } 91 require.NoError(t, err) 92 assert.NotNil(t, env.Client) 93 94 configOperatorID := os.Getenv("OPERATOR_ID") 95 configOperatorKey := os.Getenv("OPERATOR_KEY") 96 97 if configOperatorID != "" && configOperatorKey != "" { 98 env.OperatorID, err = AccountIDFromString(configOperatorID) 99 require.NoError(t, err) 100 101 env.OperatorKey, err = PrivateKeyFromString(configOperatorKey) 102 require.NoError(t, err) 103 104 env.Client.SetOperator(env.OperatorID, env.OperatorKey) 105 } 106 107 assert.NotNil(t, env.Client.GetOperatorAccountID()) 108 assert.NotNil(t, env.Client.GetOperatorPublicKey()) 109 110 newKey, err := PrivateKeyGenerateEd25519() 111 require.NoError(t, err) 112 113 env.Client.SetMaxNodeAttempts(1) 114 env.Client.SetMinBackoff(250 * time.Millisecond) 115 env.Client.SetMaxBackoff(8 * time.Second) 116 env.Client.SetNodeMinReadmitPeriod(5 * time.Second) 117 env.Client.SetNodeMaxReadmitPeriod(1 * time.Hour) 118 env.Client.SetMaxAttempts(11) 119 env.Client.SetDefaultMaxTransactionFee(NewHbar(50)) 120 env.Client.SetDefaultMaxQueryPayment(NewHbar(50)) 121 logger := NewLogger("Hedera sdk", LoggerLevelError) 122 env.Client.SetLogger(logger) 123 124 env.OriginalOperatorID = env.Client.GetOperatorAccountID() 125 env.OriginalOperatorKey = env.Client.GetOperatorPublicKey() 126 127 resp, err := NewAccountCreateTransaction(). 128 SetKey(newKey.PublicKey()). 129 SetInitialBalance(NewHbar(150)). 130 SetAutoRenewPeriod(time.Hour*24*81 + time.Minute*26 + time.Second*39). 131 Execute(env.Client) 132 133 require.NoError(t, err) 134 135 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 136 require.NoError(t, err) 137 138 env.OperatorID = *receipt.AccountID 139 env.OperatorKey = newKey 140 env.NodeAccountIDs = []AccountID{resp.NodeID} 141 env.Client.SetOperator(env.OperatorID, env.OperatorKey) 142 143 return env 144 } 145 146 func CloseIntegrationTestEnv(env IntegrationTestEnv, token *TokenID) error { 147 var resp TransactionResponse 148 var err error 149 if token != nil { 150 deleteTokenTx, err := NewTokenDeleteTransaction(). 151 SetNodeAccountIDs(env.NodeAccountIDs). 152 SetTokenID(*token). 153 FreezeWith(env.Client) 154 if err != nil { 155 return err 156 } 157 158 resp, err = deleteTokenTx. 159 Sign(env.OperatorKey). 160 Execute(env.Client) 161 if err != nil { 162 return err 163 } 164 165 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 166 if err != nil { 167 return err 168 } 169 170 // Check if env.Client.operator is nil 171 if env.Client.operator == nil { 172 return fmt.Errorf("client operator is nil") 173 } 174 175 // This is needed, because we can't delete the account while still having tokens. 176 // This works only, because the token is deleted, otherwise the acount would need to have 0 balance of it before dissociating. 177 dissociateTx, err := NewTokenDissociateTransaction(). 178 SetAccountID(env.Client.operator.accountID). 179 SetNodeAccountIDs(env.NodeAccountIDs). 180 AddTokenID(*token). 181 Execute(env.Client) 182 if err != nil { 183 return err 184 } 185 186 _, err = dissociateTx.SetValidateStatus(true).GetReceipt(env.Client) 187 if err != nil { 188 return err 189 } 190 } 191 if os.Getenv("HEDERA_NETWORK") != "testnet" { 192 resp, err = NewAccountDeleteTransaction(). 193 SetNodeAccountIDs(env.NodeAccountIDs). 194 SetAccountID(env.OperatorID). 195 SetTransferAccountID(env.OriginalOperatorID). 196 Execute(env.Client) 197 if err != nil { 198 return err 199 } 200 201 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 202 if err != nil { 203 return err 204 } 205 } 206 207 return env.Client.Close() 208 } 209 210 func _NewMockClient() (*Client, error) { 211 privateKey, err := PrivateKeyFromString(mockPrivateKey) 212 213 if err != nil { 214 return nil, err 215 } 216 217 var net = make(map[string]AccountID) 218 net["nonexistent-testnet:56747"] = AccountID{Account: 3} 219 220 client := ClientForNetwork(net) 221 defaultNetwork := []string{"nonexistent-mirror-testnet:443"} 222 client.SetMirrorNetwork(defaultNetwork) 223 client.SetOperator(AccountID{Account: 2}, privateKey) 224 225 return client, nil 226 } 227 228 func _NewMockTransaction() (*TransferTransaction, error) { 229 privateKey, err := PrivateKeyFromString(mockPrivateKey) 230 if err != nil { 231 return &TransferTransaction{}, err 232 } 233 234 client, err := _NewMockClient() 235 if err != nil { 236 return &TransferTransaction{}, err 237 } 238 239 tx, err := NewTransferTransaction(). 240 AddHbarTransfer(AccountID{Account: 2}, HbarFromTinybar(-100)). 241 AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(100)). 242 SetTransactionID(testTransactionID). 243 SetNodeAccountIDs([]AccountID{{0, 0, 4, nil, nil, nil}}). 244 FreezeWith(client) 245 if err != nil { 246 return &TransferTransaction{}, err 247 } 248 249 tx.Sign(privateKey) 250 251 return tx, nil 252 } 253 254 type TokenCreateTransactionCustomizer func(transaction *TokenCreateTransaction) 255 256 var mintMetadata = [][]byte{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}} 257 var initialMetadata = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 258 259 func createNft(env *IntegrationTestEnv, opts ...TokenCreateTransactionCustomizer) (TokenID, error) { 260 tokenCreate := NewTokenCreateTransaction(). 261 SetTokenName("Example Collection"). 262 SetTokenSymbol("ABC"). 263 SetTokenType(TokenTypeNonFungibleUnique). 264 SetDecimals(0). 265 SetInitialSupply(0). 266 SetMaxSupply(10). 267 SetTreasuryAccountID(env.Client.GetOperatorAccountID()). 268 SetSupplyType(TokenSupplyTypeFinite). 269 SetAdminKey(env.Client.GetOperatorPublicKey()). 270 SetFreezeKey(env.Client.GetOperatorPublicKey()). 271 SetPauseKey(env.Client.GetOperatorPublicKey()). 272 SetWipeKey(env.Client.GetOperatorPublicKey()). 273 SetFeeScheduleKey(env.Client.GetOperatorPublicKey()). 274 SetSupplyKey(env.Client.GetOperatorPublicKey()). 275 SetMetadataKey(env.Client.GetOperatorPublicKey()) 276 277 for _, opt := range opts { 278 opt(tokenCreate) 279 } 280 281 tokenCreateExec, err := tokenCreate.Execute(env.Client) 282 if err != nil { 283 return TokenID{}, err 284 } 285 286 receipt, err := tokenCreateExec.SetValidateStatus(true).GetReceipt(env.Client) 287 if err != nil { 288 return TokenID{}, err 289 } 290 return *receipt.TokenID, err 291 } 292 293 func createFungibleToken(env *IntegrationTestEnv, opts ...TokenCreateTransactionCustomizer) (TokenID, error) { 294 tokenCreate := NewTokenCreateTransaction(). 295 SetNodeAccountIDs(env.NodeAccountIDs). 296 SetTokenName("ffff"). 297 SetTokenSymbol("F"). 298 SetTokenMemo("asdf"). 299 SetDecimals(18). 300 SetInitialSupply(1_000_000). 301 SetTreasuryAccountID(env.Client.GetOperatorAccountID()). 302 SetAdminKey(env.Client.GetOperatorPublicKey()). 303 SetFreezeKey(env.Client.GetOperatorPublicKey()). 304 SetPauseKey(env.Client.GetOperatorPublicKey()). 305 SetWipeKey(env.Client.GetOperatorPublicKey()). 306 SetFeeScheduleKey(env.Client.GetOperatorPublicKey()). 307 SetMetadataKey(env.Client.GetOperatorPublicKey()). 308 SetSupplyKey(env.Client.GetOperatorPublicKey()). 309 SetFreezeDefault(false) 310 311 for _, opt := range opts { 312 opt(tokenCreate) 313 } 314 315 tokenCreateExec, err := tokenCreate.Execute(env.Client) 316 if err != nil { 317 return TokenID{}, err 318 } 319 320 receipt, err := tokenCreateExec.SetValidateStatus(true).GetReceipt(env.Client) 321 if err != nil { 322 return TokenID{}, err 323 } 324 return *receipt.TokenID, err 325 } 326 327 type AccountCreateTransactionCustomizer func(transaction *AccountCreateTransaction) 328 329 func createAccount(env *IntegrationTestEnv, opts ...AccountCreateTransactionCustomizer) (AccountID, PrivateKey, error) { 330 newKey, err := PrivateKeyGenerateEd25519() 331 332 if err != nil { 333 return AccountID{}, PrivateKey{}, err 334 } 335 336 accountCreate := NewAccountCreateTransaction(). 337 SetKey(newKey). 338 SetNodeAccountIDs(env.NodeAccountIDs). 339 SetInitialBalance(NewHbar(1)) 340 341 for _, opt := range opts { 342 opt(accountCreate) 343 } 344 345 accountCreateExec, err := accountCreate.Execute(env.Client) 346 if err != nil { 347 return AccountID{}, PrivateKey{}, err 348 } 349 350 receipt, err := accountCreateExec.SetValidateStatus(true).GetReceipt(env.Client) 351 352 if err != nil { 353 return AccountID{}, PrivateKey{}, err 354 } 355 356 return *receipt.AccountID, newKey, err 357 }