Sorry for the newbie question. I am experimenting with hedera Smart Contracts. Whenever trying to call a simple function which compares the uint argument with a uint member of the contract I systematically get a CONTRACT_REVERT_EXECUTED status.

solidity

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

Exception * Exception in thread "main" com.hedera.hashgraph.sdk.PrecheckStatusException: Hedera transaction [email protected] failed pre-check with the status CONTRACT_REVERT_EXECUTED at com.hedera.hashgraph.sdk.Executable$GrpcRequest.mapStatusException(Executable.java:457) at com.hedera.hashgraph.sdk.Executable.execute(Executable.java:241) at com.hedera.hashgraph.sdk.Query.execute(Query.java:29) at com.hedera.hashgraph.sdk.Executable.execute(Executable.java:189) at com.hedera.hashgraph.sdk.Query.execute(Query.java:29) at hbarTexting.GuessNumberSmartContract.compare(GuessNumberSmartContract.java:132) at hbarTexting.GuessNumberSmartContract.main(GuessNumberSmartContract.java:257) *

What am I doing wrong here?

Any help greatly appreciated!

3

There are 3 answers

0
Aobara On BEST ANSWER

I finally got it to work after properly setting contract function parameters

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

Turns out function parameter required type Uint256, after compilation with solidity version 0.7.0. In other words Uint32 did not work, but that was not only clear after compilation and testing within remix. Not clear from the initial solidity source code...

0
Trace On

According to the sdk's unit tests, there are three cases where the opcode is thrown:

  1. Cannot execute contract when contract function parameters are not set
    Test: ContractExecuteIntegrationTest

  2. Cannot call contract function when contract function is not set
    Test: ContractCallIntegrationTest

  3. Cannot create contract when constructor parameters are not set
    Test: ContractCreateIntegrationTest

Gr Kim :-)

1
Syedur Rahman On

Hedera will fail Hedera is a distributed public network designed to provide a secure, decentralized, and open platform for businesses and developers to create and operate applications. It is not designed to fail, but it is possible for the network or applications built on it to experience a variety of issues, such as network slowdowns, security vulnerabilities, or scalability issues