# Conflicts:
#	app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java
This commit is contained in:
Javier Varona 2017-10-25 21:35:14 -04:00
commit c2f627694e
6 changed files with 175 additions and 80 deletions

View file

@ -1,6 +1,5 @@
package cy.agorise.crystalwallet.apigenerator; package cy.agorise.crystalwallet.apigenerator;
import android.app.Application;
import android.arch.lifecycle.LiveData; import android.arch.lifecycle.LiveData;
import android.content.Context; import android.content.Context;
@ -17,7 +16,7 @@ import cy.agorise.crystalwallet.manager.BitsharesAccountManager;
import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo; import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinBalance;
import cy.agorise.crystalwallet.models.CryptoNetBalance; import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.network.WebSocketThread; import cy.agorise.crystalwallet.network.WebSocketThread;
import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Address;
import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.Asset;
@ -28,6 +27,7 @@ import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.api.GetAccountBalances; import cy.agorise.graphenej.api.GetAccountBalances;
import cy.agorise.graphenej.api.GetAccountByName; import cy.agorise.graphenej.api.GetAccountByName;
import cy.agorise.graphenej.api.GetAccounts; import cy.agorise.graphenej.api.GetAccounts;
import cy.agorise.graphenej.api.GetBlockHeader;
import cy.agorise.graphenej.api.GetKeyReferences; import cy.agorise.graphenej.api.GetKeyReferences;
import cy.agorise.graphenej.api.GetRelativeAccountHistory; import cy.agorise.graphenej.api.GetRelativeAccountHistory;
import cy.agorise.graphenej.api.ListAssets; import cy.agorise.graphenej.api.ListAssets;
@ -40,7 +40,6 @@ import cy.agorise.graphenej.interfaces.WitnessResponseListener;
import cy.agorise.graphenej.models.AccountBalanceUpdate; import cy.agorise.graphenej.models.AccountBalanceUpdate;
import cy.agorise.graphenej.models.AccountProperties; import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.BaseResponse; import cy.agorise.graphenej.models.BaseResponse;
import cy.agorise.graphenej.models.BroadcastedTransaction;
import cy.agorise.graphenej.models.HistoricalTransfer; import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.models.SubscriptionResponse; import cy.agorise.graphenej.models.SubscriptionResponse;
import cy.agorise.graphenej.models.WitnessResponse; import cy.agorise.graphenej.models.WitnessResponse;
@ -237,8 +236,8 @@ public abstract class GrapheneApiGenerator {
}else{ }else{
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>(); ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
for(Asset asset: assets){ for(Asset asset: assets){
BitsharesAsset.Type assetType = null; BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){ /*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
assetType = BitsharesAsset.Type.CORE; assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){ }else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){
assetType = BitsharesAsset.Type.SMART_COIN; assetType = BitsharesAsset.Type.SMART_COIN;
@ -246,7 +245,7 @@ public abstract class GrapheneApiGenerator {
assetType = BitsharesAsset.Type.UIA; assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){ }else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET; assetType = BitsharesAsset.Type.PREDICTION_MARKET;
} }*/
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType); BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
responseAssets.add(responseAsset); responseAssets.add(responseAsset);
} }
@ -278,8 +277,8 @@ public abstract class GrapheneApiGenerator {
}else{ }else{
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>(); ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
for(Asset asset: assets){ for(Asset asset: assets){
BitsharesAsset.Type assetType = null; BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){ /*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
assetType = BitsharesAsset.Type.CORE; assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){ }else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){
assetType = BitsharesAsset.Type.SMART_COIN; assetType = BitsharesAsset.Type.SMART_COIN;
@ -287,7 +286,7 @@ public abstract class GrapheneApiGenerator {
assetType = BitsharesAsset.Type.UIA; assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){ }else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET; assetType = BitsharesAsset.Type.PREDICTION_MARKET;
} }*/
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType); BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
responseAssets.add(responseAsset); responseAssets.add(responseAsset);
} }
@ -323,35 +322,40 @@ public abstract class GrapheneApiGenerator {
if(update instanceof AccountBalanceUpdate){ if(update instanceof AccountBalanceUpdate){
AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update; AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update;
if(balanceUpdate.owner.equals(accountBitsharesId)){ if(balanceUpdate.owner.equals(accountBitsharesId)){
CryptoCoinBalance balance = new CryptoCoinBalance(); final CryptoCoinBalance balance = new CryptoCoinBalance();
balance.setAccountId(accountId); balance.setAccountId(accountId);
balance.setBalance(((AccountBalanceUpdate) update).balance); balance.setBalance(((AccountBalanceUpdate) update).balance);
/*LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type); BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type);
if(assetInfo == null || assetInfo.getValue() == null){ if(assetInfo == null ){
BitsharesAsset.Type assetType; final String assetType = ((AccountBalanceUpdate) update).asset_type;
Asset asset = balance.getAsset(); ArrayList<String> idAssets = new ArrayList<>();
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){ idAssets.add(assetType);
assetType = BitsharesAsset.Type.CORE; ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){ @Override
assetType = BitsharesAsset.Type.SMART_COIN; public void success(Object answer, int idPetition) {
}else if(asset.getAssetType().equals(Asset.AssetType.UIA)){ if(answer instanceof BitsharesAsset){
assetType = BitsharesAsset.Type.UIA; BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer);
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){ long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0];
assetType = BitsharesAsset.Type.PREDICTION_MARKET; info.setCryptoCurrencyId(cryptoCurrencyId);
} bitsharesAssetDao.insertBitsharesAssetInfo(info);
balance.setCryptoCurrencyId(cryptoCurrencyId);
balanceDao.insertCryptoCoinBalance(balance);
}
}
BitsharesAsset newAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType); @Override
BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset); public void fail(int idPetition) {
cryptoCurrencyDao.insertCryptoCurrency(newAsset);
bitsharesAssetDao.insertBitsharesAssetInfo(info); }
assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); });
getAssetById(idAssets,getAssetRequest);
}else {
balance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(balance);
} }
balance.setCryptoCurrency(assetInfo.getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(balance);*/
//TODO balance function
BitsharesAccountManager.refreshAccountTransactions(accountId,context); BitsharesAccountManager.refreshAccountTransactions(accountId,context);
} }
} }
} }
@ -381,37 +385,43 @@ public abstract class GrapheneApiGenerator {
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao(); final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao(); final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao(); final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), null, new WitnessResponseListener() { WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), new ArrayList<Asset>(), new WitnessResponseListener() {
@Override @Override
public void onSuccess(WitnessResponse response) { public void onSuccess(WitnessResponse response) {
List<AssetAmount> balances = (List<AssetAmount>) response.result; List<AssetAmount> balances = (List<AssetAmount>) response.result;
for(AssetAmount balance : balances){ for(final AssetAmount balance : balances){
CryptoCoinBalance ccBalance = new CryptoCoinBalance(); final CryptoCoinBalance ccBalance = new CryptoCoinBalance();
ccBalance.setAccountId(accountId); ccBalance.setAccountId(accountId);
ccBalance.setBalance(balance.getAmount().longValue()); ccBalance.setBalance(balance.getAmount().longValue());
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
if(assetInfo == null || assetInfo.getValue() == null){ if(assetInfo == null ){
BitsharesAsset.Type assetType = null; ArrayList<String> idAssets = new ArrayList<>();
Asset asset = balance.getAsset(); idAssets.add(balance.getAsset().getObjectId());
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){ ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
assetType = BitsharesAsset.Type.CORE; @Override
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){ public void success(Object answer, int idPetition) {
assetType = BitsharesAsset.Type.SMART_COIN; List<BitsharesAsset> assets = (List<BitsharesAsset>) answer;
}else if(asset.getAssetType().equals(Asset.AssetType.UIA)){ for(BitsharesAsset asset : assets) {
assetType = BitsharesAsset.Type.UIA; BitsharesAssetInfo info = new BitsharesAssetInfo(asset);
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){ long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset);
assetType = BitsharesAsset.Type.PREDICTION_MARKET; info.setCryptoCurrencyId(cryptoCurrencyId[0]);
} bitsharesAssetDao.insertBitsharesAssetInfo(info);
ccBalance.setCryptoCurrencyId(cryptoCurrencyId[0]);
balanceDao.insertCryptoCoinBalance(ccBalance);
}
}
BitsharesAsset newAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType); @Override
BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset); public void fail(int idPetition) {
cryptoCurrencyDao.insertCryptoCurrency(newAsset); }
bitsharesAssetDao.insertBitsharesAssetInfo(info); });
assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); getAssetById(idAssets,getAssetRequest);
}else {
ccBalance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(ccBalance);
} }
ccBalance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(ccBalance);
} }
} }
@ -425,4 +435,24 @@ public abstract class GrapheneApiGenerator {
} }
public static void getBlockHeaderTime(long blockHeader, final ApiRequest request){
WebSocketThread thread = new WebSocketThread(new GetBlockHeader(blockHeader, new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
if(response == null){
request.getListener().fail(request.getId());
}else {
request.getListener().success(response.result, request.getId());
}
}
@Override
public void onError(BaseResponse.Error error) {
request.getListener().fail(request.getId());
}
}),url);
thread.start();
}
} }

