1

I'm trying to send raw transaction with Web3j, to call a smart contract function.

I try to set very high values for gas to avoid problems with it.

But I always get a null tx hash. What is wrong in my code ?

    // get nonce
        EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                ethereumAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
        BigInteger nonce = ethGetTransactionCount.getTransactionCount();
        log.debug("nonce: "+nonce.toString());

        // build function call
        Function function = new Function(
                "setClaim",
                Arrays.asList(new Address(subjectAddress), new Utf8String(claimId), new Utf8String(claimValue), new DynamicBytes("".getBytes())),
                Collections.emptyList());

        // encode function call to tx data
        String encodedFunction = FunctionEncoder.encode(function);
        log.debug("encodedFunction: "+encodedFunction);

        RawTransaction rawTx = RawTransaction.createTransaction(
                nonce
                ,DefaultGasProvider.GAS_PRICE.multiply(new BigInteger("1000000"))
                ,DefaultGasProvider.GAS_LIMIT.multiply(new BigInteger("1000000"))
                ,DefaultParams.contractAddress
                ,BigInteger.ZERO
                ,encodedFunction);

        byte[] signedMessage = TransactionEncoder.signMessage(rawTx, Credentials.create(privateKey));
        String hexValue = Numeric.toHexString(signedMessage);

        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();

        String transactionHash = ethSendTransaction.getTransactionHash();
        log.info("Tx hash: "+transactionHash);
        

I've tried this with Ganache and Infura. Both of them log an eth_sendRawTransaction. But I got no error, no message.

Thanks

3
  • 1
    How much is gasPrice and gasLimit? Do you have enough ethers to pay? You need at least gasPrice * gasLimit. If gasLimit is higher than the block gas limit the transaction will fail, block gas limit is 15M in mainnet, 8M in ropsten, 8M in goerli, 10M in rinkeby.
    – Ismael
    May 25 at 16:08
  • In Web3j, default gas limit=9M and gas price = 41M. To be sure it was enough, I multiplied it. As I'm on Ropsten, it's not a matter of cost, at least to test the raw tx feature. I didn't know there was top limits too.
    – Alex
    May 27 at 6:31
  • Usually you want gasLimit to be closer to how much the transaction will use. In solidity versions previous to 0.8 some error would consume all available gas.
    – Ismael
    May 27 at 18:39
1

Self answering my question for interested people:

I found in web3j doc ethSendTransaction.getError() :

EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();

if(ethSendTransaction.getError() != null)
    log.info(ethSendTransaction.getError().getMessage());

And I could get information that my gas limit was too high.

(slapping myself) Thought of the day : "If it's not broken, don't fix it".

But usefull information is that in web3j, event the default gas limit DefaultGasProvider.GAS_LIMIT is too high for testnets.

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.