Skip to content

Instantly share code, notes, and snippets.

@skydogch
Created December 1, 2015 14:31
Show Gist options
  • Save skydogch/47d8387de606eec4becd to your computer and use it in GitHub Desktop.
Save skydogch/47d8387de606eec4becd to your computer and use it in GitHub Desktop.
import org.ethereum.core.CallTransaction;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.HashUtil;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.facade.Repository;
import org.ethereum.vm.program.ProgramResult;
import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.SignatureException;
import java.security.interfaces.ECKey;
import java.util.concurrent.Future;
public class AccountWriteTest {
private static final Ethereum oEthereum = EthereumFactory.createEthereum();
public static void main(String[] args) throws SignatureException, InterruptedException {
//////////////////////////////////////////////////////////////////////////////////
// ContractAddress & initialization
// The contract code is here: https://gist.github.com/skydogch/0aa2c96ae5d4dbfcde0d#file-accounttestcontract-sol-L15-L20
// The genesis file is here: https://gist.github.com/skydogch/4e5bd0c891c78aee7a6c
//////////////////////////////////////////////////////////////////////////////////
// String oContractAddress = "a7b570217c7c73ca1b338cfb521afa728cbf0f17";
String oContractAddress = "4d2430de51342288ce2042263f18198056dc24ac";
//String oContractAddressString = "a7b570217c7c73ca1b338cfb521afa728cbf0f17";
//byte[] oContractAddress = Hex.decode(oContractAddressString);
BigInteger oValue = new BigInteger("1000000000000000000000");
byte[] oPrivKey = HashUtil.sha3("cow".getBytes());
byte[] oGasPrice = Hex.decode("09184e72a000");
byte[] oGas = Hex.decode("4255");
// ABIs for GetBalance and GetAccountName - both constants
String oSetBalanceABI = "{'constant':false,'inputs':[{'name':'i_Balance','type':'int256'}],'name':'setBalance','outputs':[],'type':'function'}";
String oSetAccountNameABI = "{'constant':false,'inputs':[{'name':'i_AccountName','type':'string'}],'name':'setAccountName','outputs':[],'type':'function'}";
{ oSetAccountNameABI = oSetAccountNameABI.replaceAll("'", "\""); };
{ oSetBalanceABI = oSetBalanceABI.replaceAll("'", "\""); };
// Call Transaction - Constant Return Function
CallTransaction.Function oCallTransactionFunctionSetBalance = CallTransaction.Function.fromJsonInterface(oSetBalanceABI);
//CallTransaction.Function oCallTransactionFunctionSetAccountName = CallTransaction.Function.fromJsonInterface(oSetAccountNameABI);
Thread.sleep(3000);
Repository repository = oEthereum.getRepository();
// BigInteger nonce = repository.getNonce("cd2a3d9f938e13cd947ec05abc7fe734df8dd826".getBytes());
/*
Transaction txSetAccountName =
// CallTransaction.createCallTransaction(nonce.longValue(), 1_000_000_000, 1_000_000_000,
CallTransaction.createCallTransaction(1, 1_000_000_000L, 1_000_000_000,
oContractAddress, 10, oCallTransactionFunctionSetAccountName, "SkydogA1_ethereumj_2015_11_19_001"); // "bf84b8c1f366620237a9d1e904e349228669a775", 10, oContractFuncSetName, "MSkydogA1_ethereumj");
txSetAccountName.sign(oPrivKey);
int newNonce = nonce + 1;
*/
int newNonce = 0;
Transaction txSetBalance =
CallTransaction.createCallTransaction(newNonce, 70_000_000_000L, 1_000_000_000,
// CallTransaction.createCallTransaction(1, 1_000_000_000L, 1_000_000_000,
oContractAddress, 10, oCallTransactionFunctionSetBalance, 2078); // "bf84b8c1f366620237a9d1e904e349228669a775", 10, oContractFuncSetName, "MSkydogA1_ethereumj");
txSetBalance.sign(oPrivKey);
/////////////////////////////////////////////////////////////////////////////////////
// Ethereumj add listener
/////////////////////////////////////////////////////////////////////////////////////
FollowFSAccount oFollowFSAccount = new FollowFSAccount(oEthereum);
oFollowFSAccount.setTransaction(txSetBalance);
oEthereum.addListener(oFollowFSAccount);
/////////////////////////////////////////////////////////////////////////////////////
// Execute Ethereumj transaction -> send to the blockchain
/////////////////////////////////////////////////////////////////////////////////////
// Future<Transaction> oFutureTxSetAccountName = oEthereum.submitTransaction(txSetAccountName);
Future<Transaction> oFutureTxSetBalance = oEthereum.submitTransaction(txSetBalance);
System.out.println("END******************************************************************");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment