Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
- Rename test files across the repository to ensure they consistently end with _test.py (#1055)
- Cleaned up `token_airdrop_claim_signature_required` example for pylint compliance (no functional changes). (#1080)
- Rename the file 'test_token_fee_schedule_update_transaction_e2e.py' to make it ends with _test.py as all other test files.(#1117)
- Format token examples with Black for consistent code style and improved readability (#1119)


### Fixed
Expand Down
46 changes: 35 additions & 11 deletions examples/tokens/account_allowance_approve_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.tokens.supply_type import SupplyType
from hiero_sdk_python.tokens.token_associate_transaction import TokenAssociateTransaction
from hiero_sdk_python.tokens.token_associate_transaction import (
TokenAssociateTransaction,
)
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction
from hiero_sdk_python.tokens.token_type import TokenType
from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction

load_dotenv()

network_name = os.getenv('NETWORK', 'testnet').lower()
network_name = os.getenv("NETWORK", "testnet").lower()


def setup_client():
"""Initialize and set up the client with operator account"""
Expand Down Expand Up @@ -54,7 +57,9 @@ def create_account(client):
)

if account_receipt.status != ResponseCode.SUCCESS:
print(f"Account creation failed with status: {ResponseCode(account_receipt.status).name}")
print(
f"Account creation failed with status: {ResponseCode(account_receipt.status).name}"
)
sys.exit(1)

account_account_id = account_receipt.account_id
Expand Down Expand Up @@ -99,13 +104,17 @@ def associate_token_with_account(client, account_id, account_private_key, token_
)

if receipt.status != ResponseCode.SUCCESS:
print(f"Token association failed with status: {ResponseCode(receipt.status).name}")
print(
f"Token association failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)

print(f"Token {token_id} associated with account {account_id}")


def approve_token_allowance(client, token_id, owner_account_id, spender_account_id, amount):
def approve_token_allowance(
client, token_id, owner_account_id, spender_account_id, amount
):
"""Approve token allowance for spender"""
receipt = (
AccountAllowanceApproveTransaction()
Expand All @@ -114,7 +123,9 @@ def approve_token_allowance(client, token_id, owner_account_id, spender_account_
)

if receipt.status != ResponseCode.SUCCESS:
print(f"Token allowance approval failed with status: {ResponseCode(receipt.status).name}")
print(
f"Token allowance approval failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)

print(f"Token allowance of {amount} approved for spender {spender_account_id}")
Expand All @@ -130,20 +141,29 @@ def delete_token_allowance(client, token_id, owner_account_id, spender_account_i
)

if receipt.status != ResponseCode.SUCCESS:
print(f"Token allowance deletion failed with status: {ResponseCode(receipt.status).name}")
print(
f"Token allowance deletion failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)

print(f"Token allowance deleted for spender {spender_account_id}")
return receipt


def transfer_token_without_allowance(
client, spender_account_id, spender_private_key, amount, receiver_account_id, token_id
client,
spender_account_id,
spender_private_key,
amount,
receiver_account_id,
token_id,
):
"""Transfer tokens without allowance"""
print("Trying to transfer tokens without allowance...")
owner_account_id = client.operator_account_id
client.set_operator(spender_account_id, spender_private_key) # Set operator to spender
client.set_operator(
spender_account_id, spender_private_key
) # Set operator to spender

receipt = (
TransferTransaction()
Expand All @@ -158,7 +178,9 @@ def transfer_token_without_allowance(
f"status but got: {ResponseCode(receipt.status).name}"
)

print(f"Token transfer successfully failed with {ResponseCode(receipt.status).name} status")
print(
f"Token transfer successfully failed with {ResponseCode(receipt.status).name} status"
)


def token_allowance():
Expand Down Expand Up @@ -198,7 +220,9 @@ def token_allowance():
receipt = (
TransferTransaction()
.set_transaction_id(TransactionId.generate(spender_id))
.add_approved_token_transfer(token_id, client.operator_account_id, -allowance_amount)
.add_approved_token_transfer(
token_id, client.operator_account_id, -allowance_amount
)
.add_approved_token_transfer(token_id, receiver_id, allowance_amount)
.freeze_with(client)
.sign(spender_private_key)
Expand Down
12 changes: 7 additions & 5 deletions examples/tokens/custom_fee_fixed.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Run with:
Run with:
uv run examples/tokens/custom_fixed_fee.py
python examples/tokens/custom_fixed_fee.py
"""

from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.tokens.token_id import TokenId


def custom_fixed_fee():
fixed_fee = CustomFixedFee(
amount=100,
Expand All @@ -19,9 +21,9 @@ def custom_fixed_fee():

# Convert to protobuf
fixed_fee_proto = fixed_fee._to_proto()

print("Fixed Fee Protobuf:", fixed_fee_proto)


if __name__ == "__main__":
custom_fixed_fee()
custom_fixed_fee()
32 changes: 21 additions & 11 deletions examples/tokens/custom_fee_fractional.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Run with:
Run with:
uv run examples/tokens/custom_fractional_fee.py
python examples/tokens/custom_fractional_fee.py
"""
Expand All @@ -8,7 +8,10 @@
import sys
from dotenv import load_dotenv

from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction, TokenParams
from hiero_sdk_python.tokens.token_create_transaction import (
TokenCreateTransaction,
TokenParams,
)
from hiero_sdk_python.tokens.custom_fractional_fee import CustomFractionalFee
from hiero_sdk_python.tokens.fee_assessment_method import FeeAssessmentMethod
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
Expand All @@ -23,6 +26,7 @@

load_dotenv()


def setup_client():
network_name = os.getenv("NETWORK", "testnet")

Expand All @@ -36,17 +40,18 @@ def setup_client():
print(f"Connecting to Hedera {network_name} network!")
client = Client(network)

operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ''))
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ''))
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
client.set_operator(operator_id, operator_key)
print(f"Client set up with operator id {client.operator_account_id}")

except Exception as e:
raise ConnectionError(f'Error initializing client: {e}') from e
raise ConnectionError(f"Error initializing client: {e}") from e

print(f"✅ Connected to Hedera {network_name} network as operator: {operator_id}")
return client, operator_id, operator_key


def build_fractional_fee(operator_account: AccountId) -> CustomFractionalFee:
"""Creates a CustomFractionalFee instance."""
return CustomFractionalFee(
Expand All @@ -58,7 +63,8 @@ def build_fractional_fee(operator_account: AccountId) -> CustomFractionalFee:
fee_collector_account_id=operator_account,
all_collectors_are_exempt=True,
)



def create_token_with_fee_key(client, operator_id, fractional_fee: CustomFractionalFee):
"""Create a fungible token with a fee_schedule_key."""
print("Creating fungible token with fee_schedule_key...")
Expand All @@ -74,19 +80,20 @@ def create_token_with_fee_key(client, operator_id, fractional_fee: CustomFractio
supply_type=SupplyType.INFINITE,
custom_fees=fractional_fee,
)

tx = TokenCreateTransaction(token_params=token_params)
tx.freeze_with(client)
receipt = tx.execute(client)

if receipt.status != ResponseCode.SUCCESS:
print(f"Token creation failed: {ResponseCode(receipt.status).name}")
sys.exit(1)

token_id = receipt.token_id
print(f"Token created with ID: {token_id}")
return token_id


def print_fractional_fees(token_info, fractional_fee):
"""Print all CustomFractionalFee objects from a TokenInfo."""
if not token_info.custom_fees:
Expand All @@ -96,22 +103,25 @@ def print_fractional_fees(token_info, fractional_fee):
print("\n--- Custom Fractional Fee ---")
print(fractional_fee)


def query_and_validate_fractional_fee(client: Client, token_id):
"""Fetch token info from Hedera and print the custom fractional fees."""
print("\nQuerying token info to validate fractional fee...")
token_info = TokenInfoQuery(token_id=token_id).execute(client)
return token_info


def main():
client, operator_id, _ = setup_client()
# Build fractional fee
fractional_fee = build_fractional_fee(operator_id)
token_id = create_token_with_fee_key(client, operator_id, fractional_fee)

# Query and validate fractional fee
token_info = query_and_validate_fractional_fee(client, token_id)
print_fractional_fees(token_info, fractional_fee)
print("✅ Example completed successfully.")



if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions examples/tokens/custom_fee_royalty.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""
Run with:
Run with:
uv run examples/tokens/custom_royalty_fee.py
python examples/tokens/custom_royalty_fee.py
"""

from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
from hiero_sdk_python.tokens.custom_royalty_fee import CustomRoyaltyFee
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.tokens.token_id import TokenId


def custom_royalty_fee():
fallback_fee = CustomFixedFee(
amount=50,
Expand All @@ -27,5 +29,6 @@ def custom_royalty_fee():

print("Royalty Fee Protobuf:", royalty_fee_proto)


if __name__ == "__main__":
custom_royalty_fee()
custom_royalty_fee()
Loading