View file

@ -23,7 +23,7 @@ public interface BitsharesAssetDao {
LiveData<BitsharesAssetInfo> getBitsharesAssetInfo(long cryptoCurrencyId); LiveData<BitsharesAssetInfo> getBitsharesAssetInfo(long cryptoCurrencyId);
@Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId") @Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId")
LiveData<BitsharesAssetInfo> getBitsharesAssetInfoById(String bitsharesId); BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId);
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts); public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts);

View file

@ -25,6 +25,9 @@ public interface CryptoCurrencyDao {
@Query("SELECT * FROM crypto_currency WHERE id IN (:ids)") @Query("SELECT * FROM crypto_currency WHERE id IN (:ids)")
List<CryptoCurrency> getByIds(List<Long> ids); List<CryptoCurrency> getByIds(List<Long> ids);
@Query("SELECT * FROM crypto_currency WHERE name = :name")
CryptoCurrency getByName(String name);
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertCryptoCurrency(CryptoCurrency... currencies); public long[] insertCryptoCurrency(CryptoCurrency... currencies);

View file

@ -5,11 +5,15 @@ import android.content.Context;
import com.google.common.primitives.UnsignedLong; import com.google.common.primitives.UnsignedLong;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.ECKey; import org.bitcoinj.core.ECKey;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.TimeZone;
import cy.agorise.crystalwallet.apigenerator.ApiRequest; import cy.agorise.crystalwallet.apigenerator.ApiRequest;
import cy.agorise.crystalwallet.apigenerator.ApiRequestListener; import cy.agorise.crystalwallet.apigenerator.ApiRequestListener;
@ -21,9 +25,11 @@ import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendReque
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest;
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.dao.TransactionDao;
import cy.agorise.crystalwallet.enums.CryptoNet; import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.CryptoNetAccount;
@ -38,6 +44,7 @@ import cy.agorise.graphenej.PublicKey;
import cy.agorise.graphenej.Transaction; import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.models.AccountProperties; import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.BlockHeader;
import cy.agorise.graphenej.models.HistoricalTransfer; import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.objects.Memo; import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperationBuilder; import cy.agorise.graphenej.operations.TransferOperationBuilder;
@ -77,15 +84,25 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
GrapheneAccount grapheneAccount = (GrapheneAccount) account; GrapheneAccount grapheneAccount = (GrapheneAccount) account;
if(grapheneAccount.getAccountId() == null){ if(grapheneAccount.getAccountId() == null){
grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); System.out.println("Importing without accountid");
GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName());
if(fetch == null) {
//TODO grapaheneAccount null, error fetching
return null;
}
grapheneAccount.setAccountId(fetch.getAccountId());
}else if(grapheneAccount.getName() == null){ }else if(grapheneAccount.getName() == null){
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); System.out.println("Importing without accountname");
} GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId());
if(grapheneAccount == null) { if(fetch == null) {
//TODO grapaheneAccount null, error fetching //TODO grapaheneAccount null, error fetching
return null; return null;
}
grapheneAccount.setName(fetch.getName());
} }
System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId());
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount); db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount)); db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
@ -100,14 +117,28 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
@Override @Override
public void loadAccountFromDB(CryptoNetAccount account, Context context) { public void loadAccountFromDB(CryptoNetAccount account, Context context) {
System.out.println("On loadAccount from db");
if(account instanceof GrapheneAccount){ if(account instanceof GrapheneAccount){
GrapheneAccount grapheneAccount = (GrapheneAccount) account; GrapheneAccount grapheneAccount = (GrapheneAccount) account;
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(account.getId())); GrapheneAccountInfo info = db.grapheneAccountInfoDao().getByAccountId(account.getId());
grapheneAccount.loadInfo(info);
if(grapheneAccount.getAccountId() == null){ if(grapheneAccount.getAccountId() == null){
grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); System.out.println("On loadAccount null id");
GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName());
if(fetch != null){
info.setAccountId(fetch.getAccountId());
grapheneAccount.setAccountId(fetch.getAccountId());
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info);
}
}else if(grapheneAccount.getName() == null){ }else if(grapheneAccount.getName() == null){
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); System.out.println("On loadAccount null name");
GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId());
if(fetch != null) {
info.setName(fetch.getName());
grapheneAccount.setName(fetch.getName());
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info);
}
} }
if(grapheneAccount == null) { if(grapheneAccount == null) {
@ -115,6 +146,8 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
return; return;
} }
System.out.println("On loadAccount no nulls " + grapheneAccount.getName() + " " + grapheneAccount.getAccountId());
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
this.refreshAccountTransactions(account.getId(),context); this.refreshAccountTransactions(account.getId(),context);
GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
@ -268,23 +301,20 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
} }
public static void refreshAccountTransactions(long idAccount, Context context){ public static void refreshAccountTransactions(long idAccount, Context context){
System.out.println("Call refresh Account transactions from " + idAccount);
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
List<CryptoCoinTransaction> transactions = db.transactionDao().getByIdAccount(idAccount); List<CryptoCoinTransaction> transactions = db.transactionDao().getByIdAccount(idAccount);
CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount); CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount);
if(account.getCryptoNet() == CryptoNet.BITSHARES) { if(account.getCryptoNet() == CryptoNet.BITSHARES) {
GrapheneAccount grapheneAccount = new GrapheneAccount(account); GrapheneAccount grapheneAccount = new GrapheneAccount(account);
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(idAccount)); grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(idAccount));
int start = transactions.size(); int start = transactions.size();
int limit = 50; int limit = 50;
int stop = start + limit; int stop = start + limit;
ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db)); ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db));
GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest); GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest);
} }
} }
@ -312,13 +342,13 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
CryptoCoinTransaction transaction = new CryptoCoinTransaction(); CryptoCoinTransaction transaction = new CryptoCoinTransaction();
transaction.setAccountId(account.getId()); transaction.setAccountId(account.getId());
transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue()); transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue());
BitsharesAsset currency = null; CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol());
//TODO find currency by symbol db.cryptoCurrencyDao().getBySymbol(transfer.getOperation().getAssetAmount().getAsset().getSymbol())
if(currency == null){ if(currency == null){
System.out.println("CryptoCurrency not in database");
//CryptoCurrency not in database //CryptoCurrency not in database
Asset asset = transfer.getOperation().getAssetAmount().getAsset(); Asset asset = transfer.getOperation().getAssetAmount().getAsset();
BitsharesAsset.Type assetType = null; BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
if(asset.getAssetType() == Asset.AssetType.CORE_ASSET){ /*if(asset.getAssetType() == Asset.AssetType.CORE_ASSET){
assetType = BitsharesAsset.Type.CORE; assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType() == Asset.AssetType.SMART_COIN){ }else if(asset.getAssetType() == Asset.AssetType.SMART_COIN){
assetType = BitsharesAsset.Type.SMART_COIN; assetType = BitsharesAsset.Type.SMART_COIN;
@ -326,18 +356,19 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
assetType = BitsharesAsset.Type.PREDICTION_MARKET; assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}else if(asset.getAssetType() == Asset.AssetType.UIA){ }else if(asset.getAssetType() == Asset.AssetType.UIA){
assetType = BitsharesAsset.Type.UIA; assetType = BitsharesAsset.Type.UIA;
} }*/
currency = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(), assetType); currency = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(), assetType);
db.cryptoCurrencyDao().insertCryptoCurrency(currency); db.cryptoCurrencyDao().insertCryptoCurrency(currency);
db.bitsharesAssetDao().insertBitsharesAssetInfo(new BitsharesAssetInfo((BitsharesAsset)currency));
} }
transaction.setIdCurrency(currency.getId()); transaction.setIdCurrency(currency.getId());
transaction.setConfirmed(true); //graphene transaction are always confirmed transaction.setConfirmed(true); //graphene transaction are always confirmed
//TODO date of the transaction transaction.setDate(transfer.);
transaction.setFrom(transfer.getOperation().getFrom().getName()); transaction.setFrom(transfer.getOperation().getFrom().getName());
transaction.setInput(!transfer.getOperation().getFrom().getName().equals(account.getName())); transaction.setInput(!transfer.getOperation().getFrom().getName().equals(account.getName()));
transaction.setTo(transfer.getOperation().getTo().getName()); transaction.setTo(transfer.getOperation().getTo().getName());
db.transactionDao().insertTransaction(transaction);
GrapheneApiGenerator.getBlockHeaderTime(transfer.getBlockNum(),new ApiRequest(0,new GetTransactionDate(transaction,db.transactionDao())));
} }
if(transfers.size()>= limit){ if(transfers.size()>= limit){
int newStart= start + limit; int newStart= start + limit;
@ -387,4 +418,33 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
} }
} }
private static class GetTransactionDate implements ApiRequestListener{
private CryptoCoinTransaction transaction;
TransactionDao transactionDao;
public GetTransactionDate(CryptoCoinTransaction transaction, TransactionDao transactionDao) {
this.transaction = transaction;
this.transactionDao = transactionDao;
}
@Override
public void success(Object answer, int idPetition) {
if(answer instanceof BlockHeader){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
transaction.setDate(dateFormat.parse(((BlockHeader) answer).timestamp));
transactionDao.insertTransaction(transaction);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void fail(int idPetition) {
}
}
} }

