Merge branch 'develop' into 'master'
Develop to Master - add transaction demo function See merge request monnethic/app-mobile!1
This commit is contained in:
commit
68d847eaee
|
@ -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,24 @@ 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{
|
||||
//USED FOR TEST
|
||||
|
||||
/*
|
||||
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,6 +52,9 @@ public class AccessBlockchain {
|
|||
//System.out.println("wei is : "+wei);
|
||||
|
||||
//System.out.println("*************************************************");
|
||||
//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());
|
||||
|
@ -51,6 +69,7 @@ public class AccessBlockchain {
|
|||
}
|
||||
|
||||
|
||||
//Get Balance function
|
||||
public BigInteger getTheBalanceOfWallet(String walletAddress){
|
||||
Web3j web3jNode1 = Web3jFactory.build(new HttpService(node1));
|
||||
try{
|
||||
|
@ -64,7 +83,25 @@ public class AccessBlockchain {
|
|||
return null;
|
||||
}
|
||||
|
||||
public boolean sendTransationTo(String addressDestination, BigInteger amountToSend){
|
||||
//Send transaction demo function
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue