- Created Bitshares Asset Info

- Created Btishares Asset Info DAO
- Finish the balance petition
This commit is contained in:
henry 2017-10-18 22:46:09 -04:00
parent 119f4ea12b
commit cba73cfa0d
5 changed files with 156 additions and 10 deletions

View file

@ -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<List<CryptoCoinBalance>> 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<BitsharesAssetInfo> 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<BitsharesAssetInfo> 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);
}
}

View file

@ -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<List<BitsharesAssetInfo>> getAll();
@Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId")
LiveData<BitsharesAssetInfo> getBitsharesAssetInfo(long cryptoCurrencyId);
@Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId")
LiveData<BitsharesAssetInfo> getBitsharesAssetInfoById(String bitsharesId);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts);
}

View file

@ -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) {

View file

@ -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;
}

View file

@ -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;
}
}