Hedera-sdk-py Smart Contract Deployment and Querying
Question:
I am creating a DApp on the Hedera Blockchain using Hedera-sdk-py, a python wrapper of Hedera SDK in Java. I want to create a smart contract, deploy and query it, but I can not seem to understand the stateful.json file in the documentation (https://github.com/wensheng/hedera-sdk-py). Is the stateful.json a compiled solidity contact, how can I convert my .sol contract to that format, and how can I store data in it after deploying. I have added a link to the stateful.json file below. Any help will be really appreciated.
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(500_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(50000)
.setContractId(contractId)
.setFunction("get_message")
.setQueryPayment(Hbar(1))
.execute(client))
if result.errorMessage:
exit("error calling contract: ", result.errorMessage)
message = result.getString(0)
print("contract returned message: ", message)
resp = (ContractExecuteTransaction()
.setGas(200_000)
.setContractId(contractId)
.setFunction("set_message",
ContractFunctionParameters().addString("hello from hedera again!")
)
.setMaxTransactionFee(Hbar(2))
.execute(client))
# if this doesn't throw, then we know the contract executed successfully
receipt = resp.getReceipt(client)
# now query contract
result = (ContractCallQuery()
.setGas(50000)
.setContractId(contractId)
.setFunction("get_message")
.setQueryPayment(Hbar(1))
.execute(client))
if result.errorMessage:
exit("error calling contract: ", result.errorMessage)
message = result.getString(0)
print("contract returned message: ", message)
Stateful.json
https://github.com/wensheng/hedera-sdk-py/blob/main/examples/stateful.json
Answers:
You can use Remix IDE to get that info.
If you compile a contract, you will find an {Contract Name}.json in contracts/artifacts.
Open it and find "object".
You will see what you want.
You need to look at solidity compilers such as solc or tools like truffle and hardhat which will compile your solidity code and output the result in a .json file which you can then import into your Python project.
here are a few links that may be of use:
https://hedera.com/blog/how-to-deploy-smart-contracts-on-hedera-using-truffle
and
https://docs.soliditylang.org/en/v0.8.17/installing-solidity.html
I am creating a DApp on the Hedera Blockchain using Hedera-sdk-py, a python wrapper of Hedera SDK in Java. I want to create a smart contract, deploy and query it, but I can not seem to understand the stateful.json file in the documentation (https://github.com/wensheng/hedera-sdk-py). Is the stateful.json a compiled solidity contact, how can I convert my .sol contract to that format, and how can I store data in it after deploying. I have added a link to the stateful.json file below. Any help will be really appreciated.
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(500_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(50000)
.setContractId(contractId)
.setFunction("get_message")
.setQueryPayment(Hbar(1))
.execute(client))
if result.errorMessage:
exit("error calling contract: ", result.errorMessage)
message = result.getString(0)
print("contract returned message: ", message)
resp = (ContractExecuteTransaction()
.setGas(200_000)
.setContractId(contractId)
.setFunction("set_message",
ContractFunctionParameters().addString("hello from hedera again!")
)
.setMaxTransactionFee(Hbar(2))
.execute(client))
# if this doesn't throw, then we know the contract executed successfully
receipt = resp.getReceipt(client)
# now query contract
result = (ContractCallQuery()
.setGas(50000)
.setContractId(contractId)
.setFunction("get_message")
.setQueryPayment(Hbar(1))
.execute(client))
if result.errorMessage:
exit("error calling contract: ", result.errorMessage)
message = result.getString(0)
print("contract returned message: ", message)
Stateful.json
https://github.com/wensheng/hedera-sdk-py/blob/main/examples/stateful.json
You can use Remix IDE to get that info.
If you compile a contract, you will find an {Contract Name}.json in contracts/artifacts.
Open it and find "object".
You will see what you want.
You need to look at solidity compilers such as solc or tools like truffle and hardhat which will compile your solidity code and output the result in a .json file which you can then import into your Python project.
here are a few links that may be of use:
https://hedera.com/blog/how-to-deploy-smart-contracts-on-hedera-using-truffle
and
https://docs.soliditylang.org/en/v0.8.17/installing-solidity.html