hiero-sdk-python 0.0.2.dev3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hedera_sdk_python/__init__.py +93 -0
- hedera_sdk_python/account/__init__.py +0 -0
- hedera_sdk_python/account/account_balance.py +44 -0
- hedera_sdk_python/account/account_create_transaction.py +173 -0
- hedera_sdk_python/account/account_id.py +62 -0
- hedera_sdk_python/client/__init__.py +0 -0
- hedera_sdk_python/client/client.py +139 -0
- hedera_sdk_python/client/network.py +143 -0
- hedera_sdk_python/consensus/__init__.py +0 -0
- hedera_sdk_python/consensus/topic_create_transaction.py +83 -0
- hedera_sdk_python/consensus/topic_delete_transaction.py +67 -0
- hedera_sdk_python/consensus/topic_id.py +44 -0
- hedera_sdk_python/consensus/topic_info.py +91 -0
- hedera_sdk_python/consensus/topic_message.py +61 -0
- hedera_sdk_python/consensus/topic_message_submit_transaction.py +87 -0
- hedera_sdk_python/consensus/topic_update_transaction.py +173 -0
- hedera_sdk_python/crypto/__init__.py +0 -0
- hedera_sdk_python/crypto/private_key.py +196 -0
- hedera_sdk_python/crypto/public_key.py +162 -0
- hedera_sdk_python/hbar.py +33 -0
- hedera_sdk_python/query/__init__.py +0 -0
- hedera_sdk_python/query/account_balance_query.py +100 -0
- hedera_sdk_python/query/query.py +138 -0
- hedera_sdk_python/query/topic_info_query.py +105 -0
- hedera_sdk_python/query/topic_message_query.py +105 -0
- hedera_sdk_python/query/transaction_get_receipt_query.py +128 -0
- hedera_sdk_python/response_code.py +587 -0
- hedera_sdk_python/timestamp.py +166 -0
- hedera_sdk_python/tokens/__init__.py +0 -0
- hedera_sdk_python/tokens/token_associate_transaction.py +85 -0
- hedera_sdk_python/tokens/token_create_transaction.py +144 -0
- hedera_sdk_python/tokens/token_delete_transaction.py +84 -0
- hedera_sdk_python/tokens/token_dissociate_transaction.py +81 -0
- hedera_sdk_python/tokens/token_id.py +44 -0
- hedera_sdk_python/tokens/token_mint_transaction.py +143 -0
- hedera_sdk_python/transaction/__init__.py +0 -0
- hedera_sdk_python/transaction/query_payment.py +32 -0
- hedera_sdk_python/transaction/transaction.py +267 -0
- hedera_sdk_python/transaction/transaction_id.py +140 -0
- hedera_sdk_python/transaction/transaction_receipt.py +78 -0
- hedera_sdk_python/transaction/transfer_transaction.py +119 -0
- hedera_sdk_python/utils/key_format.py +22 -0
- hiero_sdk_python-0.0.2.dev3.dist-info/METADATA +822 -0
- hiero_sdk_python-0.0.2.dev3.dist-info/RECORD +47 -0
- hiero_sdk_python-0.0.2.dev3.dist-info/WHEEL +4 -0
- hiero_sdk_python-0.0.2.dev3.dist-info/entry_points.txt +4 -0
- hiero_sdk_python-0.0.2.dev3.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from collections import defaultdict
|
|
2
|
+
from hiero-sdk-python.transaction.transaction import Transaction
|
|
3
|
+
from hiero-sdk-python.hapi.services import crypto_transfer_pb2, basic_types_pb2
|
|
4
|
+
from hiero-sdk-python.account.account_id import AccountId
|
|
5
|
+
from hiero-sdk-python.tokens.token_id import TokenId
|
|
6
|
+
from hiero-sdk-python.response_code import ResponseCode
|
|
7
|
+
|
|
8
|
+
class TransferTransaction(Transaction):
|
|
9
|
+
"""
|
|
10
|
+
Represents a transaction to transfer HBAR or tokens between accounts.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def __init__(self, hbar_transfers=None, token_transfers=None):
|
|
14
|
+
"""
|
|
15
|
+
Initializes a new TransferTransaction instance.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
hbar_transfers (dict[AccountId, int], optional): Initial HBAR transfers.
|
|
19
|
+
token_transfers (dict[TokenId, dict[AccountId, int]], optional): Initial token transfers.
|
|
20
|
+
"""
|
|
21
|
+
super().__init__()
|
|
22
|
+
self.hbar_transfers = defaultdict(int)
|
|
23
|
+
self.token_transfers = defaultdict(lambda: defaultdict(int))
|
|
24
|
+
self._default_transaction_fee = 100_000_000
|
|
25
|
+
|
|
26
|
+
if hbar_transfers:
|
|
27
|
+
for account_id, amount in hbar_transfers.items():
|
|
28
|
+
self.add_hbar_transfer(account_id, amount)
|
|
29
|
+
|
|
30
|
+
if token_transfers:
|
|
31
|
+
for token_id, account_transfers in token_transfers.items():
|
|
32
|
+
for account_id, amount in account_transfers.items():
|
|
33
|
+
self.add_token_transfer(token_id, account_id, amount)
|
|
34
|
+
|
|
35
|
+
def add_hbar_transfer(self, account_id: AccountId, amount: int) -> "TransferTransaction":
|
|
36
|
+
"""
|
|
37
|
+
Adds a HBAR transfer to the transaction.
|
|
38
|
+
"""
|
|
39
|
+
self._require_not_frozen()
|
|
40
|
+
if not isinstance(account_id, AccountId):
|
|
41
|
+
raise TypeError("account_id must be an AccountId instance.")
|
|
42
|
+
if not isinstance(amount, int) or amount == 0:
|
|
43
|
+
raise ValueError("Amount must be a non-zero integer.")
|
|
44
|
+
|
|
45
|
+
self.hbar_transfers[account_id] += amount
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
def add_token_transfer(self, token_id: TokenId, account_id: AccountId, amount: int) -> "TransferTransaction":
|
|
49
|
+
"""
|
|
50
|
+
Adds a token transfer to the transaction.
|
|
51
|
+
"""
|
|
52
|
+
self._require_not_frozen()
|
|
53
|
+
if not isinstance(token_id, TokenId):
|
|
54
|
+
raise TypeError("token_id must be a TokenId instance.")
|
|
55
|
+
if not isinstance(account_id, AccountId):
|
|
56
|
+
raise TypeError("account_id must be an AccountId instance.")
|
|
57
|
+
if not isinstance(amount, int) or amount == 0:
|
|
58
|
+
raise ValueError("Amount must be a non-zero integer.")
|
|
59
|
+
|
|
60
|
+
self.token_transfers[token_id][account_id] += amount
|
|
61
|
+
return self
|
|
62
|
+
|
|
63
|
+
def build_transaction_body(self):
|
|
64
|
+
"""
|
|
65
|
+
Builds and returns the protobuf transaction body for a transfer transaction.
|
|
66
|
+
"""
|
|
67
|
+
crypto_transfer_tx_body = crypto_transfer_pb2.CryptoTransferTransactionBody()
|
|
68
|
+
|
|
69
|
+
# HBAR
|
|
70
|
+
if self.hbar_transfers:
|
|
71
|
+
transfer_list = basic_types_pb2.TransferList()
|
|
72
|
+
for account_id, amount in self.hbar_transfers.items():
|
|
73
|
+
transfer_list.accountAmounts.append(
|
|
74
|
+
basic_types_pb2.AccountAmount(
|
|
75
|
+
accountID=account_id.to_proto(),
|
|
76
|
+
amount=amount,
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
crypto_transfer_tx_body.transfers.CopyFrom(transfer_list)
|
|
80
|
+
|
|
81
|
+
# Tokens
|
|
82
|
+
for token_id, transfers in self.token_transfers.items():
|
|
83
|
+
token_transfer_list = basic_types_pb2.TokenTransferList(
|
|
84
|
+
token=token_id.to_proto()
|
|
85
|
+
)
|
|
86
|
+
for account_id, amount in transfers.items():
|
|
87
|
+
token_transfer_list.transfers.append(
|
|
88
|
+
basic_types_pb2.AccountAmount(
|
|
89
|
+
accountID=account_id.to_proto(),
|
|
90
|
+
amount=amount,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
crypto_transfer_tx_body.tokenTransfers.append(token_transfer_list)
|
|
94
|
+
|
|
95
|
+
transaction_body = self.build_base_transaction_body()
|
|
96
|
+
transaction_body.cryptoTransfer.CopyFrom(crypto_transfer_tx_body)
|
|
97
|
+
|
|
98
|
+
return transaction_body
|
|
99
|
+
|
|
100
|
+
def _execute_transaction(self, client, transaction_proto):
|
|
101
|
+
"""
|
|
102
|
+
Executes the transfer transaction using the provided client.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
client (Client): The client instance.
|
|
106
|
+
transaction_proto (Transaction): The transaction protobuf message.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
TransactionReceipt: The receipt from the network.
|
|
110
|
+
"""
|
|
111
|
+
response = client.crypto_stub.cryptoTransfer(transaction_proto)
|
|
112
|
+
|
|
113
|
+
if response.nodeTransactionPrecheckCode != ResponseCode.OK:
|
|
114
|
+
error_code = response.nodeTransactionPrecheckCode
|
|
115
|
+
error_message = ResponseCode.get_name(error_code)
|
|
116
|
+
raise Exception(f"Error during transaction submission: {error_code} ({error_message})")
|
|
117
|
+
|
|
118
|
+
receipt = self.get_receipt(client)
|
|
119
|
+
return receipt
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from hiero-sdk-python.hapi.services.basic_types_pb2 import Key
|
|
2
|
+
|
|
3
|
+
def format_key(key: Key) -> str:
|
|
4
|
+
"""
|
|
5
|
+
Converts a protobuf Key into a nicely formatted string:
|
|
6
|
+
- If key is None, return "None"
|
|
7
|
+
- If ed25519, show "ed25519(hex-encoded)"
|
|
8
|
+
- If thresholdKey, keyList, or something else, show a short label.
|
|
9
|
+
"""
|
|
10
|
+
if key is None:
|
|
11
|
+
return "None"
|
|
12
|
+
|
|
13
|
+
if key.HasField("ed25519"):
|
|
14
|
+
return f"ed25519({key.ed25519.hex()})"
|
|
15
|
+
elif key.HasField("thresholdKey"):
|
|
16
|
+
return "thresholdKey(...)"
|
|
17
|
+
elif key.HasField("keyList"):
|
|
18
|
+
return "keyList(...)"
|
|
19
|
+
elif key.HasField("contractID"):
|
|
20
|
+
return f"contractID({key.contractID})"
|
|
21
|
+
|
|
22
|
+
return str(key).replace("\n", " ")
|