1

I have a wallet in metamask in ropsten test network, and I try to send a transaction to another wallet in metamask ropsten network, using Web3j library with java.

First, I build a httpServer with infura provider:

Admin web3j = Admin.build(new HttpService("https://ropsten.infura.io/v3/[my_private_key]"));

Second, I get the nonce from network:

EthGetTransactionCount txCount = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = txCount.getTransactionCount();

Also, before make and send transaction to network, I check the balance of my account:

EthGetBalance ethGetBalance = web3j
                .ethGetBalance(from, DefaultBlockParameterName.LATEST)
                .sendAsync()
                .get();

Next, I set the gas_price and gas_limit for the transaction:

BigInteger GAS_LIMIT = Convert.toWei("0.1", Convert.Unit.MWEI).toBigInteger();
BigInteger GAS_PRICE = Convert.toWei("10.0", Convert.Unit.GWEI).toBigInteger();

(I also check the DefaultGasProvider values for gas and I get the same error!)

and then, I make raw transaction:

RawTransaction rawTRX = RawTransaction.createEtherTransaction(nonce, GAS_PRICE, GAS_LIMIT, to, value);

I load the credentials with mnemonic phrase of my meta mask account:

Credentials cr = WalletUtils.loadBip39Credentials("", [mnemonic phrase]);

and sign the transaction and convert it to hex:

byte[] signedMessage = TransactionEncoder.signMessage(rawTRX, cr);
String hexValue = Numeric.toHexString(signedMessage);

and finally I send the transaction to network:

EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();

if (ethSendTransaction.hasError()) {
    System.out.println("result: " + ethSendTransaction.getResult());
    System.out.println("error: " + ethSendTransaction.getError());
    return ethSendTransaction.getError().getMessage();
}

return ethSendTransaction.getTransactionHash();

here is the output:

nonce: 0

account balance: 10000000000000000000

gas limit: 100000

gas price: 10000000000

value: 33000000000000000

and I get the error: insufficient funds for gas * price + value

1 Answer 1

0
  1. Why do you use Admin instance instead of Web3J instance while building the http service. This doesn't affect the error, just curious to understand.

  2. You are most likely making mistake in converting the MWEI and GWEI value. Your Gas Calculation is wrong.

  3. To make your life peaceful while working with Web3J and Java, always set convert.value.ETH in the logs to actually understand values :-)

Here's what I have been using from quite some time now in production :

    public FeeCalculationResponse finalWithdrawalFees(BigDecimal ourWithdrawFees) throws IOException {
        BigInteger gasPrice=estimateGas();
        BigInteger gasLimit=BigInteger.valueOf(30000);
        BigInteger networkGasTotal=gasLimit.multiply(gasPrice);

        ourWithdrawFees=Convert.toWei(ourWithdrawFees, Convert.Unit.ETHER);
        ourWithdrawFees=ourWithdrawFees.add(new BigDecimal(networkGasTotal));

        FeeCalculationResponse response=new FeeCalculationResponse();
        response.setGasPrice(gasPrice);
        response.setGasLimit(gasLimit);
        response.setFinalWithdrawalFee(ourWithdrawFees);
        return response;

    }
    public BigInteger estimateGas() throws IOException {

        EthGasPrice gas=web3j.ethGasPrice().send();
        return gas.getGasPrice();
    }

Your Answer

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

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