github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_wipe_transaction_e2e_test.go (about) 1 //go:build all || e2e 2 // +build all e2e 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 "testing" 29 30 "github.com/stretchr/testify/assert" 31 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestIntegrationTokenWipeTransactionCanExecute(t *testing.T) { 36 t.Parallel() 37 env := NewIntegrationTestEnv(t) 38 39 newKey, err := PrivateKeyGenerateEd25519() 40 require.NoError(t, err) 41 42 newBalance := NewHbar(2) 43 44 assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar) 45 46 resp, err := NewAccountCreateTransaction(). 47 SetNodeAccountIDs(env.NodeAccountIDs). 48 SetKey(newKey.PublicKey()). 49 SetInitialBalance(newBalance). 50 Execute(env.Client) 51 require.NoError(t, err) 52 53 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 54 require.NoError(t, err) 55 56 accountID := *receipt.AccountID 57 58 tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) { 59 transaction.SetKycKey(env.Client.GetOperatorPublicKey()) 60 }) 61 require.NoError(t, err) 62 63 transaction, err := NewTokenAssociateTransaction(). 64 SetNodeAccountIDs([]AccountID{resp.NodeID}). 65 SetAccountID(accountID). 66 SetTokenIDs(tokenID). 67 FreezeWith(env.Client) 68 require.NoError(t, err) 69 70 resp, err = transaction. 71 Sign(newKey). 72 Execute(env.Client) 73 require.NoError(t, err) 74 75 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 76 require.NoError(t, err) 77 78 resp, err = NewTokenGrantKycTransaction(). 79 SetNodeAccountIDs([]AccountID{resp.NodeID}). 80 SetAccountID(accountID). 81 SetTokenID(tokenID). 82 Execute(env.Client) 83 require.NoError(t, err) 84 85 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 86 require.NoError(t, err) 87 88 resp, err = NewTransferTransaction(). 89 SetNodeAccountIDs([]AccountID{resp.NodeID}). 90 AddTokenTransfer(tokenID, env.Client.GetOperatorAccountID(), -100). 91 AddTokenTransfer(tokenID, accountID, 100). 92 Execute(env.Client) 93 require.NoError(t, err) 94 95 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 96 require.NoError(t, err) 97 98 info, err := NewAccountBalanceQuery(). 99 SetAccountID(accountID). 100 SetNodeAccountIDs([]AccountID{resp.NodeID}). 101 Execute(env.Client) 102 require.NoError(t, err) 103 104 var value uint64 105 for balanceTokenID, balance := range info.Token { 106 if tokenID.String() == balanceTokenID.String() { 107 value = balance 108 } 109 } 110 111 // TODO: assert.Equal(t, uint64(100), value) 112 113 resp, err = NewTokenWipeTransaction(). 114 SetNodeAccountIDs([]AccountID{resp.NodeID}). 115 SetTokenID(tokenID). 116 SetAccountID(accountID). 117 SetAmount(100). 118 Execute(env.Client) 119 require.NoError(t, err) 120 121 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 122 require.NoError(t, err) 123 124 info, err = NewAccountBalanceQuery(). 125 SetAccountID(accountID). 126 SetNodeAccountIDs([]AccountID{resp.NodeID}). 127 Execute(env.Client) 128 require.NoError(t, err) 129 130 for balanceTokenID, balance := range info.Token { 131 if tokenID.String() == balanceTokenID.String() { 132 value = balance 133 } 134 } 135 136 assert.Equal(t, uint64(0), value) 137 138 tx, err := NewAccountDeleteTransaction(). 139 SetAccountID(accountID). 140 SetTransferAccountID(env.Client.GetOperatorAccountID()). 141 FreezeWith(env.Client) 142 require.NoError(t, err) 143 144 resp, err = tx. 145 Sign(newKey). 146 Execute(env.Client) 147 require.NoError(t, err) 148 149 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 150 require.NoError(t, err) 151 152 err = CloseIntegrationTestEnv(env, &tokenID) 153 require.NoError(t, err) 154 } 155 156 func TestIntegrationTokenWipeTransactionNoAmount(t *testing.T) { 157 t.Parallel() 158 env := NewIntegrationTestEnv(t) 159 160 newKey, err := PrivateKeyGenerateEd25519() 161 require.NoError(t, err) 162 163 newBalance := NewHbar(2) 164 165 assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar) 166 167 resp, err := NewAccountCreateTransaction(). 168 SetNodeAccountIDs(env.NodeAccountIDs). 169 SetKey(newKey.PublicKey()). 170 SetInitialBalance(newBalance). 171 Execute(env.Client) 172 require.NoError(t, err) 173 174 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 175 require.NoError(t, err) 176 177 accountID := *receipt.AccountID 178 179 tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) { 180 transaction.SetKycKey(env.Client.GetOperatorPublicKey()) 181 }) 182 require.NoError(t, err) 183 184 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 185 require.NoError(t, err) 186 187 nodeID := resp.NodeID 188 189 transaction, err := NewTokenAssociateTransaction(). 190 SetNodeAccountIDs([]AccountID{nodeID}). 191 SetAccountID(accountID). 192 SetTokenIDs(tokenID). 193 FreezeWith(env.Client) 194 require.NoError(t, err) 195 196 resp, err = transaction. 197 Sign(newKey). 198 Execute(env.Client) 199 require.NoError(t, err) 200 201 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 202 require.NoError(t, err) 203 204 resp, err = NewTokenGrantKycTransaction(). 205 SetNodeAccountIDs([]AccountID{nodeID}). 206 SetAccountID(accountID). 207 SetTokenID(tokenID). 208 Execute(env.Client) 209 require.NoError(t, err) 210 211 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 212 require.NoError(t, err) 213 214 resp, err = NewTransferTransaction(). 215 SetNodeAccountIDs([]AccountID{nodeID}). 216 AddTokenTransfer(tokenID, env.Client.GetOperatorAccountID(), -10). 217 AddTokenTransfer(tokenID, accountID, 10). 218 Execute(env.Client) 219 require.NoError(t, err) 220 221 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 222 require.NoError(t, err) 223 224 resp2, err := NewTokenWipeTransaction(). 225 SetNodeAccountIDs([]AccountID{nodeID}). 226 SetTokenID(tokenID). 227 SetAccountID(accountID). 228 Execute(env.Client) 229 230 // It is now possible to perform Zero Token Operations https://hips.hedera.com/hip/hip-564 231 receipt, err = resp2.SetValidateStatus(true).GetReceipt(env.Client) 232 require.NoError(t, err) 233 234 tx, err := NewAccountDeleteTransaction(). 235 SetAccountID(accountID). 236 SetTransferAccountID(env.Client.GetOperatorAccountID()). 237 FreezeWith(env.Client) 238 require.NoError(t, err) 239 240 resp, err = tx. 241 Sign(newKey). 242 Execute(env.Client) 243 require.NoError(t, err) 244 245 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 246 assert.Error(t, err) 247 if err != nil { 248 assert.Equal(t, "exceptional receipt status: TRANSACTION_REQUIRES_ZERO_TOKEN_BALANCES", err.Error()) 249 } 250 251 err = CloseIntegrationTestEnv(env, &tokenID) 252 require.NoError(t, err) 253 } 254 255 func TestIntegrationTokenWipeTransactionNoTokenID(t *testing.T) { 256 t.Parallel() 257 env := NewIntegrationTestEnv(t) 258 259 newKey, err := PrivateKeyGenerateEd25519() 260 require.NoError(t, err) 261 262 newBalance := NewHbar(2) 263 264 assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar) 265 266 resp, err := NewAccountCreateTransaction(). 267 SetNodeAccountIDs(env.NodeAccountIDs). 268 SetKey(newKey.PublicKey()). 269 SetInitialBalance(newBalance). 270 Execute(env.Client) 271 require.NoError(t, err) 272 273 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 274 require.NoError(t, err) 275 276 accountID := *receipt.AccountID 277 278 tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) { 279 transaction.SetKycKey(env.Client.GetOperatorPublicKey()) 280 }) 281 require.NoError(t, err) 282 283 transaction, err := NewTokenAssociateTransaction(). 284 SetNodeAccountIDs([]AccountID{resp.NodeID}). 285 SetAccountID(accountID). 286 SetTokenIDs(tokenID). 287 FreezeWith(env.Client) 288 require.NoError(t, err) 289 290 resp, err = transaction. 291 Sign(newKey). 292 Execute(env.Client) 293 require.NoError(t, err) 294 295 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 296 require.NoError(t, err) 297 298 resp, err = NewTokenGrantKycTransaction(). 299 SetNodeAccountIDs([]AccountID{resp.NodeID}). 300 SetAccountID(accountID). 301 SetTokenID(tokenID). 302 Execute(env.Client) 303 require.NoError(t, err) 304 305 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 306 require.NoError(t, err) 307 308 resp, err = NewTransferTransaction(). 309 SetNodeAccountIDs([]AccountID{resp.NodeID}). 310 AddTokenTransfer(tokenID, env.Client.GetOperatorAccountID(), -10). 311 AddTokenTransfer(tokenID, accountID, 10). 312 Execute(env.Client) 313 require.NoError(t, err) 314 315 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 316 require.NoError(t, err) 317 318 resp2, err := NewTokenWipeTransaction(). 319 SetNodeAccountIDs([]AccountID{resp.NodeID}). 320 SetAccountID(accountID). 321 SetAmount(10). 322 Execute(env.Client) 323 assert.Error(t, err) 324 if err != nil { 325 assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_TOKEN_ID received for transaction %s", resp2.TransactionID), err.Error()) 326 } 327 328 tx, err := NewAccountDeleteTransaction(). 329 SetAccountID(accountID). 330 SetTransferAccountID(env.Client.GetOperatorAccountID()). 331 FreezeWith(env.Client) 332 require.NoError(t, err) 333 334 resp, err = tx. 335 Sign(newKey). 336 Execute(env.Client) 337 require.NoError(t, err) 338 339 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 340 assert.Error(t, err) 341 if err != nil { 342 assert.Equal(t, "exceptional receipt status: TRANSACTION_REQUIRES_ZERO_TOKEN_BALANCES", err.Error()) 343 } 344 345 err = CloseIntegrationTestEnv(env, &tokenID) 346 require.NoError(t, err) 347 } 348 349 func TestIntegrationTokenWipeTransactionNoAccountID(t *testing.T) { 350 t.Parallel() 351 env := NewIntegrationTestEnv(t) 352 353 newKey, err := PrivateKeyGenerateEd25519() 354 require.NoError(t, err) 355 356 newBalance := NewHbar(2) 357 358 assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar) 359 360 resp, err := NewAccountCreateTransaction(). 361 SetNodeAccountIDs(env.NodeAccountIDs). 362 SetKey(newKey.PublicKey()). 363 SetInitialBalance(newBalance). 364 Execute(env.Client) 365 require.NoError(t, err) 366 367 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 368 require.NoError(t, err) 369 370 accountID := *receipt.AccountID 371 372 tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) { 373 transaction.SetKycKey(env.Client.GetOperatorPublicKey()) 374 }) 375 require.NoError(t, err) 376 377 nodeID := resp.NodeID 378 379 transaction, err := NewTokenAssociateTransaction(). 380 SetNodeAccountIDs([]AccountID{nodeID}). 381 SetAccountID(accountID). 382 SetTokenIDs(tokenID). 383 FreezeWith(env.Client) 384 require.NoError(t, err) 385 386 resp, err = transaction. 387 Sign(newKey). 388 Execute(env.Client) 389 require.NoError(t, err) 390 391 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 392 require.NoError(t, err) 393 394 resp, err = NewTokenGrantKycTransaction(). 395 SetNodeAccountIDs([]AccountID{nodeID}). 396 SetAccountID(accountID). 397 SetTokenID(tokenID). 398 Execute(env.Client) 399 require.NoError(t, err) 400 401 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 402 require.NoError(t, err) 403 404 resp, err = NewTransferTransaction(). 405 SetNodeAccountIDs([]AccountID{nodeID}). 406 AddTokenTransfer(tokenID, env.Client.GetOperatorAccountID(), -10). 407 AddTokenTransfer(tokenID, accountID, 10). 408 Execute(env.Client) 409 require.NoError(t, err) 410 411 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 412 require.NoError(t, err) 413 414 resp2, err := NewTokenWipeTransaction(). 415 SetNodeAccountIDs([]AccountID{nodeID}). 416 SetTokenID(tokenID). 417 SetAmount(10). 418 Execute(env.Client) 419 assert.Error(t, err) 420 if err != nil { 421 assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID received for transaction %s", resp2.TransactionID), err.Error()) 422 } 423 424 tx, err := NewAccountDeleteTransaction(). 425 SetAccountID(accountID). 426 SetTransferAccountID(env.Client.GetOperatorAccountID()). 427 FreezeWith(env.Client) 428 require.NoError(t, err) 429 430 resp, err = tx. 431 Sign(newKey). 432 Execute(env.Client) 433 require.NoError(t, err) 434 435 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 436 assert.Error(t, err) 437 if err != nil { 438 assert.Equal(t, "exceptional receipt status: TRANSACTION_REQUIRES_ZERO_TOKEN_BALANCES", err.Error()) 439 } 440 441 err = CloseIntegrationTestEnv(env, &tokenID) 442 require.NoError(t, err) 443 } 444 445 func TestIntegrationTokenWipeTransactionNotZeroTokensAtDelete(t *testing.T) { 446 t.Parallel() 447 env := NewIntegrationTestEnv(t) 448 449 newKey, err := PrivateKeyGenerateEd25519() 450 require.NoError(t, err) 451 452 newBalance := NewHbar(2) 453 454 assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar) 455 456 resp, err := NewAccountCreateTransaction(). 457 SetNodeAccountIDs(env.NodeAccountIDs). 458 SetKey(newKey.PublicKey()). 459 SetInitialBalance(newBalance). 460 Execute(env.Client) 461 require.NoError(t, err) 462 463 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 464 require.NoError(t, err) 465 466 accountID := *receipt.AccountID 467 468 tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) { 469 transaction.SetKycKey(env.Client.GetOperatorPublicKey()) 470 }) 471 require.NoError(t, err) 472 473 transaction, err := NewTokenAssociateTransaction(). 474 SetNodeAccountIDs([]AccountID{resp.NodeID}). 475 SetAccountID(accountID). 476 SetTokenIDs(tokenID). 477 FreezeWith(env.Client) 478 require.NoError(t, err) 479 480 resp, err = transaction. 481 Sign(newKey). 482 Execute(env.Client) 483 require.NoError(t, err) 484 485 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 486 require.NoError(t, err) 487 488 resp, err = NewTokenGrantKycTransaction(). 489 SetNodeAccountIDs([]AccountID{resp.NodeID}). 490 SetAccountID(accountID). 491 SetTokenID(tokenID). 492 Execute(env.Client) 493 require.NoError(t, err) 494 495 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 496 require.NoError(t, err) 497 498 resp, err = NewTransferTransaction(). 499 SetNodeAccountIDs([]AccountID{resp.NodeID}). 500 AddTokenTransfer(tokenID, env.Client.GetOperatorAccountID(), -100). 501 AddTokenTransfer(tokenID, accountID, 100). 502 Execute(env.Client) 503 require.NoError(t, err) 504 505 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 506 require.NoError(t, err) 507 508 info, err := NewAccountBalanceQuery(). 509 SetAccountID(accountID). 510 SetNodeAccountIDs([]AccountID{resp.NodeID}). 511 Execute(env.Client) 512 require.NoError(t, err) 513 514 var value uint64 515 for balanceTokenID, balance := range info.Token { 516 if tokenID.String() == balanceTokenID.String() { 517 value = balance 518 } 519 } 520 521 assert.Equal(t, value, value) 522 // TODO: assert.Equal(t, value, uint64(100)) 523 524 resp, err = NewTokenWipeTransaction(). 525 SetNodeAccountIDs([]AccountID{resp.NodeID}). 526 SetTokenID(tokenID). 527 SetAccountID(accountID). 528 SetAmount(10). 529 Execute(env.Client) 530 require.NoError(t, err) 531 532 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 533 require.NoError(t, err) 534 535 info, err = NewAccountBalanceQuery(). 536 SetAccountID(accountID). 537 SetNodeAccountIDs([]AccountID{resp.NodeID}). 538 Execute(env.Client) 539 require.NoError(t, err) 540 541 for balanceTokenID, balance := range info.Token { 542 if tokenID.String() == balanceTokenID.String() { 543 value = balance 544 } 545 } 546 547 // TODO: assert.Equal(t, value, uint64(90)) 548 549 tx, err := NewAccountDeleteTransaction(). 550 SetAccountID(accountID). 551 SetTransferAccountID(env.Client.GetOperatorAccountID()). 552 FreezeWith(env.Client) 553 require.NoError(t, err) 554 555 resp, err = tx. 556 Sign(newKey). 557 Execute(env.Client) 558 require.NoError(t, err) 559 560 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 561 assert.Error(t, err) 562 if err != nil { 563 assert.Equal(t, "exceptional receipt status: TRANSACTION_REQUIRES_ZERO_TOKEN_BALANCES", err.Error()) 564 } 565 566 err = CloseIntegrationTestEnv(env, &tokenID) 567 require.NoError(t, err) 568 } 569 570 func DisabledTestIntegrationTokenWipeTransactionNftsIfNotOwned(t *testing.T) { // nolint 571 t.Parallel() 572 env := NewIntegrationTestEnv(t) 573 574 newBalance := NewHbar(2) 575 576 assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar) 577 578 tokenID, err := createNft(&env) 579 require.NoError(t, err) 580 581 metaData := []byte{50, 50} 582 583 mint, err := NewTokenMintTransaction(). 584 SetTokenID(tokenID). 585 SetMetadata(metaData). 586 Execute(env.Client) 587 require.NoError(t, err) 588 589 _, err = mint.SetValidateStatus(true).GetReceipt(env.Client) 590 require.NoError(t, err) 591 592 newKey, err := PrivateKeyGenerateEd25519() 593 require.NoError(t, err) 594 595 resp, err := NewAccountCreateTransaction(). 596 SetKey(newKey). 597 SetNodeAccountIDs(env.NodeAccountIDs). 598 SetInitialBalance(NewHbar(2)). 599 Execute(env.Client) 600 require.NoError(t, err) 601 602 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 603 require.NoError(t, err) 604 605 accountID := *receipt.AccountID 606 607 freezeAssociate, err := NewTokenAssociateTransaction(). 608 SetAccountID(accountID). 609 SetTokenIDs(tokenID). 610 SetNodeAccountIDs(env.NodeAccountIDs). 611 FreezeWith(env.Client) 612 require.NoError(t, err) 613 614 freezeAssociate.Sign(newKey) 615 616 resp, err = freezeAssociate.Execute(env.Client) 617 require.NoError(t, err) 618 619 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 620 require.NoError(t, err) 621 622 resp, err = NewTokenGrantKycTransaction(). 623 SetAccountID(accountID). 624 SetTokenID(tokenID). 625 Execute(env.Client) 626 require.NoError(t, err) 627 628 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 629 require.NoError(t, err) 630 631 tx, err := NewAccountDeleteTransaction(). 632 SetAccountID(accountID). 633 SetNodeAccountIDs(env.NodeAccountIDs). 634 SetTransactionID(TransactionIDGenerate(accountID)). 635 FreezeWith(env.Client) 636 require.NoError(t, err) 637 638 tx = tx.Sign(newKey) 639 640 resp, err = tx.Execute(env.Client) 641 require.NoError(t, err) 642 643 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 644 require.NoError(t, err) 645 }