Hedera-sdk-py智能合约部署和查询



我正在使用Hedera- SDK -py (Hedera SDK的Java python包装器)在Hedera区块链上创建一个DApp。我想创建一个智能合约,部署和查询它,但我似乎不能理解有状态。Json文件中的文档(https://github.com/wensheng/hedera-sdk-py)。是有状态。如何将我的.sol合同转换为该格式,以及如何在部署后将数据存储在其中。我添加了一个链接到状态。Json文件如下。如有任何帮助,我将不胜感激。

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.jsonhttps://github.com/wensheng/hedera-sdk-py/blob/main/examples/stateful.json

你需要看看solc之类的solidity编译器,或者像truffle和hardhat这样的工具,它们会编译你的solidity代码,并将结果输出到一个.json文件中,然后你可以将这个文件导入到你的Python项目中。

这里有一些可能有用的链接:

https://hedera.com/blog/how-to-deploy-smart-contracts-on-hedera-using-truffle和https://docs.soliditylang.org/en/v0.8.17/installing-solidity.html

您可以使用Remix IDE来获取该信息。如果你编译一个契约,你会发现一个{契约名称}。契约/工件中的Json。打开它,找到"object"。你会看到你想要的。

最新更新