Updated on 2026-08-14
This commit is contained in:
parent
07bc724ecd
commit
75f19d41f4
278 changed files with 19571 additions and 206 deletions
|
|
@ -15,7 +15,7 @@ public enum Blockchain {
|
|||
BitcoinCash("BCH", "BCH", R.drawable.ic_logo_bitcoin_cash, "Bitcoin Cash"),
|
||||
Litecoin("LTC", "LTC", R.drawable.ic_logo_bitcoin, "Litecoin"),
|
||||
Stellar("XLM", "XLM", R.drawable.ic_logo_stellar, "Stellar Lumens"),
|
||||
StellarTestNet("XLM/test", "XLM", R.drawable.ic_logo_bitcoin, "Stellar Lumens");
|
||||
StellarTestNet("XLM/test", "XLM", R.drawable.ic_logo_stellar, "Stellar Lumens");
|
||||
Blockchain(String ID, String currency, int imageResource, String officialName) {
|
||||
mID = ID;
|
||||
mCurrency = currency;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
package com.tangem.data.network;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.tangem.App;
|
||||
import com.tangem.data.Blockchain;
|
||||
import com.tangem.domain.wallet.TangemContext;
|
||||
import com.tangem.util.LOG;
|
||||
import com.tangem.wallet.R;
|
||||
|
||||
import org.stellar.sdk.Network;
|
||||
import org.stellar.sdk.Server;
|
||||
import org.stellar.sdk.requests.ErrorResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
|
@ -18,15 +18,17 @@ import io.reactivex.observers.DefaultObserver;
|
|||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Request processor for Electrum Api
|
||||
* Created by dvol on 7.01.2019.
|
||||
*
|
||||
* Request processor for Stellar Horizon Rest Api
|
||||
* Every request live cycle:
|
||||
* 1. In application create request and call {@link ServerApiStellar}.requestData(..)
|
||||
* 2. Try send every request for max 4 times,
|
||||
* 3. If all 4 times fail call DefaultObserver<StellarRequest>.onError (defined in .requestData(..)) and than
|
||||
* {@link Listener}.onFail(...) callback
|
||||
* Error can be acquired with {@link StellarRequest}.getError() method
|
||||
* 4. If request network communication finished successfully then call DefaultObserver<StellarRequest>.onComplete (defined in .requestData) and than
|
||||
* {@link Listener}.onSuccess(...) callback
|
||||
* {@link Listener}.onFail(...) callback
|
||||
* Error can be acquired with {@link StellarRequest}.getError() method
|
||||
* 4. If request network communication finished successfully then call DefaultObserver<StellarRequest.Base>.onComplete (defined in .requestData) and than
|
||||
* {@link Listener}.onSuccess(...) callback
|
||||
*/
|
||||
public class ServerApiStellar {
|
||||
private static String TAG = ServerApiStellar.class.getSimpleName();
|
||||
|
|
@ -37,10 +39,10 @@ public class ServerApiStellar {
|
|||
*/
|
||||
private Listener listener;
|
||||
|
||||
private int requestsCount=0;
|
||||
private int requestsCount = 0;
|
||||
|
||||
public boolean isRequestsSequenceCompleted() {
|
||||
Log.i(TAG, String.format("isRequestsSequenceCompleted: %s (%d requests left)", String.valueOf(requestsCount <= 0), requestsCount));
|
||||
LOG.i(TAG, String.format("isRequestsSequenceCompleted: %s (%d requests left)", String.valueOf(requestsCount <= 0), requestsCount));
|
||||
return requestsCount <= 0;
|
||||
}
|
||||
|
||||
|
|
@ -51,11 +53,14 @@ public class ServerApiStellar {
|
|||
|
||||
/**
|
||||
* Notify that request processing was successful
|
||||
*
|
||||
* @param stellarRequest - processed request containing received answer {@see stellarRequest.getAnswer() method}
|
||||
*/
|
||||
void onSuccess(StellarRequest.Base stellarRequest);
|
||||
|
||||
/**
|
||||
* Notify that request processing was successful
|
||||
*
|
||||
* @param stellarRequest - processed request containing occurred error {@see stellarRequest.getError() method}
|
||||
*/
|
||||
void onFail(StellarRequest.Base stellarRequest);
|
||||
|
|
@ -63,6 +68,7 @@ public class ServerApiStellar {
|
|||
|
||||
/**
|
||||
* Set notificaion listener
|
||||
*
|
||||
* @param listener
|
||||
*/
|
||||
public void setListener(Listener listener) {
|
||||
|
|
@ -72,25 +78,26 @@ public class ServerApiStellar {
|
|||
|
||||
/**
|
||||
* Start process request
|
||||
*
|
||||
* @param ctx
|
||||
* @param stellarRequest
|
||||
*/
|
||||
public void requestData(TangemContext ctx, StellarRequest.Base stellarRequest) {
|
||||
requestsCount++;
|
||||
Log.i(TAG, String.format("New request[%d]: %s", requestsCount,stellarRequest.getClass().getSimpleName()));
|
||||
LOG.i(TAG, String.format("New request[%d]: %s", requestsCount, stellarRequest.getClass().getSimpleName()));
|
||||
Observable<StellarRequest.Base> stellarObserver = Observable.just(stellarRequest)
|
||||
.doOnNext(stellarRequest1 -> doStellarRequest(ctx, stellarRequest))
|
||||
.doOnEach(stellarRequest1 -> doStellarRequest(ctx, stellarRequest))
|
||||
|
||||
// .flatMap(stellarRequest1 -> {
|
||||
// if (stellarRequest1.answerData == null) {
|
||||
// Log.e(TAG, "NullPointerException " + stellarRequest.getMethod());
|
||||
// return Observable.error(new NullPointerException());
|
||||
// } else
|
||||
// return Observable.just(stellarRequest1);
|
||||
// })
|
||||
//
|
||||
.flatMap(stellarRequest1 -> {
|
||||
if (stellarRequest1.errorResponse != null) {
|
||||
LOG.e(TAG, "Error response on " + stellarRequest.getClass().getSimpleName());
|
||||
return Observable.error(stellarRequest.errorResponse);
|
||||
} else
|
||||
return Observable.just(stellarRequest1);
|
||||
}
|
||||
)
|
||||
.retryWhen(errors -> errors
|
||||
.filter(throwable -> throwable instanceof IOException)
|
||||
.filter(throwable -> (throwable instanceof IOException) || (throwable instanceof ErrorResponse))
|
||||
.zipWith(Observable.range(1, 4), (n, i) -> i))
|
||||
|
||||
.subscribeOn(Schedulers.io())
|
||||
|
|
@ -99,14 +106,14 @@ public class ServerApiStellar {
|
|||
|
||||
@Override
|
||||
public void onNext(StellarRequest.Base stellarRequest) {
|
||||
|
||||
LOG.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onNext ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
requestsCount--;
|
||||
Log.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onError " + e.getMessage());
|
||||
Log.e(TAG, String.format("%d requests left in processing",requestsCount));
|
||||
LOG.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onError " + e.getMessage());
|
||||
LOG.e(TAG, String.format("%d requests left in processing", requestsCount));
|
||||
stellarRequest.setError(ctx.getString(R.string.cannot_obtain_data_from_blockchain));
|
||||
//setErrorOccurred(e.getMessage());//;
|
||||
listener.onFail(stellarRequest);
|
||||
|
|
@ -118,12 +125,12 @@ public class ServerApiStellar {
|
|||
@Override
|
||||
public void onComplete() {
|
||||
requestsCount--;
|
||||
Log.e(TAG, String.format("%d requests left in processing",requestsCount));
|
||||
LOG.e(TAG, String.format("%d requests left in processing", requestsCount));
|
||||
if (stellarRequest.getError() != null) {
|
||||
Log.i(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error!=null");
|
||||
LOG.i(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error!=null");
|
||||
listener.onFail(stellarRequest);
|
||||
} else {
|
||||
Log.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error==null");
|
||||
LOG.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error==null");
|
||||
listener.onSuccess(stellarRequest);
|
||||
}
|
||||
}
|
||||
|
|
@ -134,14 +141,19 @@ public class ServerApiStellar {
|
|||
private void doStellarRequest(TangemContext ctx, StellarRequest.Base stellarRequest) throws IOException {
|
||||
stellarRequest.setError(null);
|
||||
try {
|
||||
if( ctx.getBlockchain()==Blockchain.StellarTestNet ) {
|
||||
if (ctx.getBlockchain() == Blockchain.StellarTestNet) {
|
||||
Network.useTestNetwork();
|
||||
}
|
||||
Server server = new Server(ServerURL.API_STELLAR);
|
||||
stellarRequest.process(server);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
try {
|
||||
LOG.e(TAG, "--- request " + stellarRequest.getClass().getSimpleName());
|
||||
stellarRequest.process(server);
|
||||
} catch (ErrorResponse errorResponse) {
|
||||
LOG.e(TAG, "--- error response: " + errorResponse.getMessage());
|
||||
stellarRequest.errorResponse = errorResponse;
|
||||
stellarRequest.setError(errorResponse.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
stellarRequest.setError(App.getInstance().getString(R.string.cannot_obtain_data_from_blockchain_communication_error));
|
||||
throw e;
|
||||
|
|
|
|||
|
|
@ -3,19 +3,22 @@ package com.tangem.data.network;
|
|||
import org.stellar.sdk.KeyPair;
|
||||
import org.stellar.sdk.Server;
|
||||
import org.stellar.sdk.Transaction;
|
||||
import org.stellar.sdk.requests.ErrorResponse;
|
||||
import org.stellar.sdk.responses.AccountResponse;
|
||||
import org.stellar.sdk.responses.SubmitTransactionResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by dvol on 16.07.2017.
|
||||
* Created by dvol on 7.01.2019.
|
||||
*/
|
||||
|
||||
public class StellarRequest {
|
||||
|
||||
public static abstract class Base {
|
||||
public ErrorResponse errorResponse;
|
||||
private String error = null;
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
|
@ -28,28 +31,25 @@ public class StellarRequest {
|
|||
}
|
||||
|
||||
public static class Balance extends Base {
|
||||
public KeyPair accountKeyPair;
|
||||
KeyPair accountKeyPair;
|
||||
public AccountResponse accountResponse;
|
||||
|
||||
public Balance(String walletAddress)
|
||||
{
|
||||
accountKeyPair=KeyPair.fromAccountId(walletAddress);
|
||||
public Balance(String walletAddress) {
|
||||
accountKeyPair = KeyPair.fromAccountId(walletAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(Server server) throws IOException {
|
||||
accountResponse=server.accounts().account(accountKeyPair);
|
||||
accountResponse = server.accounts().account(accountKeyPair);
|
||||
}
|
||||
}
|
||||
|
||||
public static class SubmitTransaction extends Base {
|
||||
// public KeyPair sourceAccount;
|
||||
// public KeyPair targetAccount;
|
||||
public Transaction transaction;
|
||||
public SubmitTransactionResponse response;
|
||||
|
||||
public SubmitTransaction(Transaction transaction) {
|
||||
this.transaction=transaction;
|
||||
this.transaction = transaction;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue