Skip to content

Commit 2539fac

Browse files
authored
feat: TokenTypeClass (#58)
feat: TokenTypeClass Simpler enum approach Refactor of token create to handle more parameters Expanded tests for token create type Signed-off-by: exploreriii <[email protected]> fix: respecting new README separation Signed-off-by: exploreriii <[email protected]> * mixture of changes to organise Signed-off-by: exploreriii <[email protected]> * fix: Token Create fewer parameters mixture of changes to organise chore: squash merge TokenTypeClassTest into TokenTypeClass Signed-off-by: exploreriii <[email protected]> * fix: codacy errors Signed-off-by: exploreriii <[email protected]> --------- Signed-off-by: exploreriii <[email protected]>
1 parent 1b22263 commit 2539fac

File tree

8 files changed

+1095
-183
lines changed

8 files changed

+1095
-183
lines changed

examples/README.md

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,48 @@ balance = ( CryptoGetAccountBalanceQuery() .set_account_id(some_account_id) .exe
8282
#### Pythonic Syntax:
8383
```
8484
transaction = TokenCreateTransaction(
85-
token_name="ExampleToken",
86-
token_symbol="EXT",
87-
decimals=2,
88-
initial_supply=1000,
89-
treasury_account_id=operator_id,
90-
admin_key=admin_key
91-
supply_key=supply_key
85+
token_params=TokenParams(
86+
token_name="ExampleToken",
87+
token_symbol="EXT",
88+
decimals=2, # 0 for NON_FUNGIBLE_UNIQUE
89+
initial_supply=1000, # 0 for NON_FUNGIBLE_UNIQUE
90+
token_type=TokenType.FUNGIBLE_COMMON, # or TokenType.NON_FUNGIBLE_UNIQUE
91+
freeze_default=False,
92+
treasury_account_id=operator_id
93+
),
94+
keys=TokenKeys(
95+
admin_key=admin_key, # added but optional. Necessary for Token Delete or Update.
96+
supply_key=supply_key, # added but optional. Necessary for Token Mint or Burn.
97+
freeze_key=freeze_key # added but optional. Necessary to freeze and unfreeze a token.
98+
)
9299
).freeze_with(client)
93-
94-
transaction.sign(admin_key)
95-
transaction.sign(operator_key)
100+
transaction.sign(operator_key) # Required signing by the treasury account
101+
transaction.sign(admin_key) # Required since admin key exists
96102
transaction.execute(client)
97-
98103
```
104+
99105
#### Method Chaining:
100106
```
101107
transaction = (
102-
TokenCreateTransaction()
103-
.set_token_name("ExampleToken")
104-
.set_token_symbol("EXT")
105-
.set_decimals(2)
106-
.set_initial_supply(1000)
107-
.set_treasury_account_id(operator_id)
108-
.set_admin_key(admin_key) # Optional to create a token. Necessary for Token Delete or Update.
109-
.set_supply_key(supply_key) # Optional to change token supply. Necessary for Token Mint or Burn.
110-
.freeze_with(client)
111-
)
108+
TokenCreateTransaction() # no params => uses default placeholders which are next overwritten.
109+
.set_token_name("ExampleToken")
110+
.set_token_symbol("EXT")
111+
.set_decimals(2) # 0 for NON_FUNGIBLE_UNIQUE
112+
.set_initial_supply(1000) # 0 for NON_FUNGIBLE_UNIQUE
113+
.set_token_type(TokenType.FUNGIBLE_COMMON) # or TokenType.NON_FUNGIBLE_UNIQUE
114+
.set_freeze_default(False)
115+
.set_treasury_account_id(operator_id)
116+
.set_admin_key(admin_key) # added but optional. Necessary for Token Delete or Update.
117+
.set_supply_key(supply_key) # added but optional. Necessary for Token Mint or Burn.
118+
.set_freeze_key(freeze_key) # added but optional. Necessary to freeze and unfreeze a token.
119+
.freeze_with(client)
120+
)
112121
113-
transaction.sign(admin_key) # If admin key exists.
114-
transaction.sign(operator_key)
115-
transaction.execute(client)
122+
transaction.sign(operator_key) # Required signing by the treasury account
123+
transaction.sign(admin_key) # Required since admin key exists
124+
transaction.execute(client)
116125
```
117126

118-
119127
### Minting a Fungible Token
120128

121129
#### Pythonic Syntax:

examples/query_topic_message.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def on_error_handler(e):
2020
query = TopicMessageQuery(
2121
topic_id=os.getenv('TOPIC_ID'),
2222
start_time=datetime.now(timezone.utc),
23-
2423
limit=0,
2524
chunking_enabled=True
2625
)

examples/token_create.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,85 @@
1+
"""
2+
This is a simple example of how to create a token using setting methods.
3+
4+
It:
5+
1. Loads environment variables.
6+
2. Sets up a client and creates a token with the given parameters.
7+
3. Executes the token creation and prints the result.
8+
9+
Required environment variables:
10+
- OPERATOR_ID, OPERATOR_KEY (mandatory)
11+
- ADMIN_KEY, SUPPLY_KEY, FREEZE_KEY (optional)
12+
13+
Dependencies:
14+
- dotenv
15+
- hiero_sdk_python
16+
"""
17+
18+
# Adapt imports and paths as appropriate
119
import os
220
import sys
321
from dotenv import load_dotenv
4-
522
from hiero_sdk_python import (
623
Client,
724
AccountId,
825
PrivateKey,
926
TokenCreateTransaction,
1027
Network,
11-
)
28+
TokenType
29+
)
1230

31+
# Load environment variables from .env file
1332
load_dotenv()
1433

1534
def create_token():
35+
"""Function to create a fungible token on the Hedera network."""
36+
37+
# Network Setup
1638
network = Network(network='testnet')
1739
client = Client(network)
1840

41+
# Operator credentials (must be present)
1942
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
2043
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
21-
admin_key = PrivateKey.from_string(os.getenv('ADMIN_KEY'))
22-
supply_key = PrivateKey.from_string(os.getenv('ADMIN_KEY')) #Optional
2344

24-
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) #Optional
45+
# Optional Token Keys
46+
admin_key = PrivateKey.from_string(os.getenv('ADMIN_KEY'))# Optional
47+
supply_key = PrivateKey.from_string(os.getenv('SUPPLY_KEY')) # Optional
48+
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) # Optional
2549

