# Conflicts:
#	app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java
master
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;
import android.app.Application;
import android.arch.lifecycle.LiveData;
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.BitsharesAssetInfo;
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.graphenej.Address;
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.GetAccountByName;
import cy.agorise.graphenej.api.GetAccounts;
import cy.agorise.graphenej.api.GetBlockHeader;
import cy.agorise.graphenej.api.GetKeyReferences;
import cy.agorise.graphenej.api.GetRelativeAccountHistory;
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.AccountProperties;
import cy.agorise.graphenej.models.BaseResponse;
import cy.agorise.graphenej.models.BroadcastedTransaction;
import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.models.SubscriptionResponse;
import cy.agorise.graphenej.models.WitnessResponse;
@ -237,8 +236,8 @@ public abstract class GrapheneApiGenerator {
}else{
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
for(Asset asset: assets){
BitsharesAsset.Type assetType = null;
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
/*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;
@ -246,7 +245,7 @@ public abstract class GrapheneApiGenerator {
assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}
}*/
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
responseAssets.add(responseAsset);
}
@ -278,8 +277,8 @@ public abstract class GrapheneApiGenerator {
}else{
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
for(Asset asset: assets){
BitsharesAsset.Type assetType = null;
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
/*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;
@ -287,7 +286,7 @@ public abstract class GrapheneApiGenerator {
assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}
}*/
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
responseAssets.add(responseAsset);
}
@ -323,35 +322,40 @@ public abstract class GrapheneApiGenerator {
if(update instanceof AccountBalanceUpdate){
AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update;
if(balanceUpdate.owner.equals(accountBitsharesId)){
CryptoCoinBalance balance = new CryptoCoinBalance();
final 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;
}
BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type);
if(assetInfo == null ){
final String assetType = ((AccountBalanceUpdate) update).asset_type;
ArrayList<String> idAssets = new ArrayList<>();
idAssets.add(assetType);
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
@Override
public void success(Object answer, int idPetition) {
if(answer instanceof BitsharesAsset){
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer);
long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0];
info.setCryptoCurrencyId(cryptoCurrencyId);
bitsharesAssetDao.insertBitsharesAssetInfo(info);
balance.setCryptoCurrencyId(cryptoCurrencyId);
balanceDao.insertCryptoCoinBalance(balance);
}
}
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());
@Override
public void fail(int idPetition) {
}
});
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);
}
}
}
@ -381,37 +385,43 @@ public abstract class GrapheneApiGenerator {
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() {
WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), new ArrayList<Asset>(), new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
List<AssetAmount> balances = (List<AssetAmount>) response.result;
for(AssetAmount balance : balances){
CryptoCoinBalance ccBalance = new CryptoCoinBalance();
for(final AssetAmount balance : balances){
final CryptoCoinBalance ccBalance = new CryptoCoinBalance();
ccBalance.setAccountId(accountId);
ccBalance.setBalance(balance.getAmount().longValue());
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;
}
BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
if(assetInfo == null ){
ArrayList<String> idAssets = new ArrayList<>();
idAssets.add(balance.getAsset().getObjectId());
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
@Override
public void success(Object answer, int idPetition) {
List<BitsharesAsset> assets = (List<BitsharesAsset>) answer;
for(BitsharesAsset asset : assets) {
BitsharesAssetInfo info = new BitsharesAssetInfo(asset);
long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset);
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);
BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset);
cryptoCurrencyDao.insertCryptoCurrency(newAsset);
bitsharesAssetDao.insertBitsharesAssetInfo(info);
assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
@Override
public void fail(int idPetition) {
}
});
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);
@Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId")
LiveData<BitsharesAssetInfo> getBitsharesAssetInfoById(String bitsharesId);
BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts);

View File

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

View File

@ -5,11 +5,15 @@ import android.content.Context;
import com.google.common.primitives.UnsignedLong;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.ECKey;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import cy.agorise.crystalwallet.apigenerator.ApiRequest;
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.ValidateImportBitsharesAccountRequest;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.dao.TransactionDao;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
@ -38,6 +44,7 @@ import cy.agorise.graphenej.PublicKey;
import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.BlockHeader;
import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperationBuilder;
@ -77,15 +84,25 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
GrapheneAccount grapheneAccount = (GrapheneAccount) account;
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){
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId());
}
if(grapheneAccount == null) {
//TODO grapaheneAccount null, error fetching
return null;
System.out.println("Importing without accountname");
GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId());
if(fetch == null) {
//TODO grapaheneAccount null, error fetching
return null;
}
grapheneAccount.setName(fetch.getName());
}
System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId());
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
@ -100,14 +117,28 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
@Override
public void loadAccountFromDB(CryptoNetAccount account, Context context) {
System.out.println("On loadAccount from db");
if(account instanceof GrapheneAccount){
GrapheneAccount grapheneAccount = (GrapheneAccount) account;
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){
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){
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) {
@ -115,6 +146,8 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
return;
}
System.out.println("On loadAccount no nulls " + grapheneAccount.getName() + " " + grapheneAccount.getAccountId());
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
this.refreshAccountTransactions(account.getId(),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){
System.out.println("Call refresh Account transactions from " + idAccount);
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
List<CryptoCoinTransaction> transactions = db.transactionDao().getByIdAccount(idAccount);
CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount);
if(account.getCryptoNet() == CryptoNet.BITSHARES) {
GrapheneAccount grapheneAccount = new GrapheneAccount(account);
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(idAccount));
int start = transactions.size();
int limit = 50;
int stop = start + limit;
ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db));
GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest);
}
}
@ -312,13 +342,13 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
CryptoCoinTransaction transaction = new CryptoCoinTransaction();
transaction.setAccountId(account.getId());
transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue());
BitsharesAsset currency = null;
//TODO find currency by symbol db.cryptoCurrencyDao().getBySymbol(transfer.getOperation().getAssetAmount().getAsset().getSymbol())
CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol());
if(currency == null){
System.out.println("CryptoCurrency not in database");
//CryptoCurrency not in database
Asset asset = transfer.getOperation().getAssetAmount().getAsset();
BitsharesAsset.Type assetType = null;
if(asset.getAssetType() == Asset.AssetType.CORE_ASSET){
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
/*if(asset.getAssetType() == Asset.AssetType.CORE_ASSET){
assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType() == Asset.AssetType.SMART_COIN){
assetType = BitsharesAsset.Type.SMART_COIN;
@ -326,18 +356,19 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}else if(asset.getAssetType() == Asset.AssetType.UIA){
assetType = BitsharesAsset.Type.UIA;
}
}*/
currency = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(), assetType);
db.cryptoCurrencyDao().insertCryptoCurrency(currency);
db.bitsharesAssetDao().insertBitsharesAssetInfo(new BitsharesAssetInfo((BitsharesAsset)currency));
}
transaction.setIdCurrency(currency.getId());
transaction.setConfirmed(true); //graphene transaction are always confirmed
//TODO date of the transaction transaction.setDate(transfer.);
transaction.setFrom(transfer.getOperation().getFrom().getName());
transaction.setInput(!transfer.getOperation().getFrom().getName().equals(account.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){
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",
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,
parentColumns = "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.Entity;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey;
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.
*/
@Entity(tableName="crypto_currency")
@Entity(tableName="crypto_currency",
indices = {@Index(value = {"crypto_net","name"}, unique=true)})
public class CryptoCurrency {
/**