[Ethereum] How to transfer from one ether wallet to another

etherethereumj

I am trying to transfer ether programatically with EthereumJ from my wallet to anothers. I have seen A similar question here but the answer did not define and initialize all the variables, nor did the link to code. Can someone post a full example of how to do this? I am not new to java nor bitcoin, but ether has proven to be very complicated to me.

Best Answer

This is completely off the top of my head and I don't have a working project with EthereumJ on my dev machine yet. But the general idea is as follows. I just added some extra comments to help explain.

import org.ethereum.core.*;
import org.ethereum.crypto.ECKey;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.util.ByteUtil;
import org.spongycastle.util.encoders.Hex;
import org.springframework.context.annotation.Bean;

import java.math.BigInteger;

public class sendEth{
    // Amount in ether to send
    BigInteger etherToSend = BigInteger.valueOf(100);//change 100 to any amount of eth
    // Weis in 1 ether
    BigInteger weiInEther = BigInteger.valueOf(1_000_000_000_000_000_000L);//This remains unchanged
    BigInteger weiToSend = weiInEther.multiply(etherToSend);//this converts the whole number from "etherToSend" into wei 
    BigInteger nonce = ethereum.getRepository().getNonce(senderKey.getAddress());//this gets the current nonce

    /*now we call Transaction.class "Transaction tx = new Transaction(nonce,gasPrice,gasLimit,recieving address,amount to send(in wei))"
    */
    Transaction tx = new Transaction(
          ByteUtil.bigIntegerToBytes(nonce),//adds the transaction nonce to transaction
          ByteUtil.longToBytesNoLeadZeroes(ethereum.getGasPrice()),//adds the gas current gas price
          ByteUtil.longToBytesNoLeadZeroes(3_000_000),  // adds the Gas limit
          receiveAddress,// adds the destination address of the transaction
          ByteUtil.bigIntegerToBytes(weiToSend),  //adds the Amount to send in wei
          new byte[0]  // We don't need to send any data
    ); 
    tx.sign(senderKey);//this signs the transaction with your private key
    ethereum.submitTransaction(tx);//this submits it to the blockchain
}

There's also this https://github.com/ethereum/ethereumj/blob/develop/ethereumj-core/src/main/java/org/ethereum/samples/SendTransaction.java