50+
# Set the operator for the client
2651
client.set_operator(operator_id, operator_key)
2752

53+
# Create the token creation transaction
54+
# In this example, we set up a default empty token create transaction, then set the values
2855
transaction = (
2956
TokenCreateTransaction()
3057
.set_token_name("MyToken")
3158
.set_token_symbol("MTK")
32-
.set_decimals(2)
33-
.set_initial_supply(10)
34-
.set_treasury_account_id(operator_id)
35-
.set_admin_key(admin_key)
36-
.set_supply_key(supply_key)
37-
.set_freeze_key(freeze_key)
38-
.freeze_with(client)
39-
.sign(operator_key)
40-
.sign(admin_key)
59+
.set_decimals(2) # 0 for NON_FUNGIBLE_UNIQUE
60+
.set_initial_supply(10) # 0 for NON_FUNGIBLE_UNIQUE
61+
.set_treasury_account_id(operator_id) # Also known as treasury account
62+
.set_token_type(TokenType.FUNGIBLE_COMMON) # or TokenType.NON_FUNGIBLE_UNIQUE
63+
.set_admin_key(admin_key) # Optional
64+
.set_supply_key(supply_key) # Optional
65+
.set_freeze_key(freeze_key) # Optional
66+
.freeze_with(client) # Freeze the transaction. Returns self so we can sign.
67+
.sign(operator_key) # Required signature of treasury account
68+
.sign(admin_key) if admin_key else None # Only sign if admin_key is present. No need to sign with the supply or freeze keys to create the token.
4169

4270
)
4371

4472
try:
73+
74+
# Execute the transaction and get the receipt
4575
receipt = transaction.execute(client)
76+
4677
if receipt and receipt.tokenId:
4778
print(f"Token created with ID: {receipt.tokenId}")
4879
else:
4980
print("Token creation failed: Token ID not returned in receipt.")
5081
sys.exit(1)
82+
5183
except Exception as e:
5284
print(f"Token creation failed: {str(e)}")
5385
sys.exit(1)

0 commit comments

Comments
 (0)