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