0

I am transferring funds using web3j. However, I was getting transactionHash as null. I checked various posts and found that this issue exist when gas price or gas is bad. I increased both the values, and it started working. However, the same code didn't work on other machine. So, again i changed the gas price and value and it worked. But then for some other data, it didnt work. So, its random.

My conerns: 1. I can't keep changing the values dynamically, since I don't know how much gas is required at run time. 2. Most of all, Gas price and limit that i have set are already too high (please check my code below). Even the account balance will never be that much. In reality, I can't set that much value.

Please help me here.

static BigInteger GAS_PRICE = BigInteger.valueOf(new Long(18000000000L));
static BigInteger GAS_LIMIT_ETHER_TX = BigInteger.valueOf(300000);
RawTransactionManager rawTransactionManager =  new RawTransactionManager(web3j ,credentials,100,1000 * 15);

EthSendTransaction send = rawTransactionManager.sendTransaction(GAS_PRICE, GAS_LIMIT_ETHER_TX, "toAddress", "<some  data to save>", BigInteger.valueOf(1));

Even the amount to transfer is not big, and neither is my data to save. Is my gasLimit set in genesis too high, which is why it is expecting this much gas?

{
"config": {
  "chainId": 1994,
  "homesteadBlock": 0,
  "eip155Block": 0,
  "eip158Block": 0,
  "byzantiumBlock": 0
},
"difficulty": "400",
"gasLimit": "2000000",
"alloc": {
    "<root address>":{
    "balance":"1000000000000000000000000"
}}}

2 Answers 2

1

There is a function that can give you the estimated gas of a transaction

web3j.ethEstimateGas(Transaction transaction)

But I think, even if you set a very high gas limit, you bay only for the used amount of gas and not for the limit.

1

I found the solution, which might be helpful to others:

BigInteger blockGasLimit = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock().getGasLimit();

This link has most of the solutions:

https://www.programcreek.com/java-api-examples/?api=org.web3j.protocol.core.methods.request.Transaction

2
  • isn't the gas-limit for all blocks the same?
    – Majd TL
    Dec 28, 2018 at 16:53
  • Earlier I had set the same gas limit to every transaction (as you can see in my original post). But then I changed it as per my last block consumption. Jan 10, 2019 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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