Skip to content

Conversation

@prajeeta15
Copy link
Contributor

Add Hbar object support to TransferTransaction and add tests

Allow _add_hbar_transfer, add_hbar_transfer, and add_approved_hbar_transfer to accept Hbar objects in addition to raw integer tinybar amounts. Normalize Hbar inputs to tinybars immediately (via Hbar.to_tinybars()), store the resulting tinybar integer in HbarTransfer, and keep existing integer behavior intact. Add comprehensive unit tests covering the new behavior and edge cases.

  • Accept Union[int, Hbar] for _add_hbar_transfer, add_hbar_transfer, and add_approved_hbar_transfer.
  • Add runtime type validation and raise TypeError for invalid account_id, amount, or is_approved types.
  • Normalize Hbar to tinybars via Hbar.to_tinybars() and store the tinybar integer in HbarTransfer.
  • Reject zero-value transfers (ValueError for zero tinybar amounts).
  • Accumulate multiple transfers to the same account_id by adding tinybars.
  • Add unit tests (tests/unit/transaction/test_transfer_transaction.py) that cover:
    • constructor behavior with pre-populated transfers,
    • adding HBAR transfers with integer tinybars,
    • adding HBAR transfers with Hbar objects (HBAR and TINYBAR units),
    • approved HBAR transfers with Hbar objects,
    • accumulation for mixed Hbar and int inputs,
    • zero-value validation for ints and Hbar(0),
    • invalid-type validation for amount,
    • conversion accuracy from various Hbar units to tinybars,
    • existing token and NFT transfer behavior, building transaction and scheduled bodies, and frozen transaction behavior.
  • Update changelog with an Added entry describing the feature and tests.

Related issue(s):

Fixes #917

Notes for reviewer:

  • Backwards compatible: integer inputs behave exactly as before.
  • Hbar inputs are normalized immediately to tinybars; the Hbar object is not stored—only the tinybar integer is stored in HbarTransfer.
  • Type validation:
    • account_id must be an AccountId instance (TypeError otherwise).
    • amount must be an int or Hbar (TypeError otherwise).
    • is_approved must be bool (TypeError otherwise).
  • Zero amounts are rejected with ValueError("Amount must be a non-zero value.").
  • Tests added exercise both positive and negative amounts, edge large/small integer boundaries, token/NFT behavior, and frozen transaction immutability.
  • Suggested reviewers should pay attention to any consumers of HbarTransfer (ensure they expect tinybar integers) and to Hbar.to_tinybars() behavior.

Checklist

  • Documented (Code comments, README, changelog entry)
  • Tested (unit, integration, etc.)

@exploreriii
Copy link
Contributor

@tech0priyanshu if you are available for a review

Copy link
Contributor

@exploreriii exploreriii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @prajeeta15 this branch I think was a branch from your workflow check? or a non-updated main?
You are submitting https://github.com/hiero-ledger/hiero-sdk-python/pull/942/files
two PRs: one for PR check and one for hbar

Please make sure to pull latest changes to main, ensure your origin is matching the upstream main, and then rebase your branch with that -- you may want to make a new branch

@prajeeta15
Copy link
Contributor Author

i switched branches (just before push) to push the latest changes so I think the last PR commit (from old branch) got here too (git push oldbranch: new branch) .. but that was already merged and I had taken latest upstream from main.
the old PR was already merged so I don't think it would create a problem ? idk

@exploreriii
Copy link
Contributor

Over the weekend we had to apply a hot fix, and pulled split the workflow in two
Recommendation always pull from main, else, can check the diff from main before publishing

tinybar = amount if isinstance(amount, int) else amount.to_tinybars()

if tinybar == 0:
raise ValueError("Amount must be a non-zero value.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed the error return to:
raise ValueError("Amount must be a non-zero value.")
Please add a Breaking Change section
and briefly document that small breaking change

aditinally, make sure your tests are changed to grab this new error message

transfer_tx = TransferTransaction()

hbar_value = Hbar(1.5, HbarUnit.HBAR)
expected_tinybars = 1_500_000_000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conversion is incorrcet

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also edit
tests/integration/transfer_transaction_e2e_test.py
to test hbar units?

@github-actions
Copy link

github-actions bot commented Dec 3, 2025

Hello, this is the Office Hour Bot.

This is a reminder that the Hiero Python SDK Office Hours are scheduled in approximately 4 hours (14:00 UTC).

This session provides an opportunity to ask questions regarding this Pull Request or receive assistance from a maintainer.

Details:

Disclaimer: This is an automated reminder. Please verify the schedule here to be notified of any changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow TransferTransaction add_hbar_transfer to handle Hbar as an input type

2 participants