-
Notifications
You must be signed in to change notification settings - Fork 93
feat: support hbar transfer #942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
prajeeta15
wants to merge
7
commits into
hiero-ledger:main
Choose a base branch
from
prajeeta15:hbar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−16
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8055d2e
fix: workflow check
prajeeta15 e846378
feat: support Hbar transfer inputs
prajeeta15 ca729cb
fix: hbar transfer
prajeeta15 58333db
fix: hbar support
prajeeta15 570565c
Merge branch 'main' into hbar
prajeeta15 d57cf45
Merge branch 'main' into hbar
prajeeta15 ed3a26f
Delete .github/workflows/pr-checks.yml
prajeeta15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,8 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1. | |
| - Added `.github/workflows/merge-conflict-bot.yml` to automatically detect and notify users of merge conflicts in Pull Requests. | ||
| - Added `.github/workflows/bot-office-hours.yml` to automate the Weekly Office Hour Reminder. | ||
| - feat: Implement account creation with EVM-style alias transaction example. | ||
| - Added validation logic in `.github/workflows/pr-checks.yml` to detect when no new chnagelog entries are added under [Unreleased]. | ||
| - Support for message chunking in `TopicSubmitMessageTransaction`. | ||
| - Added validation logic in `.github/workflows/pr-checks.yml` to detect when no new changelog entries are added under [Unreleased] | ||
| - feat: Allow `add_hbar_transfer`, `add_approved_hbar_transfer`, and internal `_add_hbar_transfer` to accept `Hbar` objects in addition to raw tinybar integers, with internal normalization to tinybars. Added tests validating the new behavior. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 34 must be move to line after 12 |
||
|
|
||
| ### Changed | ||
|
|
||
|
|
@@ -46,6 +46,10 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1. | |
|
|
||
| - fixed workflow: changelog check with improved sensitivity to deletions, additions, new releases | ||
|
|
||
| ### Breaking Changes | ||
|
|
||
| - Changed error message in `TransferTransaction._add_hbar_transfer()` and `AbstractTokenTransferTransaction._add_token_transfer()` when amount is zero from "Amount must be a non-zero integer" to "Amount must be a non-zero value." for clarity and consistency. | ||
|
|
||
| ## [0.1.9] - 2025-11-26 | ||
|
|
||
| ### Added | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import ( | ||
| SchedulableTransactionBody, | ||
| ) | ||
| from hiero_sdk_python.hbar import Hbar | ||
| from hiero_sdk_python.hbar_unit import HbarUnit | ||
| from hiero_sdk_python.tokens.nft_id import NftId | ||
| from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction | ||
|
|
||
|
|
@@ -284,11 +286,11 @@ def test_zero_amount_validation(mock_account_ids): | |
| transfer_tx = TransferTransaction() | ||
|
|
||
| # Test zero HBAR amount should raise ValueError | ||
| with pytest.raises(ValueError, match="Amount must be a non-zero integer"): | ||
| with pytest.raises(ValueError, match="Amount must be a non-zero value"): | ||
| transfer_tx.add_hbar_transfer(account_id_1, 0) | ||
|
|
||
| # Test zero token amount should raise ValueError | ||
| with pytest.raises(ValueError, match="Amount must be a non-zero integer"): | ||
| with pytest.raises(ValueError, match="Amount must be a non-zero value"): | ||
| transfer_tx.add_token_transfer(token_id_1, account_id_1, 0) | ||
|
|
||
|
|
||
|
|
@@ -510,3 +512,150 @@ def test_approved_token_transfer_validation(mock_account_ids): | |
| # Test zero amount | ||
| with pytest.raises(ValueError, match="Amount must be a non-zero integer"): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be also |
||
| transfer_tx.add_approved_token_transfer_with_decimals(token_id_1, account_id_1, 0, 6) | ||
|
|
||
|
|
||
| def test_add_hbar_transfer_with_hbar_object(mock_account_ids): | ||
| """Test adding HBAR transfers using Hbar objects.""" | ||
| account_id_sender, account_id_recipient, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| hbar_amount_sender = Hbar(-5, HbarUnit.HBAR) | ||
| hbar_amount_recipient = Hbar(5, HbarUnit.HBAR) | ||
|
|
||
| transfer_tx.add_hbar_transfer(account_id_sender, hbar_amount_sender) | ||
| transfer_tx.add_hbar_transfer(account_id_recipient, hbar_amount_recipient) | ||
|
|
||
| sender_transfer = next( | ||
| t for t in transfer_tx.hbar_transfers if t.account_id == account_id_sender | ||
| ) | ||
| recipient_transfer = next( | ||
| t for t in transfer_tx.hbar_transfers if t.account_id == account_id_recipient | ||
| ) | ||
|
|
||
| assert sender_transfer.amount == hbar_amount_sender.to_tinybars() | ||
| assert recipient_transfer.amount == hbar_amount_recipient.to_tinybars() | ||
|
|
||
|
|
||
| def test_add_hbar_transfer_with_hbar_tinybars(mock_account_ids): | ||
| """Test adding HBAR transfers using Hbar objects with tinybar unit.""" | ||
| account_id_sender, account_id_recipient, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| tinybar_amount_sender = Hbar(-5_000_000_000, HbarUnit.TINYBAR) | ||
| tinybar_amount_recipient = Hbar(5_000_000_000, HbarUnit.TINYBAR) | ||
|
|
||
| transfer_tx.add_hbar_transfer(account_id_sender, tinybar_amount_sender) | ||
| transfer_tx.add_hbar_transfer(account_id_recipient, tinybar_amount_recipient) | ||
|
|
||
| sender_transfer = next( | ||
| t for t in transfer_tx.hbar_transfers if t.account_id == account_id_sender | ||
| ) | ||
| recipient_transfer = next( | ||
| t for t in transfer_tx.hbar_transfers if t.account_id == account_id_recipient | ||
| ) | ||
|
|
||
| assert sender_transfer.amount == -5_000_000_000 | ||
| assert recipient_transfer.amount == 5_000_000_000 | ||
|
|
||
|
|
||
| def test_add_approved_hbar_transfer_with_hbar_object(mock_account_ids): | ||
| """Test adding approved HBAR transfers using Hbar objects.""" | ||
| account_id_1, account_id_2, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| hbar_amount_1 = Hbar(10, HbarUnit.HBAR) | ||
| hbar_amount_2 = Hbar(-10, HbarUnit.HBAR) | ||
|
|
||
| transfer_tx.add_approved_hbar_transfer(account_id_1, hbar_amount_1) | ||
| transfer_tx.add_approved_hbar_transfer(account_id_2, hbar_amount_2) | ||
|
|
||
| transfer_1 = next( | ||
| t for t in transfer_tx.hbar_transfers if t.account_id == account_id_1 | ||
| ) | ||
| transfer_2 = next( | ||
| t for t in transfer_tx.hbar_transfers if t.account_id == account_id_2 | ||
| ) | ||
|
|
||
| assert transfer_1.amount == hbar_amount_1.to_tinybars() | ||
| assert transfer_1.is_approved is True | ||
| assert transfer_2.amount == hbar_amount_2.to_tinybars() | ||
| assert transfer_2.is_approved is True | ||
|
|
||
|
|
||
| def test_hbar_accumulation_with_hbar_objects(mock_account_ids): | ||
| """Test that HBAR transfers accumulate correctly with Hbar objects.""" | ||
| account_id_1, _, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| hbar_amount_1 = Hbar(5, HbarUnit.HBAR) | ||
| hbar_amount_2 = Hbar(3, HbarUnit.HBAR) | ||
|
|
||
| transfer_tx.add_hbar_transfer(account_id_1, hbar_amount_1) | ||
| transfer_tx.add_hbar_transfer(account_id_1, hbar_amount_2) | ||
|
|
||
| transfer = next(t for t in transfer_tx.hbar_transfers if t.account_id == account_id_1) | ||
|
|
||
| expected_amount = (hbar_amount_1.to_tinybars() + hbar_amount_2.to_tinybars()) | ||
| assert transfer.amount == expected_amount | ||
| assert len(transfer_tx.hbar_transfers) == 1 | ||
|
|
||
|
|
||
| def test_hbar_and_int_accumulation_mixed(mock_account_ids): | ||
| """Test that HBAR objects and integer amounts accumulate together.""" | ||
| account_id_1, _, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| hbar_amount = Hbar(5, HbarUnit.HBAR) | ||
| int_amount = 3_000_000_000 | ||
|
|
||
| transfer_tx.add_hbar_transfer(account_id_1, hbar_amount) | ||
| transfer_tx.add_hbar_transfer(account_id_1, int_amount) | ||
|
|
||
| transfer = next(t for t in transfer_tx.hbar_transfers if t.account_id == account_id_1) | ||
|
|
||
| expected_amount = hbar_amount.to_tinybars() + int_amount | ||
| assert transfer.amount == expected_amount | ||
| assert len(transfer_tx.hbar_transfers) == 1 | ||
|
|
||
|
|
||
| def test_zero_hbar_transfer_validation(mock_account_ids): | ||
| """Test that zero Hbar objects are rejected.""" | ||
| account_id_1, _, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| zero_hbar = Hbar(0, HbarUnit.HBAR) | ||
|
|
||
| with pytest.raises(ValueError, match="Amount must be a non-zero value"): | ||
| transfer_tx.add_hbar_transfer(account_id_1, zero_hbar) | ||
|
|
||
|
|
||
| def test_invalid_hbar_type_validation(mock_account_ids): | ||
| """Test that invalid types are rejected.""" | ||
| account_id_1, _, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| with pytest.raises(TypeError, match="Amount must be of type int or Hbar"): | ||
| transfer_tx.add_hbar_transfer(account_id_1, "invalid") | ||
|
|
||
| with pytest.raises(TypeError, match="Amount must be of type int or Hbar"): | ||
| transfer_tx.add_hbar_transfer(account_id_1, 5.5) | ||
|
|
||
|
|
||
| def test_hbar_transfer_conversion_accuracy(mock_account_ids): | ||
| """Test that Hbar objects are accurately converted to tinybars.""" | ||
| account_id_1, account_id_2, _, _, _ = mock_account_ids | ||
| transfer_tx = TransferTransaction() | ||
|
|
||
| hbar_value = Hbar(1.5, HbarUnit.HBAR) | ||
| expected_tinybars = 150_000_000 | ||
|
|
||
| transfer_tx.add_hbar_transfer(account_id_1, hbar_value) | ||
|
|
||
| transfer = next(t for t in transfer_tx.hbar_transfers if t.account_id == account_id_1) | ||
| assert transfer.amount == expected_tinybars | ||
|
|
||
| micro_hbar_value = Hbar(1_500_000, HbarUnit.MICROBAR) | ||
| transfer_tx.add_hbar_transfer(account_id_2, micro_hbar_value) | ||
|
|
||
| transfer_2 = next(t for t in transfer_tx.hbar_transfers if t.account_id == account_id_2) | ||
| assert transfer_2.amount == expected_tinybars | ||
prajeeta15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also this should not be deleted