The problem is that I am trying to make free read-only calls to a smart contract on the Hedera network, but am encountering unexpected results. I have tried various methods, but am unable to successfully make the calls without incurring charges. I am looking for a solution or guidance on how to properly make these free read-only calls to the smart contract on Hedera.

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

I expected this get_message to not charge me HBAR since that function just returns a hardcoded string but I cant execute it for free like I want to. How do I do this?

1

There are 1 best solutions below

0
On

If you're using the SDK, using ContractCallQuery() is better suited for read-only queries. See sample below:

// 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);

Note that the SDK still requires some small amount of gas.

There are a couple of other ways to do cost-free queries.