[Ethereum] How to transfer ether from one account to another using EthereumJ

ethereumj

For testing purpose in test network we are creating account dynamically using ethereum java (EcKey). I have an account that contains enough ethers. How can i transfer ether from the source account to the account newly created using ethereum java. Please suggest.

Best Answer

Check any sample from samples source directory how to deal with Transactions, for example, this one: CreateContractSample.java,

Your transaction call should be something like this:

// Amount in ether to send
BigInteger etherToSend = BigInteger.valueOf(100);
// Weis in 1 ether
BigInteger weisInEther = BigInteger.valueOf(1_000_000_000_000_000_000L);
BigInteger weisToSend = weisInEther.multiply(etherToSend);
BigInteger nonce = ethereum.getRepository().getNonce(senderKey.getAddress());

Transaction tx = new Transaction(
      ByteUtil.bigIntegerToBytes(nonce),
      ByteUtil.longToBytesNoLeadZeroes(ethereum.getGasPrice()),
      ByteUtil.longToBytesNoLeadZeroes(3_000_000),  // Gas limit
      receiveAddress,
      ByteUtil.bigIntegerToBytes(weisToSend),  // Amount in weis
      new byte[0]  // We don't need to send any data
     ); 
tx.sign(senderKey);
ethereum.submitTransaction(tx);
Related Topic