Skip to content

Commit

Permalink
examples for topic transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
nadineloepfe committed Jan 1, 2025
1 parent 4e753b5 commit fb96aad
Show file tree
Hide file tree
Showing 6 changed files with 653 additions and 6 deletions.
1 change: 0 additions & 1 deletion .python-version

This file was deleted.

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!")
38 changes: 38 additions & 0 deletions examples/topic_update.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_update_transaction import TopicUpdateTransaction

load_dotenv()

def update_topic(new_memo):
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 = (
TopicUpdateTransaction(topic_id=topic_id, memo=new_memo)
.freeze_with(client)
.sign(operator_key)
)

try:
receipt = transaction.execute(client)
print(f"Topic {topic_id} updated with new memo: {new_memo}")
except Exception as e:
print(f"Topic update failed: {str(e)}")
sys.exit(1)

if __name__ == "__main__":
update_topic("Updated topic memo")
5 changes: 0 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,6 @@ def delete_token(client, token_id, admin_key):
def main():
operator_id, operator_key = load_operator_credentials()
admin_key = PrivateKey.generate()
admin_public_key = admin_key.public_key()
admin_public_key_bytes = admin_public_key.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
)

network_type = os.getenv('NETWORK')
network = Network(network=network_type)
Expand Down
Loading

0 comments on commit fb96aad

Please sign in to comment.