Skip to content

Commit d48f2b3

Browse files
authored
Merge branch 'hiero-ledger:main' into merge-conflict-bot-main-trigger
2 parents 25fbd96 + f799f9a commit d48f2b3

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
1212
- Add examples/tokens/token_create_transaction_pause_key.py example demonstrating token pause/unpause behavior and pause key usage (#833)
1313
- Added `docs/sdk_developers/training/transaction_lifecycle.md` to explain the typical lifecycle of executing a transaction using the Hedera Python SDK.
1414
- Add inactivity bot workflow to unassign stale issue assignees (#952)
15+
- Made custom fraction fee end to end
1516

1617
### Changed
1718
- Allow `PublicKey` for `TokenUpdateKeys` in `TokenUpdateTransaction`, enabling non-custodial workflows where operators can build transactions using only public keys (#934).

examples/tokens/custom_fractional_fee.py

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,115 @@
33
uv run examples/tokens/custom_fractional_fee.py
44
python examples/tokens/custom_fractional_fee.py
55
"""
6+
7+
import os
8+
import sys
9+
from dotenv import load_dotenv
10+
11+
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction, TokenParams
612
from hiero_sdk_python.tokens.custom_fractional_fee import CustomFractionalFee
713
from hiero_sdk_python.tokens.fee_assessment_method import FeeAssessmentMethod
14+
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
15+
from hiero_sdk_python.crypto.private_key import PrivateKey
816
from hiero_sdk_python.account.account_id import AccountId
17+
from hiero_sdk_python.response_code import ResponseCode
18+
from hiero_sdk_python.client.network import Network
19+
from hiero_sdk_python.client.client import Client
20+
from hiero_sdk_python.tokens.token_type import TokenType
21+
from hiero_sdk_python.tokens.supply_type import SupplyType
22+
23+
24+
load_dotenv()
25+
26+
def setup_client():
27+
network_name = os.getenv("NETWORK", "testnet")
28+
29+
# Validate environment variables
30+
if not os.getenv("OPERATOR_ID") or not os.getenv("OPERATOR_KEY"):
31+
print("❌ Missing OPERATOR_ID or OPERATOR_KEY in .env file.")
32+
sys.exit(1)
933

10-
def custom_fractional_fee() :
11-
fractional_fee = CustomFractionalFee(
34+
try:
35+
network = Network(network_name)
36+
print(f"Connecting to Hedera {network_name} network!")
37+
client = Client(network)
38+
39+
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ''))
40+
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ''))
41+
client.set_operator(operator_id, operator_key)
42+
print(f"Client set up with operator id {client.operator_account_id}")
43+
44+
except Exception as e:
45+
raise ConnectionError(f'Error initializing client: {e}') from e
46+
47+
print(f"✅ Connected to Hedera {network_name} network as operator: {operator_id}")
48+
return client, operator_id, operator_key
49+
50+
def build_fractional_fee(operator_account: AccountId) -> CustomFractionalFee:
51+
"""Creates a CustomFractionalFee instance."""
52+
return CustomFractionalFee(
1253
numerator=1,
1354
denominator=10,
1455
min_amount=1,
1556
max_amount=100,
16-
assessment_method=FeeAssessmentMethod.EXCLUSIVE,
17-
fee_collector_account_id=AccountId(0, 0, 456),
18-
all_collectors_are_exempt=False,
57+
assessment_method=FeeAssessmentMethod.INCLUSIVE,
58+
fee_collector_account_id=operator_account,
59+
all_collectors_are_exempt=True,
1960
)
20-
print(fractional_fee.__str__())
61+
62+
def create_token_with_fee_key(client, operator_id, fractional_fee: CustomFractionalFee):
63+
"""Create a fungible token with a fee_schedule_key."""
64+
print("Creating fungible token with fee_schedule_key...")
65+
fractional_fee = [fractional_fee]
2166

67+
token_params = TokenParams(
68+
token_name="Fee Key Token",
69+
token_symbol="FKT",
70+
treasury_account_id=operator_id,
71+
initial_supply=1000,
72+
decimals=2,
73+
token_type=TokenType.FUNGIBLE_COMMON,
74+
supply_type=SupplyType.INFINITE,
75+
custom_fees=fractional_fee,
76+
)
77+
78+
tx = TokenCreateTransaction(token_params=token_params)
79+
tx.freeze_with(client)
80+
receipt = tx.execute(client)
81+
82+
if receipt.status != ResponseCode.SUCCESS:
83+
print(f"Token creation failed: {ResponseCode(receipt.status).name}")
84+
sys.exit(1)
85+
86+
token_id = receipt.token_id
87+
print(f"Token created with ID: {token_id}")
88+
return token_id
2289

23-
# Convert to protobuf
24-
fractional_fee_proto = fractional_fee._to_proto()
90+
def print_fractional_fees(token_info, fractional_fee):
91+
"""Print all CustomFractionalFee objects from a TokenInfo."""
92+
if not token_info.custom_fees:
93+
print("No custom fees found.")
94+
return
95+
else:
96+
print("\n--- Custom Fractional Fee ---")
97+
print(fractional_fee)
2598

26-
print("Fractional Fee Protobuf:", fractional_fee_proto)
99+
def query_and_validate_fractional_fee(client: Client, token_id):
100+
"""Fetch token info from Hedera and print the custom fractional fees."""
101+
print("\nQuerying token info to validate fractional fee...")
102+
token_info = TokenInfoQuery(token_id=token_id).execute(client)
103+
return token_info
27104

105+
def main():
106+
client, operator_id, _ = setup_client()
107+
# Build fractional fee
108+
fractional_fee = build_fractional_fee(operator_id)
109+
token_id = create_token_with_fee_key(client, operator_id, fractional_fee)
110+
111+
# Query and validate fractional fee
112+
token_info = query_and_validate_fractional_fee(client, token_id)
113+
print_fractional_fees(token_info, fractional_fee)
114+
print("✅ Example completed successfully.")
115+
28116
if __name__ == "__main__":
29-
custom_fractional_fee()
117+
main()

0 commit comments

Comments
 (0)