|
6 | 6 | from hiero_sdk_python.account.account_id import AccountId |
7 | 7 | from hiero_sdk_python.consensus.topic_message_submit_transaction import TopicMessageSubmitTransaction |
8 | 8 | from hiero_sdk_python.crypto.private_key import PrivateKey |
| 9 | +from hiero_sdk_python.Duration import Duration |
9 | 10 | from hiero_sdk_python.exceptions import ReceiptStatusError |
10 | 11 | from hiero_sdk_python.file.file_append_transaction import FileAppendTransaction |
11 | 12 | from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction |
|
27 | 28 | from hiero_sdk_python.transaction.transaction_id import TransactionId |
28 | 29 | from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt |
29 | 30 | from hiero_sdk_python.transaction.transaction_response import TransactionResponse |
| 31 | +from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction |
30 | 32 | from tests.unit.mock_server import mock_hedera_servers |
31 | 33 |
|
32 | 34 |
|
@@ -553,3 +555,110 @@ def test_transaction_default_max_fee(account_id): |
553 | 555 |
|
554 | 556 | assert tx_body is not None |
555 | 557 | assert tx_body.transactionFee == Hbar(2).to_tinybars() |
| 558 | + |
| 559 | + |
| 560 | +def test_build_transaction_body(): |
| 561 | + """Test that build_transaction_body create transaction body with basic fields.""" |
| 562 | + tx = TransferTransaction() |
| 563 | + transaction_body = tx.build_base_transaction_body() |
| 564 | + |
| 565 | + assert transaction_body is not None |
| 566 | + assert transaction_body.high_volume == False |
| 567 | + assert transaction_body.transactionValidDuration == Duration(120)._to_proto() |
| 568 | + # TODO: Should be change to 2hbar in future. |
| 569 | + # Since the deault fee for AbstractTokenTransfer can be remove, a default_fee is now updated to 2-hbar |
| 570 | + assert transaction_body.transactionFee == Hbar(1).to_tinybars() |
| 571 | + |
| 572 | + assert not transaction_body.HasField("transactionID") |
| 573 | + assert not transaction_body.HasField("nodeAccountID") |
| 574 | + |
| 575 | + |
| 576 | +def test_set_node_account_id_updates_node_account_ids(): |
| 577 | + """Test that setting a single node account ID updates node_account_ids.""" |
| 578 | + tx = TransferTransaction().set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 579 | + |
| 580 | + account_node_id1 = AccountId(0, 0, 3) |
| 581 | + tx.set_node_account_id(account_node_id1) |
| 582 | + |
| 583 | + assert tx.node_account_ids == [account_node_id1] |
| 584 | + assert tx.node_account_id == account_node_id1 # Deprecated |
| 585 | + |
| 586 | + # Test backward compatibility |
| 587 | + node_account_id2 = AccountId(0, 0, 4) |
| 588 | + tx.node_account_id = node_account_id2 |
| 589 | + |
| 590 | + assert tx.node_account_ids == [node_account_id2] |
| 591 | + assert tx.node_account_id == node_account_id2 # Deprecated |
| 592 | + |
| 593 | + |
| 594 | +def test_set_node_account_ids_updates_node_account_ids(): |
| 595 | + """Test that setting a node account ID list updates node_account_ids.""" |
| 596 | + tx = TransferTransaction().set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 597 | + |
| 598 | + node_account_ids1 = [AccountId(0, 0, 3), AccountId(0, 0, 4)] |
| 599 | + tx.set_node_account_ids(node_account_ids1) |
| 600 | + |
| 601 | + assert tx.node_account_ids == node_account_ids1 |
| 602 | + assert tx.node_account_id == node_account_ids1[0] # Deprecated, will return 1st element of list |
| 603 | + |
| 604 | + # Test property setter |
| 605 | + node_account_ids2 = [AccountId(0, 0, 5), AccountId(0, 0, 6)] |
| 606 | + tx.node_account_ids = node_account_ids2 |
| 607 | + |
| 608 | + assert tx.node_account_ids == node_account_ids2 |
| 609 | + assert tx.node_account_id == node_account_ids2[0] # Deprecated, will return 1st element of list |
| 610 | + |
| 611 | + |
| 612 | +def test_freeze_transaction_sets_transaction_id_and_node_ids(): |
| 613 | + """Test that freeze() preserves the transaction ID and builds body bytes for all node IDs.""" |
| 614 | + tx_id = TransactionId.generate(AccountId(0, 0, 1)) |
| 615 | + node_account_ids = [AccountId(0, 0, 3), AccountId(0, 0, 4)] |
| 616 | + |
| 617 | + tx = TransferTransaction().set_transaction_id(tx_id).set_node_account_ids(node_account_ids).freeze() |
| 618 | + |
| 619 | + assert tx.transaction_id == tx_id |
| 620 | + assert tx.node_account_ids == node_account_ids |
| 621 | + |
| 622 | + assert len(tx._transaction_body_bytes) == len(node_account_ids) |
| 623 | + assert set(tx._transaction_body_bytes.keys()) == set(node_account_ids) |
| 624 | + |
| 625 | + for node_id in node_account_ids: |
| 626 | + body_bytes = tx._transaction_body_bytes[node_id] |
| 627 | + assert body_bytes is not None |
| 628 | + assert len(body_bytes) > 0 |
| 629 | + |
| 630 | + |
| 631 | +def test_freeze_with_sets_transaction_id_and_node_ids_from_client(mock_client): |
| 632 | + """Test that freeze_with() populates the transaction ID and node IDs from the client.""" |
| 633 | + tx = TransferTransaction().freeze_with(mock_client) |
| 634 | + expected_node_ids = [node._account_id for node in mock_client.network.nodes] |
| 635 | + |
| 636 | + assert tx.transaction_id is not None |
| 637 | + assert tx.node_account_ids == expected_node_ids |
| 638 | + |
| 639 | + assert len(tx._transaction_body_bytes) == len(mock_client.network.nodes) |
| 640 | + assert set(tx._transaction_body_bytes.keys()) == set(expected_node_ids) |
| 641 | + |
| 642 | + for node_id in expected_node_ids: |
| 643 | + body_bytes = tx._transaction_body_bytes[node_id] |
| 644 | + assert body_bytes is not None |
| 645 | + assert len(body_bytes) > 0 |
| 646 | + |
| 647 | + |
| 648 | +# Deprecated, Only to test backward compatibility. |
| 649 | +def test_freeze_transaction_sets_transaction_id_and_node_id(): |
| 650 | + """Test that freeze() with a single node account ID builds the transaction body.""" |
| 651 | + tx_id = TransactionId.generate(AccountId(0, 0, 1)) |
| 652 | + node_account_id = AccountId(0, 0, 3) |
| 653 | + |
| 654 | + tx = TransferTransaction().set_transaction_id(tx_id).set_node_account_id(node_account_id).freeze() |
| 655 | + |
| 656 | + assert tx.transaction_id == tx_id |
| 657 | + assert tx.node_account_id == node_account_id |
| 658 | + |
| 659 | + assert len(tx._transaction_body_bytes) == 1 |
| 660 | + assert set(tx._transaction_body_bytes.keys()) == {node_account_id} |
| 661 | + |
| 662 | + body_bytes = tx._transaction_body_bytes[node_account_id] |
| 663 | + assert body_bytes is not None |
| 664 | + assert len(body_bytes) > 0 |
0 commit comments