Skip to content
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
- bumped actions/checkout from 5.0.0 to 6.0.0
- Limit workflow bot to one message per PR
- Refactored token-related example scripts (`token_delete.py`, `token_dissociate.py`, etc.) for improved readability and modularity. [#370]
- upgrade: step security action upgraded from harden-runner-2.13.1 to harden-runner-2.13.1
- upgrade: step security action upgraded from harden-runner-2.13.1 to harden-runner.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you make sure line 38 includes this:

  • upgrade: step security action upgraded from harden-runner-2.13.1 to harden-runner-2.13.1

- chore: Split `examples/account_allowance_nft.py` into separate `account_allowance_approve_transaction_nft.py` and `account_allowance_delete_transaction_nft.py` examples.
- chore: bump protobuf from 6.33.0 to 6.33.1 (#796)
- fix: Allow `max_automatic_token_associations` to be set to -1 (unlimited) in `AccountCreateTransaction` and add field to `AccountInfo`.
- Allow `PrivateKey` to be used for keys in `TopicCreateTransaction` for consistency.
- Update github actions checkout from 5.0.0 to 5.0.1 (#814)
- changed to add concurrency to workflow bot

### Fixed

- chore: fix test.yml workflow to log import errors (#740)
Expand Down
55 changes: 26 additions & 29 deletions examples/transfer_token.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
uv run examples/transfer_token.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Raja-89 , this file is now renamed to examples/transaction/transfer_transaction_fungible.py.

cc- @exploreriii

python examples/transfer_token.py

"""
import os
import sys
Expand Down Expand Up @@ -34,7 +33,6 @@ def setup_client():
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}")

return client, operator_id, operator_key
except (TypeError, ValueError):
print("❌ Error: Creating client, Please check your .env file")
Expand All @@ -55,12 +53,13 @@ def create_account(client, operator_key):
recipient_id = receipt.account_id
print(f"✅ Success! Created a new recipient account with ID: {recipient_id}")
return recipient_id, recipient_key

except Exception as e:
print(f"Error creating new account: {e}")
sys.exit(1)


def create_token(client, operator_id, operator_key):
"""Create a new token"""
print("\nSTEP 2: Creating a new token...")
try:
token_tx = (
Expand All @@ -81,7 +80,9 @@ def create_token(client, operator_id, operator_key):
print(f"❌ Error creating token: {e}")
sys.exit(1)


def associate_token(client, recipient_id, recipient_key, token_id):
"""Associate token with the recipient"""
print("\nSTEP 3: Associating Token...")
try:
association_tx = (
Expand All @@ -96,57 +97,53 @@ def associate_token(client, recipient_id, recipient_key, token_id):
print(f"❌ Error associating token: {e}")
sys.exit(1)

def transfer_tokens():
"""
A full example to create a new recipent account, a fungible token, and
transfer the token to that account
"""
# Config Client
client, operator_id, operator_key = setup_client()

# Create a new recipient account.
recipient_id, recipient_key = create_account(client, operator_key)

# Create new tokens.
token_id = create_token(client, operator_id, operator_key)

# Associate Token
associate_token(client, recipient_id, recipient_key, token_id)

# Transfer Token
print("\nSTEP 4: Transfering Token...")
def transfer_transaction(client, operator_id, operator_key, recipient_id, token_id):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should stay transfer_tokens() please

"""Perform the token transfer"""
print("\nSTEP 4: Transferring Token...")
try:
# Check balance before transfer
# Check balance before
balance_before = (
CryptoGetAccountBalanceQuery(account_id=recipient_id)
.execute(client)
.token_balances
)
print("Token balance before token transfer:")
print("Token balance before transfer:")
print(f"{token_id}: {balance_before.get(token_id)}")

transfer_tx = (
tx = (
TransferTransaction()
.add_token_transfer(token_id, operator_id, -1)
.add_token_transfer(token_id, recipient_id, 1)
.freeze_with(client)
.sign(operator_key)
)
transfer_tx.execute(client)
tx.execute(client)

print("\n✅ Success! Token transfer complete.\n")

# Check balance after transfer
# Check balance after
balance_after = (
CryptoGetAccountBalanceQuery(account_id=recipient_id)
.execute(client)
.token_balances
)
print("Token balance after token transfer:")
print("Token balance after transfer:")
print(f"{token_id}: {balance_after.get(token_id)}")

except Exception as e:
print(f"❌ Error transferring token: {str(e)}")
sys.exit(1)


def main():
"""Main script runner"""
client, operator_id, operator_key = setup_client()
recipient_id, recipient_key = create_account(client, operator_key)
token_id = create_token(client, operator_id, operator_key)
associate_token(client, recipient_id, recipient_key, token_id)
transfer_transaction(client, operator_id, operator_key, recipient_id, token_id)


if __name__ == "__main__":
transfer_tokens()
main()