From cba73cfa0d270b8169ad92b549b3bf0932d0ae5b Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 18 Oct 2017 22:46:09 -0400 Subject: [PATCH] - Created Bitshares Asset Info - Created Btishares Asset Info DAO - Finish the balance petition --- .../apigenerator/GrapheneApiGenerator.java | 60 ++++++++++++++++--- .../crystalwallet/dao/BitsharesAssetDao.java | 30 ++++++++++ .../crystalwallet/dao/CrystalDatabase.java | 4 +- .../crystalwallet/models/BitsharesAsset.java | 12 ++++ .../models/BitsharesAssetInfo.java | 60 +++++++++++++++++++ 5 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index a3e967d..54cdfa5 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -8,10 +8,13 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import cy.agorise.crystalwallet.dao.BitsharesAssetDao; import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao; +import cy.agorise.crystalwallet.dao.CryptoCurrencyDao; import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.manager.BitsharesAccountManager; import cy.agorise.crystalwallet.models.BitsharesAsset; +import cy.agorise.crystalwallet.models.BitsharesAssetInfo; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoNetBalance; import cy.agorise.crystalwallet.network.WebSocketThread; @@ -300,8 +303,9 @@ public abstract class GrapheneApiGenerator { public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId, final Context context){ CrystalDatabase db = CrystalDatabase.getAppDatabase(context); - final LiveData> balances = db.cryptoCoinBalanceDao().getBalancesFromAccount(accountId); final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao(); + final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao(); + final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao(); SubscriptionListener balanceListener = new SubscriptionListener() { @Override public ObjectType getInterestObjectType() { @@ -316,15 +320,33 @@ public abstract class GrapheneApiGenerator { if(update instanceof AccountBalanceUpdate){ AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update; if(balanceUpdate.owner.equals(accountBitsharesId)){ - boolean find = false; - for(CryptoCoinBalance balance : balances.getValue()){ + CryptoCoinBalance balance = new CryptoCoinBalance(); + balance.setAccountId(accountId); + balance.setBalance(((AccountBalanceUpdate) update).balance); + /*LiveData assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type); + if(assetInfo == null || assetInfo.getValue() == null){ + BitsharesAsset.Type assetType; + Asset asset = balance.getAsset(); + if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){ + assetType = BitsharesAsset.Type.CORE; + }else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){ + assetType = BitsharesAsset.Type.SMART_COIN; + }else if(asset.getAssetType().equals(Asset.AssetType.UIA)){ + assetType = BitsharesAsset.Type.UIA; + }else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){ + assetType = BitsharesAsset.Type.PREDICTION_MARKET; + } + BitsharesAsset newAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType); + BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset); + cryptoCurrencyDao.insertCryptoCurrency(newAsset); + bitsharesAssetDao.insertBitsharesAssetInfo(info); + assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); } - if(!find){ - CryptoCoinBalance balance = new CryptoCoinBalance(); - balanceDao.insertCryptoCoinBalance(balance); - } + balance.setCryptoCurrency(assetInfo.getValue().getCryptoCurrencyId()); + + balanceDao.insertCryptoCoinBalance(balance);*/ //TODO balance function BitsharesAccountManager.refreshAccountTransactions(accountId,context); } @@ -351,6 +373,8 @@ public abstract class GrapheneApiGenerator { public static void getAccountBalance(final long accountId, final String accountGrapheneId, final Context context){ CrystalDatabase db = CrystalDatabase.getAppDatabase(context); final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao(); + final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao(); + final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao(); WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), null, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { @@ -359,10 +383,28 @@ public abstract class GrapheneApiGenerator { CryptoCoinBalance ccBalance = new CryptoCoinBalance(); ccBalance.setAccountId(accountId); ccBalance.setBalance(balance.getAmount().longValue()); - //TODO find asset - //ccBalance.setCryptoCurrency(); + LiveData assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); + if(assetInfo == null || assetInfo.getValue() == null){ + BitsharesAsset.Type assetType = null; + Asset asset = balance.getAsset(); + if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){ + assetType = BitsharesAsset.Type.CORE; + }else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){ + assetType = BitsharesAsset.Type.SMART_COIN; + }else if(asset.getAssetType().equals(Asset.AssetType.UIA)){ + assetType = BitsharesAsset.Type.UIA; + }else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){ + assetType = BitsharesAsset.Type.PREDICTION_MARKET; + } + BitsharesAsset newAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType); + BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset); + cryptoCurrencyDao.insertCryptoCurrency(newAsset); + bitsharesAssetDao.insertBitsharesAssetInfo(info); + assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); + } + ccBalance.setCryptoCurrency(assetInfo.getValue().getCryptoCurrencyId()); balanceDao.insertCryptoCoinBalance(ccBalance); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java new file mode 100644 index 0000000..1b6e2f0 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java @@ -0,0 +1,30 @@ +package cy.agorise.crystalwallet.dao; + +import android.arch.lifecycle.LiveData; +import android.arch.persistence.room.Dao; +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.BitsharesAssetInfo; + +/** + * Created by henry on 18/10/2017. + */ +@Dao +public interface BitsharesAssetDao { + + @Query("SELECT * FROM bitshares_asset") + LiveData> getAll(); + + @Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId") + LiveData getBitsharesAssetInfo(long cryptoCurrencyId); + + @Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId") + LiveData getBitsharesAssetInfoById(String bitsharesId); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts); +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java index 13a00c9..9918549 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java @@ -8,6 +8,7 @@ import android.content.Context; import cy.agorise.crystalwallet.dao.converters.Converters; import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.BitsharesAssetInfo; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrency; @@ -19,7 +20,7 @@ import cy.agorise.crystalwallet.models.GrapheneAccountInfo; * Created by Henry Varona on 4/9/2017. */ -@Database(entities = {AccountSeed.class, CryptoNetAccount.class, CryptoCoinTransaction.class, CryptoCurrency.class, CryptoCoinBalance.class, GrapheneAccountInfo.class}, version = 2) +@Database(entities = {AccountSeed.class, CryptoNetAccount.class, CryptoCoinTransaction.class, CryptoCurrency.class, CryptoCoinBalance.class, GrapheneAccountInfo.class, BitsharesAssetInfo.class}, version = 2) @TypeConverters({Converters.class}) public abstract class CrystalDatabase extends RoomDatabase { @@ -31,6 +32,7 @@ public abstract class CrystalDatabase extends RoomDatabase { public abstract TransactionDao transactionDao(); public abstract CryptoCoinBalanceDao cryptoCoinBalanceDao(); public abstract CryptoCurrencyDao cryptoCurrencyDao(); + public abstract BitsharesAssetDao bitsharesAssetDao(); public static CrystalDatabase getAppDatabase(Context context) { if (instance == null) { diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAsset.java b/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAsset.java index fbff981..493f1a3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAsset.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAsset.java @@ -45,6 +45,18 @@ public class BitsharesAsset extends CryptoCurrency { this.setPrecision(precision); } + public BitsharesAsset(CryptoCurrency currency){ + this.setId(currency.getId()); + this.setPrecision(currency.getPrecision()); + this.setCryptoNet(currency.getCryptoNet()); + this.setName(currency.getName()); + } + + public void loadInfo(BitsharesAssetInfo info){ + this.bitsharesId = info.getBitsharesId(); + this.assetType = info.getAssetType(); + } + public String getBitsharesId() { return bitsharesId; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java b/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java new file mode 100644 index 0000000..0b40dd5 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java @@ -0,0 +1,60 @@ +package cy.agorise.crystalwallet.models; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.ForeignKey; + +/** + * This represents the extens attributes of the Bitshares Assets, to be saved in the database + * Created by henry on 8/10/2017. + */ +@Entity(tableName = "bitshares_asset", + primaryKeys = {"crypto_curreny_id"}, + foreignKeys = @ForeignKey(entity = CryptoCurrency.class, + parentColumns = "id", + childColumns = "crypto_curreny_id")) +public class BitsharesAssetInfo { + //The crypto Currency representing this bitshares asset + @ColumnInfo(name = "crypto_curreny_id") + private long cryptoCurrencyId; + // The bitshares internal id + @ColumnInfo(name = "bitshares_id") + private String bitsharesId; + // The bitshares type see the enum below + @ColumnInfo(name = "asset_type") + private BitsharesAsset.Type assetType; + + public BitsharesAssetInfo(String symbol, int precision, String bitsharesId, BitsharesAsset.Type assetType) { + this.bitsharesId = bitsharesId; + this.assetType = assetType; + } + + public BitsharesAssetInfo(BitsharesAsset asset){ + this.bitsharesId = asset.getBitsharesId(); + this.assetType = asset.getAssetType(); + } + + public String getBitsharesId() { + return bitsharesId; + } + + public void setBitsharesId(String bitsharesId) { + this.bitsharesId = bitsharesId; + } + + public BitsharesAsset.Type getAssetType() { + return assetType; + } + + public void setAssetType(BitsharesAsset.Type assetType) { + this.assetType = assetType; + } + + public long getCryptoCurrencyId() { + return cryptoCurrencyId; + } + + public void setCryptoCurrencyId(long cryptoCurrencyId) { + this.cryptoCurrencyId = cryptoCurrencyId; + } +}