Skip to content

Allow TransferTransaction add_hbar_transfer to handle Hbar as an input type #917

@manishdait

Description

@manishdait

Problem

The current Python SDK only accepts raw integer amounts (in tinybars) when during add_hbar_transfer transfers. It does not support passing an Hbar object directly. Users must manually convert HBAR values to tinybars.

Solution

  • Allow _add_hbar_transfer, add_hbar_transfer and add_approved_hbar_transfer to accept Hbar as an input type.
   def _add_hbar_transfer(self, account_id: AccountId, amount: Union[int, Hbar], is_approved: bool = False) -> "TransferTransaction":
       ...
       

    def add_hbar_transfer(self, account_id: AccountId, amount: Union[int, Hbar]) -> "TransferTransaction":
        ...

    def add_approved_hbar_transfer(self, account_id: AccountId, amount: Union[int, Hbar]) -> "TransferTransaction":
       ...
  • Internally normalize all amounts to tinybars.
def _add_hbar_transfer(
        self, account_id: AccountId, amount: Union[int, Hbar], is_approved: bool = False
    ) -> "TransferTransaction":
       ...
        if not isinstance(amount, (int, Hbar)):
            raise ValueError("Amount must be of type int or Hbar.")
        
        tinybar = amount if isinstance(amount, int) else amount.to_tinybars()

        if tinybar == 0:
            raise ValueError("Ammount must be a non zero value.")

        for transfer in self.hbar_transfers:
            if transfer.account_id == account_id:
                transfer.amount += tinybar
                return self

        self.hbar_transfers.append(HbarTransfer(account_id, amount, is_approved))
        return self
  • Create unit/integration test to validate the changes.

📋 Step-by-Step Contribution Guide

If you have never contributed to an open source project at GitHub, the following step-by-step guide will introduce you to the workflow.

    - [ ] **Claim this issue:** Comment below that you are interested in working on the issue. Without assignment, your pull requests might be closed and the issue given to another developer.
    - [ ] **Wait for assignment:** A community member with the given rights will add you as an assignee of the issue
    - [ ] **Fork, Branch and Work on the issue:** Create a copy of the repository, create a branch for the issue and solve the problem. For instructions, please read our [Contributing guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/CONTRIBUTING.md) file. Further help can be found at [Set-up Training](https://github.com/hiero-ledger/hiero-sdk-python/tree/main/docs/sdk_developers/training/setup) and [Workflow Training](https://github.com/hiero-ledger/hiero-sdk-python/tree/main/docs/sdk_developers/training/workflow).
    - [ ] **DCO and GPG key sign each commit :** each commit must be -s and -S signed. An explanation on how to do this is at [Signing Guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/signing.md)
    - [ ] **Add a Changelog Entry :** your pull request will require a changelog. Read [Changelog Entry Guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/changelog_entry.md) to learn how.
    - [ ] **Push and Create a Pull Request :** Once your issue is resolved, and your commits are signed, and you have a changelog entry, push your changes and create a pull request. Detailed instructions can be found at [Submit PR Training](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/training/workflow/11_submit_pull_request.md), part of [Workflow Training](https://github.com/hiero-ledger/hiero-sdk-python/tree/main/docs/sdk_developers/training/workflow).
    - [ ] **You did it 🎉:** A maintainer or committer will review your pull request and provide feedback. If approved, we will merge the fix in the main branch. Thanks for being part of the Hiero community as an open-source contributor ❤️

    ***IMPORTANT*** Your pull request CANNOT BE MERGED until you add a changelog entry AND sign your commits each with `git commit -S -s -m "chore: your commit message"` with a GPG key setup.

🤔 Additional Information

For more help, we have extensive documentation attributes:

Additionally, we invite you to join our community on our Discord server.

We also invite you to attend each Wednesday, 2pm UTC our Python SDK Office Hour and Community Calls. The Python SDK Office hour is for hands-on-help and the Community Call for general community discussion.

You can also ask for help in a comment below!

Metadata

Metadata

Assignees

Labels

Blockchain / DLTIssues engineering distributed ledger functionalityintermediaterequires some knowledge of the codebase with some defined steps to implement or examples

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions