1
+ from typing import Union
2
+
1
3
from hedera_sdk_python .transaction .transaction import Transaction
2
4
from hedera_sdk_python .hapi .services import crypto_create_pb2 , duration_pb2
3
5
from hedera_sdk_python .response_code import ResponseCode
@@ -10,15 +12,22 @@ class AccountCreateTransaction(Transaction):
10
12
Represents an account creation transaction on the Hedera network.
11
13
"""
12
14
13
- def __init__ (self , key = None , initial_balance = 0 , receiver_signature_required = False , auto_renew_period = 7890000 , memo = "" ):
15
+ def __init__ (
16
+ self ,
17
+ key : PublicKey = None ,
18
+ initial_balance : Union [Hbar , int ] = 0 ,
19
+ receiver_signature_required : bool = False ,
20
+ auto_renew_period : int = 7890000 , # 90 days in seconds
21
+ memo : str = ""
22
+ ):
14
23
"""
15
- Initializes a new AccountCreateTransaction instance with default values or keyword arguments.
24
+ Initializes a new AccountCreateTransaction instance with default values or specified keyword arguments.
16
25
17
26
Args:
18
27
key (PublicKey, optional): The public key for the new account.
19
- initial_balance (int or Hbar , optional): Initial balance in tinybars or as an Hbar instance .
28
+ initial_balance (Hbar or int , optional): Initial balance in Hbar or tinybars .
20
29
receiver_signature_required (bool, optional): Whether receiver signature is required.
21
- auto_renew_period (int, optional): Auto-renew period in seconds (default is 90 days).
30
+ auto_renew_period (int, optional): Auto-renew period in seconds (default is ~ 90 days).
22
31
memo (str, optional): Memo for the account.
23
32
"""
24
33
super ().__init__ ()
@@ -27,32 +36,78 @@ def __init__(self, key=None, initial_balance=0, receiver_signature_required=Fals
27
36
self .receiver_signature_required = receiver_signature_required
28
37
self .auto_renew_period = auto_renew_period
29
38
self .account_memo = memo
30
-
31
39
self ._default_transaction_fee = 300_000_000
32
40
33
- def set_initial_balance (self , balance ):
41
+ def set_key (self , key : PublicKey ):
42
+ """
43
+ Sets the public key for the new account.
44
+
45
+ Args:
46
+ key (PublicKey): The public key to assign to the account.
47
+
48
+ Returns:
49
+ AccountCreateTransaction: The current transaction instance for method chaining.
50
+ """
34
51
self ._require_not_frozen ()
35
- if not isinstance (balance , (Hbar , int )):
36
- raise TypeError ("initial_balance must be an instance of Hbar or int representing tinybars." )
37
- self .initial_balance = balance
52
+ self .key = key
38
53
return self
39
54
40
- def set_key (self , key : PublicKey ):
55
+ def set_initial_balance (self , balance : Union [Hbar , int ]):
56
+ """
57
+ Sets the initial balance for the new account.
58
+
59
+ Args:
60
+ balance (Hbar or int): The initial balance in Hbar or tinybars.
61
+
62
+ Returns:
63
+ AccountCreateTransaction: The current transaction instance for method chaining.
64
+ """
41
65
self ._require_not_frozen ()
42
- self .key = key
66
+ if not isinstance (balance , (Hbar , int )):
67
+ raise TypeError (
68
+ "initial_balance must be either an instance of Hbar or an integer (tinybars)."
69
+ )
70
+ self .initial_balance = balance
43
71
return self
44
72
45
73
def set_receiver_signature_required (self , required : bool ):
74
+ """
75
+ Sets whether a receiver signature is required.
76
+
77
+ Args:
78
+ required (bool): True if required, False otherwise.
79
+
80
+ Returns:
81
+ AccountCreateTransaction: The current transaction instance for method chaining.
82
+ """
46
83
self ._require_not_frozen ()
47
84
self .receiver_signature_required = required
48
85
return self
49
86
50
87
def set_auto_renew_period (self , seconds : int ):
88
+ """
89
+ Sets the auto-renew period in seconds.
90
+
91
+ Args:
92
+ seconds (int): The auto-renew period.
93
+
94
+ Returns:
95
+ AccountCreateTransaction: The current transaction instance for method chaining.
96
+ """
51
97
self ._require_not_frozen ()
52
98
self .auto_renew_period = seconds
53
99
return self
54
100
55
101
def set_account_memo (self , memo : str ):
102
+ """
103
+ Sets the memo for the new account.
104
+
105
+ Args:
106
+ memo (str): The memo to associate with the account.
107
+
108
+ Returns:
109
+ AccountCreateTransaction: The current transaction instance for method chaining.
110
+ """
56
111
self ._require_not_frozen ()
57
112
self .account_memo = memo
58
113
return self
@@ -66,18 +121,17 @@ def build_transaction_body(self):
66
121
67
122
Raises:
68
123
ValueError: If required fields are missing.
124
+ TypeError: If initial_balance is an invalid type.
69
125
"""
70
126
if not self .key :
71
- raise ValueError ("Key must be set." )
72
-
73
- if self .initial_balance is None :
74
- initial_balance_tinybars = 0
75
- elif isinstance (self .initial_balance , Hbar ):
127
+ raise ValueError ("Key must be set before building the transaction." )
128
+
129
+ if isinstance (self .initial_balance , Hbar ):
76
130
initial_balance_tinybars = self .initial_balance .to_tinybars ()
77
131
elif isinstance (self .initial_balance , int ):
78
132
initial_balance_tinybars = self .initial_balance
79
133
else :
80
- raise TypeError ("initial_balance must be an instance of Hbar or int representing tinybars." )
134
+ raise TypeError ("initial_balance must be Hbar or int ( tinybars) ." )
81
135
82
136
crypto_create_body = crypto_create_pb2 .CryptoCreateTransactionBody (
83
137
key = self .key .to_proto (),
@@ -111,7 +165,9 @@ def _execute_transaction(self, client, transaction_proto):
111
165
if response .nodeTransactionPrecheckCode != ResponseCode .OK :
112
166
error_code = response .nodeTransactionPrecheckCode
113
167
error_message = ResponseCode .get_name (error_code )
114
- raise Exception (f"Error during transaction submission: { error_code } ({ error_message } )" )
168
+ raise Exception (
169
+ f"Error during transaction submission: { error_code } ({ error_message } )"
170
+ )
115
171
116
172
receipt = self .get_receipt (client )
117
173
return receipt
@@ -121,7 +177,7 @@ def get_receipt(self, client, timeout=60):
121
177
Retrieves the receipt for the transaction.
122
178
123
179
Args:
124
- client (Client): The client instance.
180
+ client (Client): The client instance to query .
125
181
timeout (int): Maximum time in seconds to wait for the receipt.
126
182
127
183
Returns:
@@ -131,7 +187,5 @@ def get_receipt(self, client, timeout=60):
131
187
Exception: If the transaction ID is not set or if receipt retrieval fails.
132
188
"""
133
189
if self .transaction_id is None :
134
- raise Exception ("Transaction ID is not set." )
135
-
136
- receipt = client .get_transaction_receipt (self .transaction_id , timeout )
137
- return receipt
190
+ raise Exception ("Transaction ID is not set. Did you forget to sign or execute the transaction?" )
191
+ return client .get_transaction_receipt (self .transaction_id , timeout )
0 commit comments