-
Notifications
You must be signed in to change notification settings - Fork 108
feat: support hbar transfer #1014
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
base: main
Are you sure you want to change the base?
Changes from all commits
1fb81ce
70c5b1f
5fe2fde
0b1e7ac
22bd4fa
0f4d7ff
5affb9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,7 +149,9 @@ def _add_token_transfer( | |
| if not isinstance(account_id, AccountId): | ||
| raise TypeError("account_id must be an AccountId instance.") | ||
| if not isinstance(amount, int) or amount == 0: | ||
| raise ValueError("Amount must be a non-zero integer.") | ||
| raise ValueError("Amount must be a integer.") | ||
|
Comment on lines
151
to
+152
Member
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. I'd keep the previous version, is a bit cleaner and also here you have two if with quite te same condition:
|
||
| if amount == 0: | ||
| raise ValueError("amount must be a non-zero value.") | ||
| if expected_decimals is not None and not isinstance(expected_decimals, int): | ||
| raise TypeError("expected_decimals must be an integer.") | ||
| if not isinstance(is_approved, bool): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,12 @@ | |
| Defines TransferTransaction for transferring HBAR or tokens between accounts. | ||
| """ | ||
|
|
||
| from typing import Dict, List, Optional, Tuple | ||
| from typing import Dict, List, Optional, Tuple, Union | ||
|
|
||
| from hiero_sdk_python.account.account_id import AccountId | ||
| from hiero_sdk_python.channels import _Channel | ||
| from hiero_sdk_python.executable import _Method | ||
| from hiero_sdk_python.hbar import Hbar | ||
| from hiero_sdk_python.hapi.services import basic_types_pb2, crypto_transfer_pb2, transaction_pb2 | ||
| from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import ( | ||
| SchedulableTransactionBody, | ||
|
|
@@ -29,7 +30,8 @@ def __init__( | |
| self, | ||
| hbar_transfers: Optional[Dict[AccountId, int]] = None, | ||
| token_transfers: Optional[Dict[TokenId, Dict[AccountId, int]]] = None, | ||
| nft_transfers: Optional[Dict[TokenId, List[Tuple[AccountId, AccountId, int, bool]]]] = None, | ||
| nft_transfers: Optional[Dict[TokenId, | ||
| List[Tuple[AccountId, AccountId, int, bool]]]] = None, | ||
| ) -> None: | ||
| """ | ||
| Initializes a new TransferTransaction instance. | ||
|
|
@@ -59,24 +61,35 @@ def _init_hbar_transfers(self, hbar_transfers: Dict[AccountId, int]) -> None: | |
| self.add_hbar_transfer(account_id, amount) | ||
|
|
||
| def _add_hbar_transfer( | ||
| self, account_id: AccountId, amount: int, is_approved: bool = False | ||
| self, account_id: AccountId, amount: Union[int, Hbar], is_approved: bool = False | ||
| ) -> "TransferTransaction": | ||
| """ | ||
| Internal method to add a HBAR transfer to the transaction. | ||
|
|
||
| Args: | ||
| account_id (AccountId): The account ID of the sender or receiver. | ||
| amount (int): The amount of the HBAR to transfer. | ||
| amount (Union[int, Hbar]): The amount of the HBAR to transfer (in tinybars if int, or Hbar object). | ||
| is_approved (bool, optional): Whether the transfer is approved. Defaults to False. | ||
|
|
||
| Returns: | ||
| TransferTransaction: The current instance of the transaction for chaining. | ||
| """ | ||
| self._require_not_frozen() | ||
|
|
||
| if not isinstance(account_id, AccountId): | ||
| raise TypeError("account_id must be an AccountId instance.") | ||
| if not isinstance(amount, int) or amount == 0: | ||
| raise ValueError("Amount must be a non-zero integer.") | ||
|
|
||
| if amount is None: | ||
| raise TypeError("amount cannot be None.") | ||
|
|
||
| if isinstance(amount, Hbar): | ||
| amount = amount.to_tinybars() | ||
| elif not isinstance(amount, int): | ||
| raise TypeError("amount must be an integer or Hbar object.") | ||
|
|
||
| if amount == 0: | ||
| raise ValueError("Amount must be a non-zero value.") | ||
|
|
||
| if not isinstance(is_approved, bool): | ||
| raise TypeError("is_approved must be a boolean.") | ||
|
|
||
|
|
@@ -85,16 +98,17 @@ def _add_hbar_transfer( | |
| transfer.amount += amount | ||
|
Member
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. what could happen if the |
||
| return self | ||
|
|
||
| self.hbar_transfers.append(HbarTransfer(account_id, amount, is_approved)) | ||
| self.hbar_transfers.append( | ||
| HbarTransfer(account_id, amount, is_approved)) | ||
| return self | ||
|
|
||
| def add_hbar_transfer(self, account_id: AccountId, amount: int) -> "TransferTransaction": | ||
| def add_hbar_transfer(self, account_id: AccountId, amount: Union[int, Hbar]) -> "TransferTransaction": | ||
| """ | ||
| Adds a HBAR transfer to the transaction. | ||
|
|
||
| Args: | ||
| account_id (AccountId): The account ID of the sender or receiver. | ||
| amount (int): The amount of the HBAR to transfer. | ||
| amount (Union[int, Hbar]): The amount of the HBAR to transfer (in tinybars if int, or Hbar object). | ||
|
|
||
| Returns: | ||
| TransferTransaction: The current instance of the transaction for chaining. | ||
|
|
@@ -103,14 +117,14 @@ def add_hbar_transfer(self, account_id: AccountId, amount: int) -> "TransferTran | |
| return self | ||
|
|
||
| def add_approved_hbar_transfer( | ||
| self, account_id: AccountId, amount: int | ||
| self, account_id: AccountId, amount: Union[int, Hbar] | ||
| ) -> "TransferTransaction": | ||
| """ | ||
| Adds a HBAR transfer with approval to the transaction. | ||
|
|
||
| Args: | ||
| account_id (AccountId): The account ID of the sender or receiver. | ||
| amount (int): The amount of the HBAR to transfer. | ||
| amount (Union[int, Hbar]): The amount of the HBAR to transfer (in tinybars if int, or Hbar object). | ||
|
|
||
| Returns: | ||
| TransferTransaction: The current instance of the transaction for chaining. | ||
|
|
@@ -190,7 +204,8 @@ def _from_protobuf(cls, transaction_body, body_bytes: bytes, sig_map): | |
|
|
||
| if crypto_transfer.HasField("transfers"): | ||
| for account_amount in crypto_transfer.transfers.accountAmounts: | ||
| account_id = AccountId._from_proto(account_amount.accountID) | ||
| account_id = AccountId._from_proto( | ||
| account_amount.accountID) | ||
| amount = account_amount.amount | ||
| is_approved = account_amount.is_approval | ||
| transaction.hbar_transfers.append( | ||
|
|
@@ -210,17 +225,21 @@ def _from_protobuf(cls, transaction_body, body_bytes: bytes, sig_map): | |
| expected_decimals = token_transfer_list.expected_decimals.value | ||
|
|
||
| transaction.token_transfers[token_id].append( | ||
| TokenTransfer(token_id, account_id, amount, expected_decimals, is_approved) | ||
| TokenTransfer(token_id, account_id, amount, | ||
| expected_decimals, is_approved) | ||
| ) | ||
|
|
||
| for nft_transfer in token_transfer_list.nftTransfers: | ||
| sender_id = AccountId._from_proto(nft_transfer.senderAccountID) | ||
| receiver_id = AccountId._from_proto(nft_transfer.receiverAccountID) | ||
| sender_id = AccountId._from_proto( | ||
| nft_transfer.senderAccountID) | ||
| receiver_id = AccountId._from_proto( | ||
| nft_transfer.receiverAccountID) | ||
| serial_number = nft_transfer.serialNumber | ||
| is_approved = nft_transfer.is_approval | ||
|
|
||
| transaction.nft_transfers[token_id].append( | ||
| TokenNftTransfer(token_id, sender_id, receiver_id, serial_number, is_approved) | ||
| TokenNftTransfer( | ||
| token_id, sender_id, receiver_id, serial_number, is_approved) | ||
| ) | ||
|
|
||
| return transaction | ||
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.
This must be move at line 53