Real Time updates with the bitshares api

This commit is contained in:
henry 2018-01-23 22:07:17 -04:00
parent 68b0692a0f
commit fa8d39ca6d
2 changed files with 67 additions and 38 deletions

View file

@ -12,16 +12,19 @@ import cy.agorise.crystalwallet.dao.BitsharesAssetDao;
import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao; import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao;
import cy.agorise.crystalwallet.dao.CryptoCurrencyDao; import cy.agorise.crystalwallet.dao.CryptoCurrencyDao;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.dao.TransactionDao;
import cy.agorise.crystalwallet.manager.BitsharesAccountManager; 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.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence; import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence;
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;
import cy.agorise.graphenej.AssetAmount; import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.BaseOperation;
import cy.agorise.graphenej.Converter; import cy.agorise.graphenej.Converter;
import cy.agorise.graphenej.LimitOrder; import cy.agorise.graphenej.LimitOrder;
import cy.agorise.graphenej.ObjectType; import cy.agorise.graphenej.ObjectType;
@ -43,9 +46,11 @@ 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;
import cy.agorise.graphenej.operations.TransferOperation;
/** /**
@ -69,6 +74,7 @@ public abstract class GrapheneApiGenerator {
@Override @Override
public void onError(BaseResponse.Error error) { public void onError(BaseResponse.Error error) {
//TODO subcription hub error //TODO subcription hub error
System.out.println("GrapheneAPI error");
} }
}); });
@ -347,15 +353,16 @@ public abstract class GrapheneApiGenerator {
*/ */
public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId, public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId,
final Context context){ final Context context){
System.out.println("GrapheneAPI subscribe to account balance update");
if(!currentBitsharesListener.containsKey(accountId)){ if(!currentBitsharesListener.containsKey(accountId)){
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
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();
final TransactionDao transactionDao = db.transactionDao();
SubscriptionListener balanceListener = new SubscriptionListener() { SubscriptionListener balanceListener = new SubscriptionListener() {
@Override @Override
public ObjectType getInterestObjectType() { public ObjectType getInterestObjectType() {
return ObjectType.BALANCE_OBJECT; return ObjectType.TRANSACTION_OBJECT;
} }
@Override @Override
@ -363,43 +370,66 @@ public abstract class GrapheneApiGenerator {
List<Serializable> updatedObjects = (List<Serializable>) response.params.get(1); List<Serializable> updatedObjects = (List<Serializable>) response.params.get(1);
if(updatedObjects.size() > 0){ if(updatedObjects.size() > 0){
for(Serializable update : updatedObjects){ for(Serializable update : updatedObjects){
if(update instanceof AccountBalanceUpdate){ if(update instanceof BroadcastedTransaction){
AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update; BroadcastedTransaction transactionUpdate = (BroadcastedTransaction) update;
if(balanceUpdate.owner.equals(accountBitsharesId)){ for(BaseOperation operation : transactionUpdate.getTransaction().getOperations()){
final CryptoCoinBalance balance = new CryptoCoinBalance(); if(operation instanceof TransferOperation){
balance.setAccountId(accountId); TransferOperation tOperation = (TransferOperation) operation;
balance.setBalance(((AccountBalanceUpdate) update).balance); if(tOperation.getFrom().getObjectId().equals(accountBitsharesId) || tOperation.getTo().getObjectId().equals(accountBitsharesId)){
BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type); GrapheneApiGenerator.getAccountBalance(accountId,accountBitsharesId,context);
if(assetInfo == null ){ CryptoCoinTransaction transaction = new CryptoCoinTransaction();
final String assetType = ((AccountBalanceUpdate) update).asset_type; transaction.setAccountId(accountId);
ArrayList<String> idAssets = new ArrayList<>(); transaction.setAmount(tOperation.getAssetAmount().getAmount().longValue());
idAssets.add(assetType); BitsharesAssetInfo info = bitsharesAssetDao.getBitsharesAssetInfoById(tOperation.getAssetAmount().getAsset().getObjectId());
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() { if (info == null) {
//The cryptoCurrency is not in the database, queringfor its data
final Object SYNC = new Object(); //Object to syn the answer
ApiRequest assetRequest = new ApiRequest(0, new ApiRequestListener() {
@Override @Override
public void success(Object answer, int idPetition) { public void success(Object answer, int idPetition) {
if(answer instanceof BitsharesAsset){ ArrayList<BitsharesAsset> assets = (ArrayList<BitsharesAsset>) answer;
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); for(BitsharesAsset asset : assets){
long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0]; long idCryptoCurrency = cryptoCurrencyDao.insertCryptoCurrency(asset)[0];
info.setCryptoCurrencyId(cryptoCurrencyId); BitsharesAssetInfo info = new BitsharesAssetInfo(asset);
info.setCryptoCurrencyId(idCryptoCurrency);
asset.setId((int)idCryptoCurrency);
bitsharesAssetDao.insertBitsharesAssetInfo(info); bitsharesAssetDao.insertBitsharesAssetInfo(info);
balance.setCryptoCurrencyId(cryptoCurrencyId); }
balanceDao.insertCryptoCoinBalance(balance); synchronized (SYNC){
SYNC.notifyAll();
} }
} }
@Override @Override
public void fail(int idPetition) { public void fail(int idPetition) {
synchronized (SYNC){
SYNC.notifyAll();
}
} }
}); });
getAssetById(idAssets,getAssetRequest); ArrayList<String> assets = new ArrayList<>();
}else { assets.add(tOperation.getAssetAmount().getAsset().getObjectId());
GrapheneApiGenerator.getAssetById(assets,assetRequest);
balance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId()); synchronized (SYNC){
balanceDao.insertCryptoCoinBalance(balance); try {SYNC.wait(60000);} catch (InterruptedException ignore) {}
}
info = bitsharesAssetDao.getBitsharesAssetInfoById(tOperation.getAssetAmount().getAsset().getObjectId());
}
if( info == null){
//We couldn't retrieve the cryptocurrency
return;
}
transaction.setIdCurrency((int)info.getCryptoCurrencyId());
transaction.setConfirmed(true); //graphene transaction are always confirmed
transaction.setFrom(tOperation.getFrom().getObjectId());
transaction.setInput(!tOperation.getFrom().getObjectId().equals(accountBitsharesId));
transaction.setTo(tOperation.getTo().getObjectId());
transaction.setDate(new Date());
transactionDao.insertTransaction(transaction);
//GrapheneApiGenerator.getBlockHeaderTime(, new ApiRequest(0, new BitsharesAccountManager.GetTransactionDate(transaction, db.transactionDao())));
}
} }
BitsharesAccountManager.refreshAccountTransactions(accountId,context);
} }
} }
} }

View file

@ -465,7 +465,6 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
if (info == null) { if (info == null) {
//The cryptoCurrency is not in the database, queringfor its data //The cryptoCurrency is not in the database, queringfor its data
System.out.println("CryptoCurrency not in database");
final Object SYNC = new Object(); //Object to syn the answer final Object SYNC = new Object(); //Object to syn the answer
ApiRequest assetRequest = new ApiRequest(0, new ApiRequestListener() { ApiRequest assetRequest = new ApiRequest(0, new ApiRequestListener() {
@Override @Override
@ -628,7 +627,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
/** /**
* Class to retrieve the transaction date * Class to retrieve the transaction date
*/ */
private static class GetTransactionDate implements ApiRequestListener{ public static class GetTransactionDate implements ApiRequestListener{
/** /**
* The transaction to retrieve * The transaction to retrieve
*/ */
@ -638,7 +637,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
*/ */
TransactionDao transactionDao; TransactionDao transactionDao;
GetTransactionDate(CryptoCoinTransaction transaction, TransactionDao transactionDao) { public GetTransactionDate(CryptoCoinTransaction transaction, TransactionDao transactionDao) {
this.transaction = transaction; this.transaction = transaction;
this.transactionDao = transactionDao; this.transactionDao = transactionDao;
} }