add transaction test
This commit is contained in:
parent
1f175b73c1
commit
38b6fa986a
Binary file not shown.
|
@ -25,7 +25,7 @@
|
|||
</value>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
|
|
@ -28,4 +28,5 @@ dependencies {
|
|||
androidTestImplementation 'com.android.support.test:runner:1.0.1'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
|
||||
compile ('org.web3j:core:3.3.1-android')
|
||||
compile ('com.j256.ormlite:ormlite-android:4.48')
|
||||
}
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
package com.example.monnthic.monnthicmobile;
|
||||
|
||||
import org.web3j.crypto.RawTransaction;
|
||||
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.core.methods.response.TransactionReceipt;
|
||||
import org.web3j.protocol.http.HttpService;
|
||||
import org.web3j.protocol.core.DefaultBlockParameterName;
|
||||
import org.web3j.utils.Convert;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
|
||||
|
@ -18,9 +22,11 @@ import java.math.BigInteger;
|
|||
*/
|
||||
|
||||
public class AccessBlockchain {
|
||||
private static final String node3 = "http://5.51.59.70:1403";
|
||||
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_test = "0x5421c79d465a288c28e10aa43f9b7dff1b313c8e";
|
||||
private static String address_node3 = "0x869abc2dadd7e23c8b38f054276813a67d8131a7";
|
||||
//Web3j web3jNode2 = Web3jFactory.build(new HttpService(node2));
|
||||
|
||||
public static void main(String [ ] args)
|
||||
|
@ -46,12 +52,26 @@ public class AccessBlockchain {
|
|||
System.out.println("Noded 2 ethAccounts : "+web3jNode2.ethAccounts().send().getAccounts());
|
||||
EthGetBalance ethGetBalance2 = web3jNode2.ethGetBalance("0xef816528949bda3b87e19b86848fb28767156232", DefaultBlockParameterName.LATEST).sendAsync().get();
|
||||
|
||||
System.out.println("Node 1 ethMining : "+web3jNode1.ethMining().send().getResult());
|
||||
|
||||
System.out.println("Node 1 sync : "+web3jNode1.ethSyncing().send().getResult().toString());
|
||||
|
||||
|
||||
BigInteger wei1 = ethGetBalance1.getBalance();
|
||||
System.out.println("Node 1 balance is : "+wei1);
|
||||
|
||||
BigInteger wei2 = ethGetBalance2.getBalance();
|
||||
System.out.println("Node 2 balance is : "+wei2);
|
||||
|
||||
System.out.println("ethBlockNumber Node 1 : "+web3jNode1.ethBlockNumber().send().getResult());
|
||||
System.out.println("ethGetWork Node 1 : "+web3jNode1.ethGetWork().send().getResult());
|
||||
|
||||
|
||||
Web3j web3jNode3 = Web3jFactory.build(new HttpService(node3));
|
||||
System.out.println("Node 3 : ethAccounts : "+web3jNode3.ethAccounts().send().getAccounts());
|
||||
|
||||
EthGetBalance ethGetBalance_n3 = web3jNode1.ethGetBalance(address_node3, DefaultBlockParameterName.LATEST).sendAsync().get();
|
||||
System.out.println("TEST node 1 wallet 3 : "+ethGetBalance_n3);
|
||||
|
||||
//BigInteger test = getTheBalanceOfWallet(address);
|
||||
//System.out.println(test);
|
||||
|
@ -98,25 +118,44 @@ public class AccessBlockchain {
|
|||
return null;
|
||||
}
|
||||
|
||||
//Send transaction demo function
|
||||
public boolean sendTransationTo(String addressSource, String addressDestination, BigInteger amountToSend){
|
||||
Web3j web3jNode1 = Web3jFactory.build(new HttpService(node1));
|
||||
//Send DbTransaction demo function
|
||||
public static boolean sendTransationTo(String addressSource, String addressDestination, BigInteger amountToSend){
|
||||
Web3j n1 = Web3jFactory.build(new HttpService(node1));
|
||||
try{
|
||||
EthGetTransactionCount transactionCount = web3jNode1.ethGetTransactionCount(addressSource, DefaultBlockParameterName.LATEST).sendAsync().get();
|
||||
String coinbase = web3jNode1.ethCoinbase().sendAsync().get().getResult();
|
||||
EthGetTransactionCount transactionCount = n1.ethGetTransactionCount(addressSource, DefaultBlockParameterName.LATEST).sendAsync().get();
|
||||
String coinbase = n1.ethCoinbase().sendAsync().get().getResult();
|
||||
BigInteger nonce = transactionCount.getTransactionCount();
|
||||
BigInteger gasPrice = web3jNode1.ethGasPrice().send().getGasPrice();
|
||||
BigInteger gasPrice = n1.ethGasPrice().send().getGasPrice();
|
||||
BigInteger gasLimit = gasPrice.multiply(BigInteger.valueOf(2));
|
||||
|
||||
Transaction transaction = Transaction.createEtherTransaction(coinbase, nonce, gasPrice, gasLimit, addressDestination, amountToSend);
|
||||
BigInteger amountEther = Convert.toWei(amountToSend.toString(), Convert.Unit.ETHER).toBigInteger();
|
||||
Transaction transaction = Transaction.createEtherTransaction(coinbase, nonce, gasPrice, gasLimit, addressDestination, amountEther);
|
||||
String txUnit = "ETHER";
|
||||
|
||||
System.out.println("source : "+coinbase+"\nnonce : "+nonce+"\ngasPrice : "+gasPrice);
|
||||
System.out.println("gasLimit : "+gasLimit+"\ndestination : "+addressDestination+"\namount : "+amountEther);
|
||||
|
||||
/*
|
||||
Send the transaction
|
||||
*/
|
||||
//EthSendTransaction ethSendTransaction = n1.ethSendTransaction(transaction).sendAsync().get();
|
||||
//String txHash = ethSendTransaction.getTransactionHash();
|
||||
//TransactionReceipt txR = n1.ethGetTransactionReceipt(txHash).send().getResult();
|
||||
//txR.getBlockNumber();
|
||||
//insertTransaction(addressSource, addressDestination, amountToSend, txHash, txR.getBlockNumber().toString(), txUnit);
|
||||
|
||||
return true;
|
||||
|
||||
EthSendTransaction ethSendTransaction = web3jNode1.ethSendTransaction(transaction).sendAsync().get();
|
||||
|
||||
String txHash = ethSendTransaction.getTransactionHash();
|
||||
}catch (Exception e){
|
||||
e.getMessage();
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void insertTransaction(String adS, String adD, BigInteger amTs, String txH, String blN, String txUnit){
|
||||
|
||||
DbTransaction dbTrs = new DbTransaction();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package com.example.monnthic.monnthicmobile;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.util.Log;
|
||||
|
||||
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
|
||||
public class DatabaseManager extends OrmLiteSqliteOpenHelper{
|
||||
private static final String DATABASE_NAME = "WalletTransaction.db";
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
|
||||
public DatabaseManager(Context context){
|
||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
|
||||
try{
|
||||
TableUtils.clearTable(connectionSource, DbTransaction.class);
|
||||
Log.i("DATABASE","onCreate invoked");
|
||||
}catch(Exception exception){
|
||||
Log.e("DATABASE","Can't create Database", exception);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
|
||||
try{
|
||||
TableUtils.dropTable(connectionSource, DbTransaction.class, true);
|
||||
onCreate(database, connectionSource);
|
||||
Log.i("DATABASE","onUpdate invoked");
|
||||
}catch(Exception exception){
|
||||
Log.e("DATABASE","Can't update Database", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void insertTransaction(DbTransaction tx){
|
||||
try{
|
||||
Dao<DbTransaction, Integer> dao = getDao(DbTransaction.class);
|
||||
dao.create(tx);
|
||||
} catch(Exception exception){
|
||||
Log.e("DATABASE","Can't insert DbTransaction into Database", exception);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
package com.example.monnthic.monnthicmobile;
|
||||
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Date;
|
||||
|
||||
@DatabaseTable(tableName = "T_TRANSACTION")
|
||||
public class DbTransaction {
|
||||
@DatabaseField(columnName = "transactionId", generatedId = true, unique = true)
|
||||
private int id;
|
||||
@DatabaseField(columnName = "transactionDate")
|
||||
private Date transactionDate;
|
||||
@DatabaseField(columnName = "transactionDSrcAddress")
|
||||
private String sourceAddress;
|
||||
@DatabaseField(columnName = "transactionDestAddress")
|
||||
private String destAddress;
|
||||
@DatabaseField(columnName = "transactionHash")
|
||||
private String transactionHash;
|
||||
@DatabaseField(columnName = "transactionBlockNumber")
|
||||
private String blockNumber;
|
||||
@DatabaseField(columnName = "transactionAmout")
|
||||
private BigInteger amount;
|
||||
@DatabaseField(columnName = "transactionUnit")
|
||||
private String unit;
|
||||
|
||||
public DbTransaction() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DbTransaction(Date transactionDate, String sourceAddress, String destAddress, String transactionHash, String blockNumber, BigInteger amount, String unit) {
|
||||
super();
|
||||
this.transactionDate = transactionDate;
|
||||
this.sourceAddress = sourceAddress;
|
||||
this.destAddress = destAddress;
|
||||
this.transactionHash = transactionHash;
|
||||
this.blockNumber = blockNumber;
|
||||
this.amount = amount;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public String getSourceAddress() {
|
||||
return sourceAddress;
|
||||
}
|
||||
|
||||
public void setSourceAddress(String sourceAddress) {
|
||||
this.sourceAddress = sourceAddress;
|
||||
}
|
||||
|
||||
public String getDestAddress() {
|
||||
return destAddress;
|
||||
}
|
||||
|
||||
public void setDestAddress(String destAddress) {
|
||||
this.destAddress = destAddress;
|
||||
}
|
||||
|
||||
public String getTransactionHash() {
|
||||
return transactionHash;
|
||||
}
|
||||
|
||||
public void setTransactionHash(String transactionHash) {
|
||||
this.transactionHash = transactionHash;
|
||||
}
|
||||
|
||||
public BigInteger getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(BigInteger amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Date getTransactionDate() {
|
||||
return transactionDate;
|
||||
}
|
||||
|
||||
public void setTransactionDate(Date transactionDate) {
|
||||
this.transactionDate = transactionDate;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public String getBlockNumber() {
|
||||
return blockNumber;
|
||||
}
|
||||
|
||||
public void setBlockNumber(String blockNumber) {
|
||||
this.blockNumber = blockNumber;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.example.monnthic.monnthicmobile;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
@ -7,20 +8,25 @@ import android.widget.Button;
|
|||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class TransactionActivity extends AppCompatActivity {
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class TransactionActivity extends AppCompatActivity {
|
||||
private String address;
|
||||
private EditText addressDestination;
|
||||
private EditText Amout;
|
||||
private EditText amount;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_transaction);
|
||||
addressDestination = findViewById(R.id.destination_address);
|
||||
Amout = findViewById(R.id.amount);
|
||||
amount = findViewById(R.id.amount);
|
||||
Button buttonCancel = findViewById(R.id.btn_cancel);
|
||||
Button buttonOk = findViewById(R.id.btn_send);
|
||||
|
||||
Intent intent = getIntent();
|
||||
address = intent.getStringExtra("WALLET_ADDRESS");
|
||||
|
||||
buttonCancel.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
|
@ -37,7 +43,13 @@ public class TransactionActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
public void sendTransaction(){
|
||||
Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
|
||||
String adD = addressDestination.getText().toString();
|
||||
BigInteger amt = new BigInteger(amount.getText().toString());
|
||||
|
||||
if(AccessBlockchain.sendTransationTo(address, adD, amt)){
|
||||
Toast.makeText(this, "OK.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
//Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import android.widget.Button;
|
|||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.web3j.utils.Convert;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class WalletPresenterActivity extends AppCompatActivity {
|
||||
|
@ -31,7 +33,7 @@ public class WalletPresenterActivity extends AppCompatActivity {
|
|||
if(initiateBalance(address)==null){
|
||||
balance.setText("Error");
|
||||
}else{
|
||||
balance.setText(initiateBalance(address).toString());
|
||||
balance.setText(Convert.fromWei(initiateBalance(address).toString(), Convert.Unit.ETHER).toString());
|
||||
}
|
||||
|
||||
buttonSendTransaction.setOnClickListener(new View.OnClickListener() {
|
||||
|
@ -51,13 +53,14 @@ public class WalletPresenterActivity extends AppCompatActivity {
|
|||
|
||||
public void launchTransactionActivity(){
|
||||
Intent transactionIntent = new Intent(WalletPresenterActivity.this, TransactionActivity.class);
|
||||
transactionIntent.putExtra("WALLET_ADDRESS",address);
|
||||
WalletPresenterActivity.this.startActivity(transactionIntent);
|
||||
}
|
||||
|
||||
public BigInteger initiateBalance(String address){
|
||||
public String initiateBalance(String address){
|
||||
AccessBlockchain ab = new AccessBlockchain();
|
||||
System.out.println("addresse "+ address);
|
||||
BigInteger balanceIs = ab.getTheBalanceOfWallet(address);
|
||||
String balanceIs = ab.getTheBalanceOfWallet(address).toString();
|
||||
return balanceIs;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue