|
3 | 3 | uv run examples/tokens/custom_fractional_fee.py |
4 | 4 | python examples/tokens/custom_fractional_fee.py |
5 | 5 | """ |
| 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 |
6 | 12 | from hiero_sdk_python.tokens.custom_fractional_fee import CustomFractionalFee |
7 | 13 | 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 |
8 | 16 | 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) |
9 | 33 |
|
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( |
12 | 53 | numerator=1, |
13 | 54 | denominator=10, |
14 | 55 | min_amount=1, |
15 | 56 | 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, |
19 | 60 | ) |
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] |
21 | 66 |
|
| 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 |
22 | 89 |
|
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) |
25 | 98 |
|
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 |
27 | 104 |
|
| 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 | + |
28 | 116 | if __name__ == "__main__": |
29 | | - custom_fractional_fee() |
| 117 | + main() |
0 commit comments