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.
[Ethereum] How to transfer ether from one account to another using EthereumJ
ethereumj
Related Solutions
Firstly you will need the following Maven pom.xml dependency:
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.2.0-RELEASE</version>
<!-- <type>zip</type> -->
</dependency>
And here is the code to create a private key / account pair:
import org.ethereum.crypto.ECKey;
import org.spongycastle.util.encoders.Hex;
public class CreateAccount {
public static void main(String[] args) {
ECKey key = new ECKey();
byte[] addr = key.getAddress();
byte[] priv = key.getPrivKeyBytes();
String addrBase16 = Hex.toHexString(addr);
String privBase16 = Hex.toHexString(priv);
System.out.println("Address : " + addrBase16);
System.out.println("Private Key : " + privBase16);
}
}
Running the code twice produces the following output:
Address : 82d4b1c01afaf7f25bb21fd0b4b4c4a7eb7120ce
Private Key : 133965f412d1362645cbd963619023585abc8765c7372ed238374acb884b2b3a
Address : 68ccabefc7f4ae21ce0df1d98e50e099d7fc290f
Private Key : 1caeb7f26cb9f3cc7d9d0dbcdd3cf3cb056dbc011ec9013e8f3b8cdb2f193b32
Verifying the information with https://www.myetherwallet.com/#view-wallet-info:
And note that the accounts created in EthereumJ are all lowercase while the accounts generated from the private keys using MyEtherWallet are mixed case. This is because MyEtherWallet is using the new checksummed accounts. See Yet another cool checksum address encoding #55.
I finally found a solution. As discussed in the other answer, you need to parse and append the init parameters to the contract code before sending the transaction. I did't found actually an elegant and generic way to parse arguments directly in Java (but it should not hard to do it) so I decoded the ABI syntax into byte[].
First of all copy the StandaloneBlockchain.java and add one method (sorry, I can't find an easy way to do it via extension):
public SolidityContract submitNewContract(String soliditySrc, String contractName, byte[] initParameters) {
SolidityContractImpl contract = createContract(soliditySrc, contractName);
submitNewTx(new PendingTx(new byte[0], BigInteger.ZERO, ByteUtil.merge(Hex.decode(contract.getBinary()), initParameters), contract, null));
return contract;
}
Then in the StandaloneBlockchainSample.java, refer the modified class, modify the sample contract to have 2 parameters in the constructor (I didn't have time to check all types, but it should work without problems) and assign the parameters init values:
// Pretty simple (and probably the most expensive) Calculator
private static final String contractSrc =
"contract Calculator {" +
" int public result;" + // public field can be accessed by calling 'result' function
" string testString;" +
" bool testBool;"+
" function add(int num) {" +
" result = result + num;" +
" }" +
" function sub(int num) {" +
" result = result - num;" +
" }" +
" function mul(int num) {" +
" result = result * num;" +
" }" +
" function div(int num) {" +
" result = result / num;" +
" }" +
" function clear() {" +
" result = 0;" +
" }" +
" function getString() constant returns (string) { return testString; }" +
" function getBool() constant returns (bool) { return testBool; }" +
" function Calculator(string _testString, bool _testBool){" +
" testString = _testString;" +
" testBool = _testBool;" +
" }" +
"}";
[...]
String parameters = "{ 'inputs': [ { 'name': '_test', 'type': 'string' }, { 'name': '_test2', 'type': 'bool' } ] }".replaceAll("'", "\"");
byte[] initParameters = CallTransaction.Function.fromJsonInterface(parameters).encodeArguments("Test", true);
SolidityContract calc = bc.submitNewContract(contractSrc, null, initParameters);
System.out.println("Calculating...");
After that, check the correct value of the parameters with:
assertEqual("Test", (String) calc.callConstFunction("getString")[0]);
assertEqual(true, (boolean) calc.callConstFunction("getBool")[0]);
Note that you need to modify the private assertEqual method inside the class to assert other types aside from the already defined BigInteger. I tested this solution with EthereumJ 1.2.0: maybe next releases will include a better way to pass init values to the constructor, so update the answer if you will be aware of that.
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: