1

I'm trying to transfer ETH from one account to another using web3j and a java program using following code.

public void transfer (String from, String to, BigInteger amount) throws InterruptedException, ExecutionException, IOException, CipherException{
     Credentials credentials1 = WalletUtils.loadCredentials(
            "mypassword", 
            "path\\to\\keystore");

     EthGetTransactionCount ethGetTransactionCount = web3.ethGetTransactionCount(
             from, DefaultBlockParameterName.LATEST).sendAsync().get();

     BigInteger nonce = ethGetTransactionCount.getTransactionCount();
     System.out.println(nonce); 

     RawTransaction rawTransaction = RawTransaction.createEtherTransaction (
             nonce, Convert.toWei("22", Convert.Unit.MWEI).toBigInteger(), Convert.toWei("44", Convert.Unit.GWEI).toBigInteger(), to, amount);
     byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials1);
     String hexValue = Numeric.toHexString(signedMessage);

     EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(hexValue).sendAsync().get();
     String transactionHash = ethSendTransaction.getTransactionHash();
}

But I cannot get it to work. Also the nonce doesn't change and keeps returning 44.

I am running it on the Ropsten test net.

Any thoughts?

Thanks, Coen

1 Answer 1

1

With some help from the creator of web3 I was able to solve this.

Adding:

...
static final BigInteger GAS_PRICE = BigInteger.valueOf(20_000_000_000L);
static final BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);
...

and

...
RawTransaction rawTransaction = RawTransaction.createEtherTransaction (
             nonce, GAS_PRICE, GAS_LIMIT, to, amount);
...

I managed to transfer the ether.

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.