Make the grapheneapi multi account compatible
This commit is contained in:
parent
3b66b91807
commit
594bd4b816
1 changed files with 280 additions and 278 deletions
|
@ -79,7 +79,7 @@ public abstract class GrapheneApiGenerator {
|
|||
/**
|
||||
* This is used for manager each listener in the subscription thread
|
||||
*/
|
||||
private static HashMap<Long,SubscriptionListener> currentBitsharesListener = new HashMap<>();
|
||||
private static HashMap<Long, SubscriptionListener> currentBitsharesListener = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Retrieves the data of an account searching by it's id
|
||||
|
@ -87,7 +87,7 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param accountId The accountId to retrieve
|
||||
* @param request The Api request object, to answer this petition
|
||||
*/
|
||||
public static void getAccountById(String accountId, final ApiRequest request){
|
||||
public static void getAccountById(String accountId, final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetAccounts(accountId,
|
||||
new WitnessResponseListener() {
|
||||
@Override
|
||||
|
@ -96,7 +96,7 @@ public abstract class GrapheneApiGenerator {
|
|||
List list = (List) response.result;
|
||||
if (list.size() > 0) {
|
||||
if (list.get(0).getClass() == AccountProperties.class) {
|
||||
request.getListener().success(list.get(0),request.getId());
|
||||
request.getListener().success(list.get(0), request.getId());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
|
@ -118,17 +118,20 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param address The address to retrieve
|
||||
* @param request The Api request object, to answer this petition
|
||||
*/
|
||||
public static void getAccountByOwnerOrActiveAddress(Address address, final ApiRequest request){
|
||||
public static void getAccountByOwnerOrActiveAddress(Address address, final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetKeyReferences(address, true,
|
||||
new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
final List<List<UserAccount>> resp = (List<List<UserAccount>>) response.result;
|
||||
if(resp.size() > 0){
|
||||
if (resp.size() > 0) {
|
||||
List<UserAccount> accounts = resp.get(0);
|
||||
if(accounts.size() > 0){
|
||||
for(UserAccount account : accounts) {
|
||||
request.getListener().success(account,request.getId());}}}
|
||||
if (accounts.size() > 0) {
|
||||
for (UserAccount account : accounts) {
|
||||
request.getListener().success(account, request.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
|
||||
|
@ -136,7 +139,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
|
||||
thread.start();
|
||||
}
|
||||
|
@ -151,19 +154,19 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param request The Api request object, to answer this petition
|
||||
*/
|
||||
public static void getAccountTransaction(String accountGrapheneId, int start, int stop,
|
||||
int limit, final ApiRequest request){
|
||||
int limit, final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetRelativeAccountHistory(new UserAccount(accountGrapheneId),
|
||||
start, limit, stop, new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
request.getListener().success(response.result,request.getId());
|
||||
request.getListener().success(response.result, request.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
|
@ -173,16 +176,16 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param accountName The account Name to find
|
||||
* @param request The Api request object, to answer this petition
|
||||
*/
|
||||
public static void getAccountByName(String accountName, final ApiRequest request){
|
||||
public static void getAccountByName(String accountName, final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetAccountByName(accountName,
|
||||
new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
AccountProperties accountProperties = (AccountProperties)response.result;
|
||||
if(accountProperties == null){
|
||||
AccountProperties accountProperties = (AccountProperties) response.result;
|
||||
if (accountProperties == null) {
|
||||
request.getListener().fail(request.getId());
|
||||
}else{
|
||||
request.getListener().success(accountProperties,request.getId());
|
||||
} else {
|
||||
request.getListener().success(accountProperties, request.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,7 +193,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
|
@ -200,16 +203,16 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param accountName The account Name to find
|
||||
* @param request The Api request object, to answer this petition
|
||||
*/
|
||||
public static void getAccountIdByName(String accountName, final ApiRequest request){
|
||||
public static void getAccountIdByName(String accountName, final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetAccountByName(accountName,
|
||||
new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
AccountProperties accountProperties = (AccountProperties)response.result;
|
||||
if(accountProperties == null){
|
||||
request.getListener().success(null,request.getId());
|
||||
}else{
|
||||
request.getListener().success(accountProperties.id,request.getId());
|
||||
AccountProperties accountProperties = (AccountProperties) response.result;
|
||||
if (accountProperties == null) {
|
||||
request.getListener().success(null, request.getId());
|
||||
} else {
|
||||
request.getListener().success(accountProperties.id, request.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -217,7 +220,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
|
@ -229,19 +232,19 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param request the api request object, to answer this petition
|
||||
*/
|
||||
public static void broadcastTransaction(Transaction transaction, Asset feeAsset,
|
||||
final ApiRequest request){
|
||||
final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new TransactionBroadcastSequence(transaction,
|
||||
feeAsset, new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
request.getListener().success(true,request.getId());
|
||||
request.getListener().success(true, request.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
|
@ -251,18 +254,18 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param assetNames The list of the names of the assets to be retrieve
|
||||
* @param request the api request object, to answer this petition
|
||||
*/
|
||||
public static void getAssetByName(ArrayList<String> assetNames, final ApiRequest request){
|
||||
public static void getAssetByName(ArrayList<String> assetNames, final ApiRequest request) {
|
||||
|
||||
WebSocketThread thread = new WebSocketThread(new LookupAssetSymbols(assetNames,true,
|
||||
WebSocketThread thread = new WebSocketThread(new LookupAssetSymbols(assetNames, true,
|
||||
new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
List<Asset> assets = (List<Asset>) response.result;
|
||||
if(assets.size() <= 0){
|
||||
if (assets.size() <= 0) {
|
||||
request.getListener().fail(request.getId());
|
||||
}else{
|
||||
} else {
|
||||
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
|
||||
for(Asset asset: assets){
|
||||
for (Asset asset : assets) {
|
||||
//TODO asset type
|
||||
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
|
||||
/*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
|
||||
|
@ -275,10 +278,10 @@ public abstract class GrapheneApiGenerator {
|
|||
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
|
||||
}*/
|
||||
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),
|
||||
asset.getPrecision(),asset.getObjectId(),assetType);
|
||||
asset.getPrecision(), asset.getObjectId(), assetType);
|
||||
responseAssets.add(responseAsset);
|
||||
}
|
||||
request.getListener().success(responseAssets,request.getId());
|
||||
request.getListener().success(responseAssets, request.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,31 +289,32 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the asset ifnormation using the id of the net
|
||||
*
|
||||
* @param assetIds The list of the ids to retrieve
|
||||
* @param request the api request object, to answer this petition
|
||||
*/
|
||||
public static void getAssetById(ArrayList<String> assetIds, final ApiRequest request){
|
||||
public static void getAssetById(ArrayList<String> assetIds, final ApiRequest request) {
|
||||
ArrayList<Asset> assets = new ArrayList<>();
|
||||
for(String assetId : assetIds){
|
||||
for (String assetId : assetIds) {
|
||||
Asset asset = new Asset(assetId);
|
||||
assets.add(asset);
|
||||
}
|
||||
|
||||
WebSocketThread thread = new WebSocketThread(new LookupAssetSymbols(assets,true, new WitnessResponseListener() {
|
||||
WebSocketThread thread = new WebSocketThread(new LookupAssetSymbols(assets, true, new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
List<Asset> assets = (List<Asset>) response.result;
|
||||
if(assets.size() <= 0){
|
||||
if (assets.size() <= 0) {
|
||||
request.getListener().fail(request.getId());
|
||||
}else{
|
||||
} else {
|
||||
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
|
||||
for(Asset asset: assets){
|
||||
for (Asset asset : assets) {
|
||||
//TODO asset type
|
||||
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
|
||||
/*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
|
||||
|
@ -323,10 +327,10 @@ public abstract class GrapheneApiGenerator {
|
|||
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
|
||||
}*/
|
||||
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),
|
||||
asset.getPrecision(),asset.getObjectId(),assetType);
|
||||
asset.getPrecision(), asset.getObjectId(), assetType);
|
||||
responseAssets.add(responseAsset);
|
||||
}
|
||||
request.getListener().success(responseAssets,request.getId());
|
||||
request.getListener().success(responseAssets, request.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -334,7 +338,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
}
|
||||
|
||||
|
@ -346,8 +350,8 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param context The android context of this application
|
||||
*/
|
||||
public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId,
|
||||
final Context context){
|
||||
if(!currentBitsharesListener.containsKey(accountId)){
|
||||
final Context context) {
|
||||
if (!currentBitsharesListener.containsKey(accountId)) {
|
||||
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
||||
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
|
||||
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
|
||||
|
@ -360,15 +364,15 @@ public abstract class GrapheneApiGenerator {
|
|||
@Override
|
||||
public void onSubscriptionUpdate(SubscriptionResponse response) {
|
||||
List<Serializable> updatedObjects = (List<Serializable>) response.params.get(1);
|
||||
if(updatedObjects.size() > 0){
|
||||
for(Serializable update : updatedObjects){
|
||||
if(update instanceof BroadcastedTransaction){
|
||||
if (updatedObjects.size() > 0) {
|
||||
for (Serializable update : updatedObjects) {
|
||||
if (update instanceof BroadcastedTransaction) {
|
||||
BroadcastedTransaction transactionUpdate = (BroadcastedTransaction) update;
|
||||
for(BaseOperation operation : transactionUpdate.getTransaction().getOperations()){
|
||||
if(operation instanceof TransferOperation){
|
||||
for (BaseOperation operation : transactionUpdate.getTransaction().getOperations()) {
|
||||
if (operation instanceof TransferOperation) {
|
||||
final TransferOperation tOperation = (TransferOperation) operation;
|
||||
if(tOperation.getFrom().getObjectId().equals(accountBitsharesId) || tOperation.getTo().getObjectId().equals(accountBitsharesId)){
|
||||
GrapheneApiGenerator.getAccountBalance(accountId,accountBitsharesId,context);
|
||||
if (tOperation.getFrom().getObjectId().equals(accountBitsharesId) || tOperation.getTo().getObjectId().equals(accountBitsharesId)) {
|
||||
GrapheneApiGenerator.getAccountBalance(accountId, accountBitsharesId, context);
|
||||
final CryptoCoinTransaction transaction = new CryptoCoinTransaction();
|
||||
transaction.setAccountId(accountId);
|
||||
transaction.setAmount(tOperation.getAssetAmount().getAmount().longValue());
|
||||
|
@ -379,13 +383,13 @@ public abstract class GrapheneApiGenerator {
|
|||
@Override
|
||||
public void success(Object answer, int idPetition) {
|
||||
ArrayList<BitsharesAsset> assets = (ArrayList<BitsharesAsset>) answer;
|
||||
for(BitsharesAsset asset : assets){
|
||||
for (BitsharesAsset asset : assets) {
|
||||
long idCryptoCurrency = cryptoCurrencyDao.insertCryptoCurrency(asset)[0];
|
||||
BitsharesAssetInfo info = new BitsharesAssetInfo(asset);
|
||||
info.setCryptoCurrencyId(idCryptoCurrency);
|
||||
asset.setId((int)idCryptoCurrency);
|
||||
asset.setId((int) idCryptoCurrency);
|
||||
bitsharesAssetDao.insertBitsharesAssetInfo(info);
|
||||
saveTransaction(transaction,cryptoCurrencyDao.getById(info.getCryptoCurrencyId()),accountBitsharesId,tOperation,context);
|
||||
saveTransaction(transaction, cryptoCurrencyDao.getById(info.getCryptoCurrencyId()), accountBitsharesId, tOperation, context);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -396,9 +400,9 @@ public abstract class GrapheneApiGenerator {
|
|||
});
|
||||
ArrayList<String> assets = new ArrayList<>();
|
||||
assets.add(tOperation.getAssetAmount().getAsset().getObjectId());
|
||||
GrapheneApiGenerator.getAssetById(assets,assetRequest);
|
||||
}else{
|
||||
saveTransaction(transaction,cryptoCurrencyDao.getById(info.getCryptoCurrencyId()),accountBitsharesId,tOperation,context);
|
||||
GrapheneApiGenerator.getAssetById(assets, assetRequest);
|
||||
} else {
|
||||
saveTransaction(transaction, cryptoCurrencyDao.getById(info.getCryptoCurrencyId()), accountBitsharesId, tOperation, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -410,27 +414,25 @@ public abstract class GrapheneApiGenerator {
|
|||
}
|
||||
};
|
||||
|
||||
currentBitsharesListener.put(accountId,balanceListener);
|
||||
currentBitsharesListener.put(accountId, balanceListener);
|
||||
bitsharesSubscriptionHub.addSubscriptionListener(balanceListener);
|
||||
try {
|
||||
if (!subscriptionThread.isConnected() && !subscriptionThread.isAlive()) {
|
||||
|
||||
if(!subscriptionThread.isConnected() && !subscriptionThread.isAlive()){
|
||||
try {
|
||||
subscriptionThread.start();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else if(bitsharesSubscriptionHub!= null && !bitsharesSubscriptionHub.isSubscribed()){
|
||||
try {
|
||||
|
||||
} else if (bitsharesSubscriptionHub != null && !bitsharesSubscriptionHub.isSubscribed()) {
|
||||
bitsharesSubscriptionHub.resubscribe();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to save a transaction retrieved from the update
|
||||
*
|
||||
* @param transaction The transaction db object
|
||||
* @param currency The currency of the transaccion
|
||||
* @param accountBitsharesId The id of the account in the bitshares network
|
||||
|
@ -438,24 +440,24 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param context The context of this app
|
||||
*/
|
||||
private static void saveTransaction(CryptoCoinTransaction transaction, CryptoCurrency currency,
|
||||
String accountBitsharesId, TransferOperation tOperation ,
|
||||
Context context){
|
||||
transaction.setIdCurrency((int)currency.getId());
|
||||
String accountBitsharesId, TransferOperation tOperation,
|
||||
Context context) {
|
||||
transaction.setIdCurrency((int) currency.getId());
|
||||
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());
|
||||
CrystalDatabase.getAppDatabase(context).transactionDao().insertTransaction(transaction);
|
||||
if(transaction.getInput()){
|
||||
CryptoNetEvents.getInstance().fireEvent(new ReceivedFundsCryptoNetEvent(transaction.getAccount(),currency,transaction.getAmount()));
|
||||
if (transaction.getInput()) {
|
||||
CryptoNetEvents.getInstance().fireEvent(new ReceivedFundsCryptoNetEvent(transaction.getAccount(), currency, transaction.getAmount()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels all bitshares account subscriptions
|
||||
*/
|
||||
public static void cancelBitsharesAccountSubscriptions(){
|
||||
public static void cancelBitsharesAccountSubscriptions() {
|
||||
bitsharesSubscriptionHub.cancelSubscriptions();
|
||||
}
|
||||
|
||||
|
@ -467,7 +469,7 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param context The android context of this application
|
||||
*/
|
||||
public static void getAccountBalance(final long accountId, final String accountGrapheneId,
|
||||
final Context context){
|
||||
final Context context) {
|
||||
|
||||
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
||||
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
|
||||
|
@ -478,19 +480,19 @@ public abstract class GrapheneApiGenerator {
|
|||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
List<AssetAmount> balances = (List<AssetAmount>) response.result;
|
||||
for(final AssetAmount balance : balances){
|
||||
for (final AssetAmount balance : balances) {
|
||||
final CryptoCoinBalance ccBalance = new CryptoCoinBalance();
|
||||
ccBalance.setAccountId(accountId);
|
||||
ccBalance.setBalance(balance.getAmount().longValue());
|
||||
BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
|
||||
if(assetInfo == null ){
|
||||
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) {
|
||||
for (BitsharesAsset asset : assets) {
|
||||
BitsharesAssetInfo info = new BitsharesAssetInfo(asset);
|
||||
long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset);
|
||||
info.setCryptoCurrencyId(cryptoCurrencyId[0]);
|
||||
|
@ -504,9 +506,9 @@ public abstract class GrapheneApiGenerator {
|
|||
public void fail(int idPetition) {
|
||||
}
|
||||
});
|
||||
getAssetById(idAssets,getAssetRequest);
|
||||
getAssetById(idAssets, getAssetRequest);
|
||||
|
||||
}else {
|
||||
} else {
|
||||
|
||||
ccBalance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId());
|
||||
balanceDao.insertCryptoCoinBalance(ccBalance);
|
||||
|
@ -518,7 +520,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
|
||||
thread.start();
|
||||
|
||||
|
@ -530,13 +532,13 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param blockHeader The block header to retrieve the date time
|
||||
* @param request the api request object, to answer this petition
|
||||
*/
|
||||
public static void getBlockHeaderTime(long blockHeader, final ApiRequest request){
|
||||
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){
|
||||
if (response == null) {
|
||||
request.getListener().fail(request.getId());
|
||||
}else {
|
||||
} else {
|
||||
request.getListener().success(response.result, request.getId());
|
||||
}
|
||||
}
|
||||
|
@ -545,7 +547,7 @@ public abstract class GrapheneApiGenerator {
|
|||
public void onError(BaseResponse.Error error) {
|
||||
request.getListener().fail(request.getId());
|
||||
}
|
||||
}),CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
}), CryptoNetManager.getURL(CryptoNet.BITSHARES));
|
||||
thread.start();
|
||||
|
||||
}
|
||||
|
@ -557,17 +559,17 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param quoteId the quote asset bitshares id
|
||||
* @param request the api request object, to answer this petition
|
||||
*/
|
||||
public static void getEquivalentValue(final String baseId, String quoteId, final ApiRequest request){
|
||||
public static void getEquivalentValue(final String baseId, String quoteId, final ApiRequest request) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetLimitOrders(baseId, quoteId, 10,
|
||||
new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
List<LimitOrder> orders = (List<LimitOrder>) response.result;
|
||||
if(orders.size()<= 0){
|
||||
if (orders.size() <= 0) {
|
||||
//TODO indirect equivalent value
|
||||
}
|
||||
for(LimitOrder order : orders){
|
||||
if(order.getSellPrice().base.getAsset().getBitassetId().equals(baseId)) {
|
||||
for (LimitOrder order : orders) {
|
||||
if (order.getSellPrice().base.getAsset().getBitassetId().equals(baseId)) {
|
||||
Converter converter = new Converter();
|
||||
double equiValue = converter.getConversionRate(order.getSellPrice(),
|
||||
Converter.BASE_TO_QUOTE);
|
||||
|
@ -593,11 +595,11 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param context The android context of this application
|
||||
*/
|
||||
public static void getEquivalentValue(BitsharesAsset baseAsset,
|
||||
final List<BitsharesAsset> quoteAssets, final Context context){
|
||||
for(BitsharesAsset quoteAsset : quoteAssets){
|
||||
final List<BitsharesAsset> quoteAssets, final Context context) {
|
||||
for (BitsharesAsset quoteAsset : quoteAssets) {
|
||||
WebSocketThread thread = new WebSocketThread(new GetLimitOrders(baseAsset.getBitsharesId(),
|
||||
quoteAsset.getBitsharesId(), 10, new EquivalentValueListener(baseAsset,
|
||||
quoteAsset,context)), BitsharesConstant.EQUIVALENT_URL); //todo change equivalent url for current server url
|
||||
quoteAsset, context)), BitsharesConstant.EQUIVALENT_URL); //todo change equivalent url for current server url
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
@ -609,22 +611,22 @@ public abstract class GrapheneApiGenerator {
|
|||
* @param quoteAssets The list of quotes assets to query
|
||||
* @param context The Context of this Application
|
||||
*/
|
||||
public static void getEquivalentValue(String baseAssetName, final List<BitsharesAsset> quoteAssets, final Context context){
|
||||
public static void getEquivalentValue(String baseAssetName, final List<BitsharesAsset> quoteAssets, final Context context) {
|
||||
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
||||
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
|
||||
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
|
||||
CryptoCurrency baseCurrency = cryptoCurrencyDao.getByName(baseAssetName);
|
||||
BitsharesAssetInfo info = null;
|
||||
if(baseCurrency != null){
|
||||
if (baseCurrency != null) {
|
||||
info = db.bitsharesAssetDao().getBitsharesAssetInfo(baseCurrency.getId());
|
||||
}
|
||||
if(baseCurrency == null || info == null){
|
||||
if (baseCurrency == null || info == null) {
|
||||
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
|
||||
@Override
|
||||
public void success(Object answer, int idPetition) {
|
||||
if(answer instanceof BitsharesAsset){
|
||||
if (answer instanceof BitsharesAsset) {
|
||||
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer);
|
||||
long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0];
|
||||
long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) answer)[0];
|
||||
info.setCryptoCurrencyId(cryptoCurrencyId);
|
||||
bitsharesAssetDao.insertBitsharesAssetInfo(info);
|
||||
GrapheneApiGenerator.getEquivalentValue((BitsharesAsset) answer, quoteAssets, context);
|
||||
|
@ -638,12 +640,12 @@ public abstract class GrapheneApiGenerator {
|
|||
});
|
||||
ArrayList<String> names = new ArrayList<>();
|
||||
names.add(baseAssetName);
|
||||
GrapheneApiGenerator.getAssetByName(names,getAssetRequest);
|
||||
GrapheneApiGenerator.getAssetByName(names, getAssetRequest);
|
||||
|
||||
}else {
|
||||
} else {
|
||||
BitsharesAsset baseAsset = new BitsharesAsset(baseCurrency);
|
||||
baseAsset.loadInfo(info);
|
||||
getEquivalentValue(baseAsset,quoteAssets,context);
|
||||
getEquivalentValue(baseAsset, quoteAssets, context);
|
||||
}
|
||||
|
||||
|
||||
|
@ -652,7 +654,7 @@ public abstract class GrapheneApiGenerator {
|
|||
/**
|
||||
* Listener of the equivalent value the answer is stored in the database, for use in conjuntion with LiveData
|
||||
*/
|
||||
private static class EquivalentValueListener implements WitnessResponseListener{
|
||||
private static class EquivalentValueListener implements WitnessResponseListener {
|
||||
/**
|
||||
* The base asset
|
||||
*/
|
||||
|
@ -675,10 +677,10 @@ public abstract class GrapheneApiGenerator {
|
|||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
List<LimitOrder> orders = (List<LimitOrder>) response.result;
|
||||
if(orders.size()<= 0){
|
||||
if (orders.size() <= 0) {
|
||||
//TODO indirect equivalent value
|
||||
}
|
||||
for(LimitOrder order : orders){
|
||||
for (LimitOrder order : orders) {
|
||||
try {
|
||||
//if (order.getSellPrice().base.getAsset().getBitassetId().equals(baseAsset.getBitsharesId())) {
|
||||
Converter converter = new Converter();
|
||||
|
@ -689,7 +691,7 @@ public abstract class GrapheneApiGenerator {
|
|||
CrystalDatabase.getAppDatabase(context).cryptoCurrencyEquivalenceDao().insertCryptoCurrencyEquivalence(equivalence);
|
||||
break;
|
||||
//}
|
||||
}catch(Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue