I'm trying to estimate gas cost of a contract call, i need to send a string to a contract, but i need to see if i overtook the GAS limit.

I'm trying like this:

    Transaction t = Transaction.createContractTransaction(myAddress, BigInteger.valueOf(100), gasPrice, gasLimit, null, "0123456789");
    EthEstimateGas eg = web3j.ethEstimateGas(t).send();
    System.out.println("EstimateGas: " + eg.getAmountUsed().toByteArray());

But i receive the error:

Erro: org.web3j.exceptions.MessageDecodingException: Value must be in format 0x[1-9]+[0-9]* or 0x0


What you are doing is sending a message to the contract (which needs to be hex-encoded). In this case, you want the message to say 'execute function with the following params'.

Take a look at FunctionEncoder.encode that should give you what you need.

  • In this case, i need to put the hex on Transaction creation in my code example? – SkyWorker Oct 22 '19 at 13:56
  • Exactly, take the encoded output from the function encoder and pass it into the transaction. – Antony Denyer Oct 24 '19 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.