Updated on 2026-08-14

This commit is contained in:
Tangem 2019-07-08 15:13:52 +03:00
commit 9f38de003a
279 changed files with 20155 additions and 10 deletions

View file

@ -22,7 +22,9 @@ public enum Blockchain {
Binance("BINANCE", "BNB", 100000000.0, R.drawable.tangem2, "Binance"),
BinanceTestNet("BINANCE/test", "BNB", 100000000.0, R.drawable.tangem2, "Binance Testnet"),
Matic("MATIC", "MTX", 1.0, R.drawable.tangem2, "Matic"),
MaticTestNet("MATIC/test", "MTX", 1.0, R.drawable.tangem2, "Matic Testnet");
MaticTestNet("MATIC/test", "MTX", 1.0, R.drawable.tangem2, "Matic Testnet"),
Stellar("XLM", "XLM", 1000000.0, R.drawable.ic_logo_stellar, "Stellar"),
StellarTestNet("XLM/test", "XLM", 1000000.0, R.drawable.ic_logo_stellar, "Stellar Testnet");
Blockchain(String ID, String currency, double multiplier, int imageResource, String officialName) {
mID = ID;
@ -105,6 +107,9 @@ public enum Blockchain {
case "ETH":
return R.drawable.ic_logo_ethereum;
case "XLM":
return R.drawable.ic_logo_stellar;
}
return R.drawable.tangem2;
}

View file

@ -0,0 +1,169 @@
package com.tangem.data.network;
import com.tangem.App;
import com.tangem.data.Blockchain;
import com.tangem.util.LOG;
import com.tangem.wallet.R;
import com.tangem.wallet.TangemContext;
import org.stellar.sdk.Network;
import org.stellar.sdk.Server;
import org.stellar.sdk.requests.ErrorResponse;
import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;
/**
* Created by dvol on 7.01.2019.
* <p>
* 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.Base>.onComplete (defined in .requestData) and than
* {@link Listener}.onSuccess(...) callback
*/
public class ServerApiStellar {
private static String TAG = ServerApiStellar.class.getSimpleName();
/**
* TCP, SSL
* Used in BTC, BCH
*/
private Listener listener;
private int requestsCount = 0;
public boolean isRequestsSequenceCompleted() {
LOG.i(TAG, String.format("isRequestsSequenceCompleted: %s (%d requests left)", String.valueOf(requestsCount <= 0), requestsCount));
return requestsCount <= 0;
}
/**
* Interface for notification every request result
*/
public interface Listener {
/**
* 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);
}
/**
* Set notificaion listener
*
* @param listener
*/
public void setListener(Listener listener) {
this.listener = listener;
}
/**
* 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()));
Observable<StellarRequest.Base> stellarObserver = Observable.just(stellarRequest)
.doOnEach(stellarRequest1 -> doStellarRequest(ctx, stellarRequest))
.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) || (throwable instanceof ErrorResponse))
.zipWith(Observable.range(1, 4), (n, i) -> i))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
stellarObserver.subscribe(new DefaultObserver<StellarRequest.Base>() {
@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));
stellarRequest.setError(ctx.getString(R.string.cannot_obtain_data_from_blockchain));
//setErrorOccurred(e.getMessage());//;
listener.onFail(stellarRequest);
}
/**
* Called after completion request processing
*/
@Override
public void onComplete() {
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");
listener.onFail(stellarRequest);
} else {
LOG.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error==null");
listener.onSuccess(stellarRequest);
}
}
});
}
private void doStellarRequest(TangemContext ctx, StellarRequest.Base stellarRequest) throws IOException {
stellarRequest.setError(null);
try {
Server server;
if (ctx.getBlockchain() == Blockchain.Stellar) {
Network.usePublicNetwork();
server = new Server(ServerURL.API_STELLAR);
} else if (ctx.getBlockchain() == Blockchain.StellarTestNet) {
Network.useTestNetwork();
server = new Server(ServerURL.API_STELLAR_TESTNET);
} else {
throw new IOException("Wrong blockchain for ServerApiStellar");
}
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.Companion.getInstance().getString(R.string.cannot_obtain_data_from_blockchain_communication_error));
throw e;
}
}
}

View file

@ -12,4 +12,6 @@ class ServerURL {
static final String API_BINANCE = "https://dex.binance.org/";
static final String API_BINANCE_TESTNET = "https://testnet-dex.binance.org/";
static final String API_MATIC_TESTNET = "https://testnet2.matic.network";
static final String API_STELLAR = "https://horizon.stellar.org/";
static final String API_STELLAR_TESTNET = "https://horizon-testnet.stellar.org";
}

View file

@ -0,0 +1,84 @@
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.LedgerResponse;
import org.stellar.sdk.responses.SubmitTransactionResponse;
import java.io.IOException;
/**
* 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;
}
public void setError(String error) {
this.error = error;
}
public abstract void process(Server server) throws IOException;
}
public static class Balance extends Base {
KeyPair accountKeyPair;
public AccountResponse accountResponse;
public Balance(String walletAddress) {
accountKeyPair = KeyPair.fromAccountId(walletAddress);
}
@Override
public void process(Server server) throws IOException {
accountResponse = server.accounts().account(accountKeyPair);
}
}
public static class SubmitTransaction extends Base {
public Transaction transaction;
public SubmitTransactionResponse response;
public SubmitTransaction(Transaction transaction) {
this.transaction = transaction;
}
@Override
public void process(Server server) throws IOException {
// // First, check to make sure that the destination account exists.
// // You could skip this, but if the account does not exist, you will be charged
// // the transaction fee when the transaction fails.
// // It will throw HttpResponseException if account does not exist or there was another error.
// server.accounts().account(targetAccount);
//
// // If there was no error, load up-to-date information on your account.
// AccountResponse sourceAccount = server.accounts().account(sourceAccount);
// And finally, send it off to Stellar!
response = server.submitTransaction(transaction);
}
}
public static class Ledgers extends Base {
public LedgerResponse ledgerResponse;
public Ledgers() {};
@Override
public void process(Server server) throws IOException {
int latestLedger = server.root().getCoreLatestLedger();
ledgerResponse = server.ledgers().ledger(latestLedger);
}
}
}