Skip to content

Commit

Permalink
Merge pull request #31 from nadineloepfe/imports
Browse files Browse the repository at this point in the history
topic info and message query
  • Loading branch information
nadineloepfe authored Jan 2, 2025
2 parents d774687 + 8aca40f commit 1f5da94
Show file tree
Hide file tree
Showing 65 changed files with 2,102 additions and 340 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ ADMIN_KEY=<ADMIN_KEY>
TOKEN_ID=<TOKEN_ID>
TOKEN_NAME=<TOKEN_NAME>
TOKEN_SYMBOL=<TOKEN_SYMBOL>
TOPIC_ID=<TOPIC_ID>
NETWORK=<NETWORK>
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
PUBLIC_KEY: ${{ steps.solo.outputs.publicKey }}
NETWORK: solo
run: |
python test.py
python test.py
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ src/hedera_sdk_python/hapi

# Mac OS X Files
.DS_Store

# Intellij
.idea

# Pytest
.pytest_cache

uv.lock
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/GitLink.xml

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/hedera_sdk_python.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

1 change: 0 additions & 1 deletion .python-version

This file was deleted.

72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ submitting messages.
- [Deleting a Token](#deleting-a-token)
- [Transferring HBAR](#transferring-hbar)
- [Creating a Topic](#creating-a-topic)
- [Submitting a Topic Message](#submitting-a-topic-message)
- [Updating a Topic](#updating-a-topic)
- [Deleting a Topic](#deleting-a-topic)
- [Querying Topic](#querying-topic)
- [Querying Topic Message](#querying-topic-message)
- [Contributing](#contributing)

## Installation
Expand Down Expand Up @@ -70,6 +75,7 @@ OPERATOR_KEY=302e020100300506032b657004220420...
ADMIN_KEY=302a300506032b65700321009308ecfdf...
RECIPIENT_ID=0.0.789xx
TOKEN_ID=0.0.100xx
TOPIC_ID=0.0.200xx
NETWORK=testnet
```

Expand Down Expand Up @@ -99,6 +105,10 @@ Token creation successful. Token ID: 0.0.5025xxx
Token association successful.
Token transfer successful.
Token deletion successful.
Topic creation successful.
Topic Message submitted.
Topic update successful.
Topic deletion successful.
```

## Usage
Expand Down Expand Up @@ -209,6 +219,68 @@ transaction = (
transaction.execute(client)
```

### Submitting a Topic Message

```
transaction = (
TopicMessageSubmitTransaction(topic_id=topic_id, message="Hello, from Python SDK!")
.freeze_with(client)
.sign(operator_key)
)
transaction.execute(client)
```

### Updating a Topic

```
transaction = (
TopicUpdateTransaction(topic_id=topic_id, memo="Python SDK updated topic")
.freeze_with(client)
.sign(operator_key)
)
transaction.execute(client)
```

### Deleting a Topic

```
transaction = (
TopicDeleteTransaction(topic_id=topic_id)
.freeze_with(client)
.sign(operator_key)
)
transaction.execute(client)
```

### Querying Topic Info

```
topic_info_query = (
TopicInfoQuery()
.set_topic_id(topic_id)
)
topic_info = topic_info_query.execute(client)
print(topic_info)
```

### Querying Topic Message

```
query = (
TopicMessageQuery()
.set_topic_id(topic_id)
.set_start_time(datetime.utcnow())
.set_chunking_enabled(True)
.set_limit(0)
)
query.subscribe(client)
```

## Contributing

Contributions are welcome! Please follow these steps:
Expand Down
3 changes: 0 additions & 3 deletions examples/account_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import sys
from dotenv import load_dotenv

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
Expand Down
3 changes: 0 additions & 3 deletions examples/query_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import time
from dotenv import load_dotenv

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
Expand Down
3 changes: 0 additions & 3 deletions examples/query_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import sys
from dotenv import load_dotenv

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
Expand Down
25 changes: 25 additions & 0 deletions examples/query_topic_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from dotenv import load_dotenv
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.client.client import Client
from hedera_sdk_python.consensus.topic_id import TopicId
from hedera_sdk_python.query.topic_info_query import TopicInfoQuery
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey

def query_topic_info():
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
topic_id = TopicId.from_string(os.getenv('TOPIC_ID'))

network = Network(network='testnet')
client = Client(network)
client.set_operator(operator_id, operator_key)

query = TopicInfoQuery().set_topic_id(topic_id)

topic_info = query.execute(client)
print("Topic Info:", topic_info)

if __name__ == "__main__":
query_topic_info()
44 changes: 44 additions & 0 deletions examples/query_topic_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import time
from datetime import datetime
from dotenv import load_dotenv

from hedera_sdk_python.client.network import Network
from hedera_sdk_python.client.client import Client
from hedera_sdk_python.query.topic_message_query import TopicMessageQuery

load_dotenv()

def query_topic_messages():
network = Network(network='testnet')
client = Client(network)

def on_message_handler(topic_message):
print(f"Received topic message: {topic_message}")

def on_error_handler(e):
print(f"Subscription error: {e}")
if "Stream removed" in str(e):
print("Reconnecting due to stream removal...")
query_topic_messages()

query = (
TopicMessageQuery()
.set_topic_id(os.getenv('TOPIC_ID'))
.set_start_time(datetime.utcnow())
.set_chunking_enabled(True)
.set_limit(0)
)

query.subscribe(
client,
on_message=on_message_handler,
on_error=on_error_handler
)

print("Subscription started. Waiting for messages...")
while True:
time.sleep(10)

if __name__ == "__main__":
query_topic_messages()
3 changes: 0 additions & 3 deletions examples/token_associate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import sys
from dotenv import load_dotenv

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
Expand Down
3 changes: 0 additions & 3 deletions examples/token_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import sys
from dotenv import load_dotenv

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, project_root)

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
Expand Down
38 changes: 38 additions & 0 deletions examples/topic_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import sys
from dotenv import load_dotenv

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.consensus.topic_id import TopicId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.consensus.topic_delete_transaction import TopicDeleteTransaction

load_dotenv()

def delete_topic():
network = Network(network='testnet')
client = Client(network)

operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
topic_id = TopicId.from_string(os.getenv('TOPIC_ID'))

client.set_operator(operator_id, operator_key)

transaction = (
TopicDeleteTransaction(topic_id=topic_id)
.freeze_with(client)
.sign(operator_key)
)

try:
receipt = transaction.execute(client)
print(f"Topic {topic_id} deleted successfully.")
except Exception as e:
print(f"Topic deletion failed: {str(e)}")
sys.exit(1)

if __name__ == "__main__":
delete_topic()
38 changes: 38 additions & 0 deletions examples/topic_message_submit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import sys
from dotenv import load_dotenv

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.consensus.topic_id import TopicId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.consensus.topic_message_submit_transaction import TopicMessageSubmitTransaction

load_dotenv()

def submit_message(message):
network = Network(network='testnet')
client = Client(network)

operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
topic_id = TopicId.from_string(os.getenv('TOPIC_ID'))

client.set_operator(operator_id, operator_key)

transaction = (
TopicMessageSubmitTransaction(topic_id=topic_id, message=message)
.freeze_with(client)
.sign(operator_key)
)

try:
receipt = transaction.execute(client)
print(f"Message submitted to topic {topic_id}: {message}")
except Exception as e:
print(f"Message submission failed: {str(e)}")
sys.exit(1)

if __name__ == "__main__":
submit_message("Hello, Hedera!")
Loading

0 comments on commit 1f5da94

Please sign in to comment.