View file

@ -18,7 +18,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin;
*/ */
@Entity(tableName="crypto_coin_balance", @Entity(tableName="crypto_coin_balance",
indices = {@Index("id"),@Index("account_id")}, indices = {@Index("id"),@Index("account_id"),@Index(value = {"account_id","crypto_currency_id"}, unique=true)},
foreignKeys = @ForeignKey(entity = CryptoNetAccount.class, foreignKeys = @ForeignKey(entity = CryptoNetAccount.class,
parentColumns = "id", parentColumns = "id",
childColumns = "account_id")) childColumns = "account_id"))

View file

@ -2,6 +2,7 @@ package cy.agorise.crystalwallet.models;
import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity; import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey; import android.arch.persistence.room.PrimaryKey;
import android.arch.persistence.room.TypeConverters; import android.arch.persistence.room.TypeConverters;
@ -13,7 +14,8 @@ import cy.agorise.crystalwallet.enums.CryptoNet;
* *
* Created by henry Henry Varona on 11/9/2017. * Created by henry Henry Varona on 11/9/2017.
*/ */
@Entity(tableName="crypto_currency") @Entity(tableName="crypto_currency",
indices = {@Index(value = {"crypto_net","name"}, unique=true)})
public class CryptoCurrency { public class CryptoCurrency {
/** /**