From 322ffed6b97bca5d2187202a8cffc30cbe4c2145 Mon Sep 17 00:00:00 2001 From: henry Date: Mon, 6 Nov 2017 21:38:42 -0400 Subject: [PATCH] Equivalent Value in services --- .../apigenerator/GrapheneApiGenerator.java | 26 ++++++++- .../CryptoNetEquivalentRequest.java | 55 +++++++++++++++++++ .../manager/BitsharesAccountManager.java | 27 ++++++++- .../manager/CryptoAccountManager.java | 2 + 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/CryptoNetEquivalentRequest.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 399fca9..aa824cd 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -1,6 +1,5 @@ package cy.agorise.crystalwallet.apigenerator; -import android.arch.lifecycle.LiveData; import android.content.Context; import java.io.Serializable; @@ -21,6 +20,8 @@ import cy.agorise.crystalwallet.network.WebSocketThread; import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.AssetAmount; +import cy.agorise.graphenej.Converter; +import cy.agorise.graphenej.LimitOrder; import cy.agorise.graphenej.ObjectType; import cy.agorise.graphenej.Transaction; import cy.agorise.graphenej.UserAccount; @@ -29,6 +30,8 @@ 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.GetLimitOrders; +import cy.agorise.graphenej.api.GetMarketHistory; import cy.agorise.graphenej.api.GetRelativeAccountHistory; import cy.agorise.graphenej.api.ListAssets; import cy.agorise.graphenej.api.LookupAssetSymbols; @@ -455,4 +458,25 @@ public abstract class GrapheneApiGenerator { } + public static void getEquivalentValue(String baseId, String quoteId, final ApiRequest request){ + WebSocketThread thread = new WebSocketThread(new GetLimitOrders(baseId, quoteId, 100, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + List orders = (List) response.result; + for(LimitOrder order : orders){ + Converter converter = new Converter(); + double equiValue = converter.getConversionRate(order.getSellPrice(),Converter.BASE_TO_QUOTE); + request.getListener().success(equiValue,request.getId()); + break; + } + } + + @Override + public void onError(BaseResponse.Error error) { + request.getListener().fail(request.getId()); + } + }),url); + thread.start(); + } + } diff --git a/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/CryptoNetEquivalentRequest.java b/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/CryptoNetEquivalentRequest.java new file mode 100644 index 0000000..4fc9b14 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/CryptoNetEquivalentRequest.java @@ -0,0 +1,55 @@ +package cy.agorise.crystalwallet.cryptonetinforequests; + +import android.content.Context; + +import cy.agorise.crystalwallet.enums.CryptoCoin; +import cy.agorise.crystalwallet.models.CryptoCurrency; + +/** + * Created by henry on 6/11/2017. + */ + +public class CryptoNetEquivalentRequest extends CryptoNetInfoRequest { + + private Context context; + private CryptoCurrency fromCurrency; + private CryptoCurrency toCurrency; + private long price = -1; + + public CryptoNetEquivalentRequest(CryptoCoin coin, Context context, CryptoCurrency fromCurrency, CryptoCurrency toCurrency) { + super(coin); + this.context = context; + this.fromCurrency = fromCurrency; + this.toCurrency = toCurrency; + } + + public Context getContext() { + return context; + } + + public void setContext(Context context) { + this.context = context; + } + + public CryptoCurrency getFromCurrency() { + return fromCurrency; + } + + public void setFromCurrency(CryptoCurrency fromCurrency) { + this.fromCurrency = fromCurrency; + } + + public CryptoCurrency getToCurrency() { + return toCurrency; + } + + public void setToCurrency(CryptoCurrency toCurrency) { + this.toCurrency = toCurrency; + } + + public void setPrice(long price) { + this.price = price; + this._fireOnCarryOutEvent(); + } +} + diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java index c77246c..6e9a553 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -19,6 +19,7 @@ import cy.agorise.crystalwallet.apigenerator.ApiRequest; import cy.agorise.crystalwallet.apigenerator.ApiRequestListener; import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; +import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetEquivalentRequest; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequest; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestsListener; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendRequest; @@ -166,6 +167,10 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI this.validateExistAcccount((ValidateExistBitsharesAccountRequest) request); } else if (request instanceof ValidateBitsharesSendRequest){ this.validateSendRequest((ValidateBitsharesSendRequest) request); + }else if (request instanceof CryptoNetEquivalentRequest){ + this.getEquivalentValue((CryptoNetEquivalentRequest) request); + }else{ + //TODO not implemented } } @@ -305,7 +310,6 @@ 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 transactions = db.transactionDao().getByIdAccount(idAccount); CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount); @@ -410,6 +414,27 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } } + private static void getEquivalentValue(final CryptoNetEquivalentRequest request){ + if(request.getFromCurrency() instanceof BitsharesAsset && request.getToCurrency() instanceof BitsharesAsset) { + BitsharesAsset fromAsset = (BitsharesAsset) request.getFromCurrency(); + BitsharesAsset toAsset = (BitsharesAsset) request.getToCurrency(); + ApiRequest getEquivalentRequest = new ApiRequest(0, new ApiRequestListener() { + @Override + public void success(Object answer, int idPetition) { + request.setPrice((Long)answer); + } + + @Override + public void fail(int idPetition) { + //TODO error + } + }); + GrapheneApiGenerator.getEquivalentValue(fromAsset.getBitsharesId(),toAsset.getBitsharesId(), getEquivalentRequest); + }else{ + //TODO error + } + } + private class AccountIdOrNameListener implements ApiRequestListener{ Object SYNC; boolean ready = false; diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/CryptoAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/CryptoAccountManager.java index 42c9a3d..e1e3b6d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/CryptoAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/CryptoAccountManager.java @@ -33,4 +33,6 @@ public interface CryptoAccountManager { * @param account The CryptoNetAccount to be loaded */ public void loadAccountFromDB(CryptoNetAccount account, Context context); + + }