如何在Hedera区块链网络上对智能合约进行免费只读调用而不产生费用?



问题是,我试图对Hedera网络上的智能合约进行免费只读调用,但遇到了意想不到的结果。我尝试了各种方法,但都无法成功拨打而不产生费用。我正在寻找关于如何正确地对Hedera上的智能合约进行这些免费只读调用的解决方案或指导。

//Create the transaction
const transaction = new ContractExecuteTransaction()
.setContractId(newContractId)
.setFunction("get_message")

我希望这个get_message不会向我收取HBAR费用,因为该函数只是返回一个硬编码字符串,但我不能像我想的那样免费执行它。我该怎么做呢?

如果您正在使用SDK,那么使用ContractCallQuery()更适合只读查询。参见下面的示例:

// Query the contract to check changes in state variable
const contractQueryTx1 = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("get_message";
const contractQuerySubmit1 = await contractQueryTx1.execute(client);

请注意,SDK仍然需要一些少量的气体。

还有其他几种方法可以实现无成本查询。

  • 使用镜像节点。这两个教程可以为您提供有关使用镜像节点的额外信息:https://hedera.com/blog/how-to-inspect-smart-contract-transactions-on-hedera-using-mirror-nodes和https://hedera.com/blog/how-to-look-up-transaction-history-on-hedera-using-mirror-nodes-back-to-the-basics

  • 如果您使用Hashio (https://swirldslabs.com/hashio/)作为JSON-RPC中继,那么您可以使用EVM工具在Hedera上部署并与契约交互。然后,你可以像在以太坊这样的链中那样简单地调用合约。下面是一些例子:https://github.com/hashgraph/hedera-json-rpc-relay/tree/main/tools

最新更新