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.

Alternatives

No response

Metadata

Metadata

Assignees

Labels

Good First Issue CandidateIssues that can become a “good first issue” but need more description/context.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions