diff --git a/app/src/main/java/com/example/monnthic/monnthicmobile/AccessBlockchain.java b/app/src/main/java/com/example/monnthic/monnthicmobile/AccessBlockchain.java index dd129db..46eef3a 100644 --- a/app/src/main/java/com/example/monnthic/monnthicmobile/AccessBlockchain.java +++ b/app/src/main/java/com/example/monnthic/monnthicmobile/AccessBlockchain.java @@ -3,6 +3,9 @@ package com.example.monnthic.monnthicmobile; import org.web3j.protocol.Web3j; import org.web3j.protocol.Web3jFactory; 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.core.DefaultBlockParameterName; import java.math.BigInteger; @@ -17,12 +20,22 @@ import java.math.BigInteger; public class AccessBlockchain { private static final String node2 = "http://93.30.148.59:1402"; 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)); public static void main(String [ ] args) { 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); //System.out.println(test); @@ -37,7 +50,10 @@ public class AccessBlockchain { //System.out.println("wei is : "+wei); //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("ethGetWork : "+web3jNode2.ethGetWork().send().getResult()); //System.out.println("ethBlockNumber : "+web3jNode2.ethBlockNumber().send().getResult()); @@ -64,7 +80,24 @@ public class AccessBlockchain { 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; } }