Change BitcoinTransactions to adapt to new architecture

Change bitcoin like logic to adapt to new architecture
This commit is contained in:
hvarona 2018-10-17 23:32:17 -04:00
parent 7681523252
commit 356300fed4
7 changed files with 106 additions and 62 deletions

View file

@ -2,15 +2,11 @@ package cy.agorise.crystalwallet.activities;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.text.SimpleDateFormat;
import butterknife.BindView;
@ -47,7 +43,7 @@ public class CryptoCoinTransactionReceiptActivity extends AppCompatActivity {
if (this.cryptoCoinTransactionId != -1) {
db = CrystalDatabase.getAppDatabase(this);
this.cryptoCoinTransactionLiveData = db.transactionDao().getById(this.cryptoCoinTransactionId);
this.cryptoCoinTransactionLiveData = db.transactionDao().getByIdLiveData(this.cryptoCoinTransactionId);
this.cryptoCoinTransactionLiveData.observe(this, new Observer<CryptoCoinTransaction>() {
@Override

View file

@ -6,8 +6,11 @@ import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import java.util.List;
import cy.agorise.crystalwallet.models.BitcoinTransaction;
import cy.agorise.crystalwallet.models.BitcoinTransactionExtended;
import cy.agorise.crystalwallet.models.BitcoinTransactionGTxIO;
/**
* Created by Henry Varona on 10/02/2018.
@ -19,8 +22,11 @@ public interface BitcoinTransactionDao {
LiveData<BitcoinTransactionExtended> getAll();
@Query("SELECT * FROM bitcoin_transaction bt WHERE bt.tx_id = :txid")
BitcoinTransaction getByTxid(String txid);
List<BitcoinTransaction> getTransactionsByTxid(String txid);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitcoinTransaction(BitcoinTransaction... transactions);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitcoinTransactionGTxIO(BitcoinTransactionGTxIO... transactiongtxios);
}

View file

@ -51,7 +51,10 @@ public interface TransactionDao {
List<CryptoCoinTransaction> getByIdAccount(long idAccount);
@Query("SELECT * FROM crypto_coin_transaction WHERE id = :id")
LiveData<CryptoCoinTransaction> getById(long id);
LiveData<CryptoCoinTransaction> getByIdLiveData(long id);
@Query("SELECT * FROM crypto_coin_transaction WHERE id = :id")
CryptoCoinTransaction getById(long id);
@Query("SELECT * FROM crypto_coin_transaction WHERE date = :date and 'from' = :from and 'to' = :to and amount = :amount ")
CryptoCoinTransaction getByTransaction(Date date, String from, String to, long amount);

View file

@ -20,19 +20,17 @@ import java.util.List;
import cy.agorise.crystalwallet.apigenerator.ApiRequest;
import cy.agorise.crystalwallet.apigenerator.ApiRequestListener;
import cy.agorise.crystalwallet.apigenerator.InsightApiGenerator;
import cy.agorise.crystalwallet.apigenerator.insightapi.BroadcastTransaction;
import cy.agorise.crystalwallet.apigenerator.insightapi.GetTransactionData;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Txi;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Vin;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Vout;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.enums.CryptoCoin;
import cy.agorise.crystalwallet.models.BitcoinTransaction;
import cy.agorise.crystalwallet.models.BitcoinTransactionGTxIO;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
import cy.agorise.crystalwallet.models.GTxIO;
import cy.agorise.crystalwallet.models.GeneralCoinAccount;
import cy.agorise.crystalwallet.models.GeneralCoinAddress;
import cy.agorise.crystalwallet.models.GeneralTransaction;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequestsListener;
import cy.agorise.crystalwallet.requestmanagers.GeneralAccountSendRequest;
@ -96,69 +94,69 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf
*/
public void processTxi(Txi txi){
CrystalDatabase db = CrystalDatabase.getAppDatabase(this.context);
BitcoinTransaction btTransaction = db.bitcoinTransactionDao().getByTxid(txi.txid);
if(btTransaction != null){
btTransaction.setConfirmations(txi.confirmations);
db.bitcoinTransactionDao().insertBitcoinTransaction(btTransaction);
List<BitcoinTransaction> btTransactions = db.bitcoinTransactionDao().getTransactionsByTxid(txi.txid);
if(!btTransactions.isEmpty()){
for(BitcoinTransaction btTransaction : btTransactions) {
btTransaction.setConfirmations(txi.confirmations);
CryptoCoinTransaction ccTransaction = db.transactionDao().getById(btTransaction.getCryptoCoinTransactionId());
if (!ccTransaction.isConfirmed() && btTransaction.getConfirmations() >= cryptoCoin.getCryptoNet().getConfirmationsNeeded()) {
ccTransaction.setConfirmed(true);
db.transactionDao().insertTransaction(ccTransaction);
}
db.bitcoinTransactionDao().insertBitcoinTransaction(btTransaction);
}
}else {
/*List<CryptoCoinTransaction> ccTransactions = new ArrayList();
btTransactions = new ArrayList();*/ //TODO transactions involving multiples accounts
CryptoCoinTransaction ccTransaction = new CryptoCoinTransaction();
BitcoinTransaction btTransaction = new BitcoinTransaction();
btTransaction.setTxId(txi.txid);
btTransaction.setBlock(txi.blockheight);
btTransaction.setFee((long) (txi.fee * Math.pow(10, cryptoCoin.getPrecision())));
btTransaction.setConfirmations(txi.confirmations);
ccTransaction.setDate(new Date(txi.time * 1000));
if(txi.txlock || txi.confirmations >= cryptoCoin.getCryptoNet().getConfirmationsNeeded()) {
ccTransaction.setConfirmed(true);
}else{
ccTransaction.setConfirmed(false);
}
ccTransaction.setInput(false);
long amount = 0;
GeneralTransaction transaction = new GeneralTransaction();
//transaction.setAccount(this.mAccount);
transaction.setTxid(txi.txid);
transaction.setBlock(txi.blockheight);
transaction.setDate(new Date(txi.time * 1000));
transaction.setFee((long) (txi.fee * Math.pow(10, cryptoCoin.getPrecision())));
transaction.setConfirm(txi.confirmations);
transaction.setType(cryptoCoin);
transaction.setBlockHeight(txi.blockheight);
//transaction.setType(cryptoCoin);
List<BitcoinTransactionGTxIO> gtxios = new ArrayList();
for (Vin vin : txi.vin) {
GTxIO input = new GTxIO();
input.setAmount((long) (vin.value * Math.pow(10, cryptoCoin.getPrecision())));
input.setTransaction(transaction);
input.setOut(true);
input.setType(cryptoCoin);
BitcoinTransactionGTxIO input = new BitcoinTransactionGTxIO();
String addr = vin.addr;
input.setAddressString(addr);
input.setAddress(addr);
input.setIndex(vin.n);
input.setOutput(true);
input.setAmount((long) (vin.value * Math.pow(10, cryptoCoin.getPrecision())));
input.setOriginalTxId(vin.txid);
input.setScriptHex(vin.scriptSig.hex);
input.setOriginalTxid(vin.txid);
/*for (GeneralCoinAddress address : this.mAddresses) {
if (address.getAddressString(this.mAccount.getNetworkParam()).equals(addr)) {
input.setAddress(address);
tempAccount = address.getAccount();
if (!address.hasTransactionOutput(input, this.mAccount.getNetworkParam())) {
address.getTransactionOutput().add(input);
}
}
}*/
transaction.getTxInputs().add(input);
gtxios.add(input);
}
for (Vout vout : txi.vout) {
if (vout.scriptPubKey.addresses == null || vout.scriptPubKey.addresses.length <= 0) {
// The address is null, this must be a memo
String hex = vout.scriptPubKey.hex;
int opReturnIndex = hex.indexOf("6a");
if (opReturnIndex >= 0) {
byte[] memoBytes = new byte[Integer.parseInt(hex.substring(opReturnIndex + 2, opReturnIndex + 4), 16)];
for (int i = 0; i < memoBytes.length; i++) {
memoBytes[i] = Byte.parseByte(hex.substring(opReturnIndex + 4 + (i * 2), opReturnIndex + 6 + (i * 2)), 16);
}
transaction.setMemo(new String(memoBytes));
}
} else {
GTxIO output = new GTxIO();
output.setAmount((long) (vout.value * Math.pow(10, cryptoCoin.getPrecision())));
output.setTransaction(transaction);
output.setOut(false);
output.setType(cryptoCoin);
BitcoinTransactionGTxIO output = new BitcoinTransactionGTxIO();
String addr = vout.scriptPubKey.addresses[0];
output.setAddressString(addr);
output.setAddress(addr);
output.setIndex(vout.n);
output.setOutput(false);
output.setAmount((long) (vout.value * Math.pow(10, cryptoCoin.getPrecision())));
output.setScriptHex(vout.scriptPubKey.hex);
gtxios.add(output);
/*for (GeneralCoinAddress address : this.mAddresses) {
if (address.getAddressString(this.mAccount.getNetworkParam()).equals(addr)) {
output.setAddress(address);
@ -170,12 +168,14 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf
changed = true;
}
}*/
transaction.getTxOutputs().add(output);
}
}
if (txi.txlock && txi.confirmations < cryptoCoin.getCryptoNet().getConfirmationsNeeded()) {
transaction.setConfirm(cryptoCoin.getCryptoNet().getConfirmationsNeeded());
long ccId = db.transactionDao().insertTransaction(ccTransaction)[0];
btTransaction.setCryptoCoinTransactionId(ccId);
long btId = db.bitcoinTransactionDao().insertBitcoinTransaction(btTransaction)[0];
for(BitcoinTransactionGTxIO gtxio : gtxios){
gtxio.setBitcoinTransactionId(btId);
}
//TODO database
/*SCWallDatabase db = new SCWallDatabase(this.mContext);

View file

@ -60,6 +60,9 @@ public class BitcoinTransaction {
@ColumnInfo(name="confirmations")
protected int confirmations;
public BitcoinTransaction() {
}
public BitcoinTransaction(long cryptoCoinTransactionId, String txId, long block, long fee, int confirmations) {
this.cryptoCoinTransactionId = cryptoCoinTransactionId;
this.txId = txId;

View file

@ -47,6 +47,18 @@ public class BitcoinTransactionGTxIO {
@ColumnInfo(name="is_output")
protected boolean isOutput;
@ColumnInfo(name="amount")
protected long amount;
@ColumnInfo(name="script_hex")
protected String scriptHex;
@ColumnInfo(name="original_txid")
protected String originalTxId;
public BitcoinTransactionGTxIO() {
}
public BitcoinTransactionGTxIO(long bitcoinTransactionId, int index, String address, boolean isOutput) {
this.bitcoinTransactionId = bitcoinTransactionId;
this.index = index;
@ -85,4 +97,28 @@ public class BitcoinTransactionGTxIO {
public void setOutput(boolean output) {
isOutput = output;
}
public long getAmount() {
return amount;
}
public void setAmount(long amount) {
this.amount = amount;
}
public String getScriptHex() {
return scriptHex;
}
public void setScriptHex(String scriptHex) {
this.scriptHex = scriptHex;
}
public String getOriginalTxId() {
return originalTxId;
}
public void setOriginalTxId(String originalTxId) {
this.originalTxId = originalTxId;
}
}

View file

@ -71,7 +71,7 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
//Retrieves the preferred currency selected by the user
final LiveData<GeneralSetting> preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY);
//Retrieves the currency of this balance
//final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(balance.getCryptoCurrencyId());
//final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getByIdLiveData(balance.getCryptoCurrencyId());
LiveData<CryptoCurrency> currencyFromLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(balance.getCryptoCurrencyId());