From 356300fed4f613ce500293197e3626d1a789a13e Mon Sep 17 00:00:00 2001 From: hvarona Date: Wed, 17 Oct 2018 23:32:17 -0400 Subject: [PATCH] Change BitcoinTransactions to adapt to new architecture Change bitcoin like logic to adapt to new architecture --- .../CryptoCoinTransactionReceiptActivity.java | 6 +- .../dao/BitcoinTransactionDao.java | 8 +- .../crystalwallet/dao/TransactionDao.java | 5 +- .../manager/GeneralAccountManager.java | 108 +++++++++--------- .../models/BitcoinTransaction.java | 3 + .../models/BitcoinTransactionGTxIO.java | 36 ++++++ .../views/CryptoCoinBalanceViewHolder.java | 2 +- 7 files changed, 106 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/CryptoCoinTransactionReceiptActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/CryptoCoinTransactionReceiptActivity.java index f7c8c1f..e4f68d5 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/CryptoCoinTransactionReceiptActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/CryptoCoinTransactionReceiptActivity.java @@ -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() { @Override diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java index 5b62286..7244c81 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java @@ -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 getAll(); @Query("SELECT * FROM bitcoin_transaction bt WHERE bt.tx_id = :txid") - BitcoinTransaction getByTxid(String txid); + List getTransactionsByTxid(String txid); @Insert(onConflict = OnConflictStrategy.REPLACE) public long[] insertBitcoinTransaction(BitcoinTransaction... transactions); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertBitcoinTransactionGTxIO(BitcoinTransactionGTxIO... transactiongtxios); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java index 3ba5a9e..7e66ad9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -51,7 +51,10 @@ public interface TransactionDao { List getByIdAccount(long idAccount); @Query("SELECT * FROM crypto_coin_transaction WHERE id = :id") - LiveData getById(long id); + LiveData 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); diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java index 8beb806..7e2d753 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -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 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 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 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); diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransaction.java b/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransaction.java index e7ed29b..3ba5f08 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransaction.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransaction.java @@ -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; diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransactionGTxIO.java b/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransactionGTxIO.java index 8febad2..105f8de 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransactionGTxIO.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinTransactionGTxIO.java @@ -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; + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java index 916e6c1..f6d3b3d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java @@ -71,7 +71,7 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { //Retrieves the preferred currency selected by the user final LiveData 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 currencyFromLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(balance.getCryptoCurrencyId());