Skip to content

Commit

Permalink
all examples but token transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
wensheng committed Apr 28, 2021
1 parent e8e554a commit fda5e89
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 112 deletions.
69 changes: 69 additions & 0 deletions examples/consensus_pubsub_with_submit_key.py
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)
50 changes: 14 additions & 36 deletions examples/create_simple_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,44 @@

from hedera import (
Hbar,
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractCallQuery,
ContractDeleteTransaction,
)

assert "OPERATOR_ID" in os.environ
assert "OPERATOR_KEY" in os.environ

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", "")

# sending 1 hbar so make sure we don't use mainnet
assert HEDERA_NETWORK in ("testnet", "previewnet")

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)
from get_client import client, OPERATOR_KEY

cur_dir = os.path.abspath(os.path.dirname(__file__))
jsonf = open(os.path.join(cur_dir, "hello_world.json"))
hello_world = json.load(jsonf)

tran = FileCreateTransaction()
resp = tran.setKeys(OPERATOR_KEY
).setContents(hello_world['object'].encode()
).setMaxTransactionFee(Hbar(2)
).execute(client)
).setContents(hello_world['object'].encode()
).setMaxTransactionFee(Hbar(2)
).execute(client)
receipt = resp.getReceipt(client)
fileId = receipt.fileId

print("contract bytecode file: ", fileId.toString())

tran = ContractCreateTransaction()
resp = tran.setGas(500
).setBytecodeFileId(fileId
).setAdminKey(OPERATOR_KEY
).setMaxTransactionFee(Hbar(16)
).execute(client)
).setBytecodeFileId(fileId
).setAdminKey(OPERATOR_KEY
).setMaxTransactionFee(Hbar(16)
).execute(client)
receipt = resp.getReceipt(client)
contractId = receipt.contractId
print(receipt.toString())
print("new contract id: ", contractId.toString())

query = ContractCallQuery()
result = query.setGas(600
).setContractId(contractId
).setFunction("greet"
).setMaxQueryPayment(Hbar(1)
).execute(client)
).setContractId(contractId
).setFunction("greet"
).setMaxQueryPayment(Hbar(1)
).execute(client)

if result.errorMessage:
exit("error calling contract: ", result.errorMessage)
Expand All @@ -76,7 +54,7 @@

tran = ContractDeleteTransaction()
resp = tran.setContractId(contractId
).setMaxTransactionFee(Hbar(1)
).execute(client)
).setMaxTransactionFee(Hbar(1)
).execute(client)
receipt = resp.getReceipt(client)
print("Deleting contract - status: ", receipt.status.toString())
73 changes: 73 additions & 0 deletions examples/create_stateful_contract.py
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)
28 changes: 2 additions & 26 deletions examples/delete_file.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
import os
import sys
from hedera import (
AccountId,
PrivateKey,
Client,
FileId,
FileDeleteTransaction,
)

assert "OPERATOR_ID" in os.environ
assert "OPERATOR_KEY" in os.environ
from hedera import FileId, FileDeleteTransaction
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)

tran = FileDeleteTransaction()
resp = tran.setFileId(fileId).execute(client)
receipt = resp.getReceipt(client)
Expand Down
26 changes: 2 additions & 24 deletions examples/get_account_balance.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import os
from hedera import (
AccountId,
PrivateKey,
Client,
AccountBalanceQuery,
)

assert "OPERATOR_ID" in os.environ
assert "OPERATOR_KEY" in os.environ

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)
from hedera import AccountBalanceQuery
from get_client import client, OPERATOR_ID

query = AccountBalanceQuery()
response = query.setAccountId(OPERATOR_ID).execute(client)
Expand Down
28 changes: 2 additions & 26 deletions examples/get_file_contents.py
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())
Loading

0 comments on commit fda5e89

Please sign in to comment.