使用简单的比较运算符调用 hedera 智能合约函数失败并显示 CONTRACT_REVERT_EXECUTED 状态

Calling a hedera smart contract function with a simple comparison operator fails with CONTRACT_REVERT_EXECUTED status

抱歉新手问题。我正在试验 hedera 智能合约。每当尝试调用一个将 uint 参数与合约的 uint 成员进行比较的简单函数时,我都会系统地获得 CONTRACT_REVERT_EXECUTED 状态。

团结

    function compare(uint number_) public view returns (bool){
        
        return (number_ > secret_number);
    }

java

    public static boolean compare(Client client, ContractId contractId, int guess) throws TimeoutException, PrecheckStatusException
    {
         // Calls a function of the smart contract
        ContractCallQuery contractQuery = new ContractCallQuery()
             //Set the gas for the query
             .setGas(100_000) 
             //Set the contract ID to return the request for
             .setContractId(contractId)
             //Set the function of the contract to call 
             .setFunction("compare", new ContractFunctionParameters().addUint32(guess))
             //Set the query payment for the node returning the request
             //This value must cover the cost of the request otherwise will fail 
             .setQueryPayment(new Hbar(4)); 

        //Submit to a Hedera network
        ContractFunctionResult getMessage = contractQuery.execute(client);

        
        return getMessage.getBool(0);
    }

异常 * 线程“main”中的异常 com.hedera.hashgraph.sdk.PrecheckStatusException:Hedera 事务 0.0.34318751@1651508840.487521537 预检查失败,状态为 CONTRACT_REVERT_EXECUTED 在 com.hedera.hashgraph.sdk.Executable$GrpcRequest.mapStatusException(Executable.java:457) 在 com.hedera.hashgraph.sdk.Executable.execute(Executable.java:241) 在 com.hedera.hashgraph.sdk.Query.execute(Query.java:29) 在 com.hedera.hashgraph.sdk.Executable.execute(Executable.java:189) 在 com.hedera.hashgraph.sdk.Query.execute(Query.java:29) 在 hbarTexting.GuessNumberSmartContract.compare(GuessNumberSmartContract.java:132) 在 hbarTexting.GuessNumberSmartContract.main(GuessNumberSmartContract.java:257) *

我做错了什么?

非常感谢任何帮助!

根据sdk的单元测试,有3种情况会抛出opcode:

  1. 未设置合约函数参数时无法执行合约
    测试:ContractExecuteIntegrationTest

  2. 未设置合约函数时无法调用合约函数
    测试:ContractCallIntegrationTest

  3. 未设置构造函数参数时无法创建合约
    测试:ContractCreateIntegrationTest

金老师 :-)

我在正确设置合约功能参数后终于让它工作了

    public static String tryNumberGuess(Client client, ContractId contractId, int guess) throws TimeoutException, PrecheckStatusException
{
     // Calls a function of the smart contract
    ContractCallQuery contractQuery = new ContractCallQuery()
         //Set the gas for the query
         .setGas(1000_000) 
         //Set the contract ID to return the request for
         .setContractId(contractId)
         //Set the function of the contract to call 
         .setFunction("guess", new ContractFunctionParameters().addUint256(new BigInteger(""+guess)))
         //Set the query payment for the node returning the request
         //This value must cover the cost of the request otherwise will fail 
         .setQueryPayment(new Hbar(4)); 

    //Submit to a Hedera network
    ContractFunctionResult getMessage = contractQuery.execute(client);

    
    return getMessage.getString(0);
}

在使用 solidity 版本 0.7.0 编译后,函数参数需要类型为 Uint256。换句话说,Uint32 没有工作,但这不仅在 remix 中编译和测试后很清楚。从最初的 solidity 源代码中不清楚...