Add transaction demo function

This commit is contained in:
GME 2018-06-10 16:24:42 +02:00
parent 72af832156
commit 212cf65521

View file

@ -3,6 +3,9 @@ package com.example.monnthic.monnthicmobile;
import org.web3j.protocol.Web3j; import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jFactory; import org.web3j.protocol.Web3jFactory;
import org.web3j.protocol.core.methods.response.EthGetBalance; import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService; import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.DefaultBlockParameterName;
import java.math.BigInteger; import java.math.BigInteger;
@ -17,12 +20,22 @@ import java.math.BigInteger;
public class AccessBlockchain { public class AccessBlockchain {
private static final String node2 = "http://93.30.148.59:1402"; private static final String node2 = "http://93.30.148.59:1402";
private static final String node1 = "http://93.30.148.59:1401"; private static final String node1 = "http://93.30.148.59:1401";
private static String address = "0x5421c79d465a288c28e10aa43f9b7dff1b313c8e"; private static String address_test = "0x5421c79d465a288c28e10aa43f9b7dff1b313c8e";
//Web3j web3jNode2 = Web3jFactory.build(new HttpService(node2)); //Web3j web3jNode2 = Web3jFactory.build(new HttpService(node2));
public static void main(String [ ] args) public static void main(String [ ] args)
{ {
try{ try{
/*
Web3j web3jNode1 = Web3jFactory.build(new HttpService(node1));
System.out.println(web3jNode1.ethCoinbase().sendAsync().get().getResult());
EthGetTransactionCount transactionCount = web3jNode1.ethGetTransactionCount(address_test, DefaultBlockParameterName.LATEST).sendAsync().get();
System.out.println(transactionCount);
BigInteger nonce = transactionCount.getTransactionCount();
System.out.println(nonce);
*/
//BigInteger test = getTheBalanceOfWallet(address); //BigInteger test = getTheBalanceOfWallet(address);
//System.out.println(test); //System.out.println(test);
@ -37,7 +50,10 @@ public class AccessBlockchain {
//System.out.println("wei is : "+wei); //System.out.println("wei is : "+wei);
//System.out.println("*************************************************"); //System.out.println("*************************************************");
//System.out.println("ethAccounts : "+web3jNode2.ethAccounts().send().getAccounts()); Web3j web3jNode1 = Web3jFactory.build(new HttpService(node1));
Web3j web3jNode2 = Web3jFactory.build(new HttpService(node2));
System.out.println("ethAccounts : "+web3jNode1.ethAccounts().send().getAccounts());
System.out.println("ethAccounts : "+web3jNode2.ethAccounts().send().getAccounts());
//System.out.println("ethGasPrice : "+web3jNode2.ethGasPrice().send().getGasPrice()); //System.out.println("ethGasPrice : "+web3jNode2.ethGasPrice().send().getGasPrice());
//System.out.println("ethGetWork : "+web3jNode2.ethGetWork().send().getResult()); //System.out.println("ethGetWork : "+web3jNode2.ethGetWork().send().getResult());
//System.out.println("ethBlockNumber : "+web3jNode2.ethBlockNumber().send().getResult()); //System.out.println("ethBlockNumber : "+web3jNode2.ethBlockNumber().send().getResult());
@ -64,7 +80,24 @@ public class AccessBlockchain {
return null; return null;
} }
public boolean sendTransationTo(String addressDestination, BigInteger amountToSend){ public boolean sendTransationTo(String addressSource, String addressDestination, BigInteger amountToSend){
Web3j web3jNode1 = Web3jFactory.build(new HttpService(node1));
try{
EthGetTransactionCount transactionCount = web3jNode1.ethGetTransactionCount(addressSource, DefaultBlockParameterName.LATEST).sendAsync().get();
String coinbase = web3jNode1.ethCoinbase().sendAsync().get().getResult();
BigInteger nonce = transactionCount.getTransactionCount();
BigInteger gasPrice = web3jNode1.ethGasPrice().send().getGasPrice();
BigInteger gasLimit = gasPrice.multiply(BigInteger.valueOf(2));
Transaction transaction = Transaction.createEtherTransaction(coinbase, nonce, gasPrice, gasLimit, addressDestination, amountToSend);
EthSendTransaction ethSendTransaction = web3jNode1.ethSendTransaction(transaction).sendAsync().get();
String txHash = ethSendTransaction.getTransactionHash();
}catch (Exception e){
e.getMessage();
e.printStackTrace();
}
return false; return false;
} }
} }