-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
273 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
import time | ||
from random import randint | ||
|
||
from hedera import ( | ||
PrivateKey, | ||
TopicCreateTransaction, | ||
TopicMessageQuery, | ||
TopicMessageSubmitTransaction, | ||
PyConsumer, | ||
) | ||
from get_client import client | ||
from jnius import autoclass | ||
|
||
messagesToPublish = 5 | ||
secondsBetweenMessages = 2 | ||
|
||
# this need to be cleaned up | ||
mirror_node_address = os.environ.get('MIRROR_NODE_ADDRESS', "hcs.testnet.mirrornode.hedera.com:5600") | ||
JList = autoclass("java.util.List") | ||
mirror_node = JList.of(mirror_node_address) | ||
client.setMirrorNetwork(mirror_node) | ||
|
||
# createTopicWithSubmitKey | ||
submitKey = PrivateKey.generate() | ||
submitPublicKey = submitKey.getPublicKey() | ||
|
||
resp = TopicCreateTransaction( | ||
).setTopicMemo("HCS topic with submit key" | ||
).setSubmitKey(submitPublicKey | ||
).execute(client) | ||
|
||
topicId = resp.getReceipt(client).topicId | ||
print("Created new topic ", topicId.toString(), " with ED25519 submitKey of ", submitKey.toString()) | ||
|
||
time.sleep(5) | ||
|
||
|
||
def showMsg(*args): | ||
print("time: {} received topic message: {}".format(args[0], args[2])) | ||
|
||
|
||
# subscribeToTopic | ||
# will not use this: .setStartTime(Instant.ofEpochSecond(0)) | ||
# Instant is org.threeten.bp backport, but hedera sdk already use at least java 8 | ||
query = TopicMessageQuery( | ||
).setTopicId(topicId | ||
).subscribe(client, PyConsumer(showMsg)) | ||
|
||
time.sleep(2) | ||
|
||
# publishMessagesToTopic | ||
for i in range(messagesToPublish): | ||
message = "random message " + str(randint(0, 10 ** 9)) | ||
print("Publishing message: ", message) | ||
|
||
# The transaction is automatically signed by the payer. | ||
# Due to the topic having a submitKey requirement, additionally sign the transaction with that key. | ||
receipt = TopicMessageSubmitTransaction( | ||
).setTopicId(topicId | ||
).setMessage(message | ||
).freezeWith(client | ||
).sign(submitKey | ||
).execute(client | ||
).transactionId.getReceipt(client) | ||
|
||
time.sleep(secondsBetweenMessages) | ||
|
||
time.sleep(10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import os | ||
import json | ||
|
||
from hedera import ( | ||
Hbar, | ||
FileCreateTransaction, | ||
ContractCreateTransaction, | ||
ContractCallQuery, | ||
ContractExecuteTransaction, | ||
ContractFunctionParameters, | ||
) | ||
from get_client import client, OPERATOR_KEY | ||
|
||
client.setMaxTransactionFee(Hbar(100)) | ||
client.setMaxQueryPayment(Hbar(10)) | ||
|
||
cur_dir = os.path.abspath(os.path.dirname(__file__)) | ||
jsonf = open(os.path.join(cur_dir, "stateful.json")) | ||
stateful_json = json.load(jsonf) | ||
jsonf.close() | ||
byteCode = stateful_json['object'].encode() | ||
|
||
tran = FileCreateTransaction() | ||
resp = tran.setKeys(OPERATOR_KEY | ||
).setContents(byteCode | ||
).execute(client) | ||
fileId = resp.getReceipt(client).fileId | ||
print("contract bytecode file: ", fileId.toString()) | ||
|
||
tran = ContractCreateTransaction() | ||
resp = tran.setGas(100_000_000 | ||
).setBytecodeFileId(fileId | ||
).setConstructorParameters( | ||
ContractFunctionParameters().addString("hello from hedera!") | ||
).execute(client) | ||
contractId = resp.getReceipt(client).contractId | ||
print("new contract id: ", contractId.toString()) | ||
|
||
# 600 < gas fee < 1000 | ||
result = ContractCallQuery( | ||
).setGas(1000 | ||
).setContractId(contractId | ||
).setFunction("get_message" | ||
).execute(client) | ||
|
||
if result.errorMessage: | ||
exit("error calling contract: ", result.errorMessage) | ||
|
||
message = result.getString(0) | ||
print("contract returned message: ", message) | ||
|
||
resp = ContractExecuteTransaction( | ||
).setGas(100_000_000 | ||
).setContractId(contractId | ||
).setFunction("set_message", | ||
ContractFunctionParameters().addString("hello from hedera again!") | ||
).execute(client) | ||
|
||
# if this doesn't throw then we know the contract executed successfully | ||
receipt = resp.getReceipt(client) | ||
|
||
# now query contract | ||
result = ContractCallQuery( | ||
).setGas(100_000_000 | ||
).setContractId(contractId | ||
).setFunction("get_message" | ||
).execute(client) | ||
|
||
if result.errorMessage: | ||
exit("error calling contract: ", result.errorMessage) | ||
|
||
message = result.getString(0) | ||
print("contract returned message: ", message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,11 @@ | ||
import sys | ||
import os | ||
from hedera import ( | ||
AccountId, | ||
PrivateKey, | ||
Client, | ||
FileId, | ||
FileContentsQuery, | ||
) | ||
|
||
assert "OPERATOR_ID" in os.environ | ||
assert "OPERATOR_KEY" in os.environ | ||
from hedera import FileId, FileContentsQuery | ||
from get_client import client | ||
|
||
if len(sys.argv) < 2: | ||
exit("need a file id") | ||
|
||
fileId = FileId.fromString(sys.argv[1]) | ||
|
||
OPERATOR_ID = AccountId.fromString(os.environ["OPERATOR_ID"]) | ||
OPERATOR_KEY = PrivateKey.fromString(os.environ["OPERATOR_KEY"]) | ||
HEDERA_NETWORK = os.environ.get("HEDERA_NETWORK", "testnet") | ||
CONFIG_FILE = os.environ.get("CONFIG_FILE", "") | ||
|
||
if HEDERA_NETWORK == "previewnet": | ||
client = Client.forPreviewnet() | ||
elif HEDERA_NETWORK == "testnet": | ||
client = Client.forTestnet() | ||
else: | ||
client = Client.fromConfigFile(CONFIG_FILE) | ||
|
||
client.setOperator(OPERATOR_ID, OPERATOR_KEY) | ||
|
||
query = FileContentsQuery() | ||
contents = query.setFileId(fileId).execute(client) | ||
print("File content query results: ", contents.toStringUtf8()) |
Oops, something went wrong.