From cc12aaf5939db8ddf73de6dabe72aef369b761d8 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 2 Oct 2019 15:50:20 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/data/network/ServerApiStellar.java | 10 ++-- .../java/com/tangem/wallet/xlm/XlmData.java | 16 +++++- .../java/com/tangem/wallet/xlm/XlmEngine.java | 50 ++++++++++++------- app/src/main/res/values/strings.xml | 2 + 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/tangem/data/network/ServerApiStellar.java b/app/src/main/java/com/tangem/data/network/ServerApiStellar.java index b63a42274e..9882a26902 100644 --- a/app/src/main/java/com/tangem/data/network/ServerApiStellar.java +++ b/app/src/main/java/com/tangem/data/network/ServerApiStellar.java @@ -117,9 +117,9 @@ public class ServerApiStellar { return Observable.just(stellarRequest1); } ) - .retryWhen(errors -> errors - .filter(throwable -> (throwable instanceof IOException) || (throwable instanceof ErrorResponse)) - .zipWith(Observable.range(1, 4), (n, i) -> i)) +// .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()); @@ -136,8 +136,8 @@ public class ServerApiStellar { LOG.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onError " + e.getMessage()); LOG.e(TAG, String.format("%d requests left in processing", requestsCount)); - if (isRetry) { - stellarRequest.setError(ctx.getString(R.string.loaded_wallet_error_obtaining_blockchain_data)); + if (isRetry || stellarRequest.errorResponse.getCode() == 404) { + stellarRequest.setError(e.getMessage()); //setErrorOccurred(e.getMessage());//; listener.onFail(stellarRequest); } else { diff --git a/app/src/main/java/com/tangem/wallet/xlm/XlmData.java b/app/src/main/java/com/tangem/wallet/xlm/XlmData.java index b956759278..9eab0764f8 100644 --- a/app/src/main/java/com/tangem/wallet/xlm/XlmData.java +++ b/app/src/main/java/com/tangem/wallet/xlm/XlmData.java @@ -30,13 +30,14 @@ public class XlmData extends CoinData { private Long sequenceNumber = 0L; private CoinEngine.Amount baseReserve = new CoinEngine.Amount("0.5", "XLM"); private CoinEngine.Amount baseFee = new CoinEngine.Amount("0.00001", "XLM"); - private boolean error404 = false; + private boolean error404, targetAccountCreated = false; @Override public void clearInfo() { super.clearInfo(); balance = null; error404 = false; + targetAccountCreated = false; } CoinEngine.Amount getBalance() { @@ -86,6 +87,14 @@ public class XlmData extends CoinData { this.error404 = error404; } + public boolean isTargetAccountCreated() { + return targetAccountCreated; + } + + public void setTargetAccountCreated(boolean targetAccountCreated) { + this.targetAccountCreated = targetAccountCreated; + } + @Override public void loadFromBundle(Bundle B) { super.loadFromBundle(B); @@ -116,6 +125,9 @@ public class XlmData extends CoinData { if (B.containsKey("Error404")) error404 = B.getBoolean("Error404"); else error404 = false; + + if (B.containsKey("TargetAccountCreated")) targetAccountCreated = B.getBoolean("TargetAccountCreated"); + else targetAccountCreated = false; } @Override @@ -143,6 +155,8 @@ public class XlmData extends CoinData { if (error404) B.putBoolean("Error404", true); + if (targetAccountCreated) B.putBoolean("TargetAccountCreated", true); + } catch (Exception e) { Log.e("Can't save to bundle ", e.getMessage()); } diff --git a/app/src/main/java/com/tangem/wallet/xlm/XlmEngine.java b/app/src/main/java/com/tangem/wallet/xlm/XlmEngine.java index f22bdc5c2f..717d3d85bf 100644 --- a/app/src/main/java/com/tangem/wallet/xlm/XlmEngine.java +++ b/app/src/main/java/com/tangem/wallet/xlm/XlmEngine.java @@ -361,7 +361,7 @@ public class XlmEngine extends CoinEngine { } Operation operation; - if (isAccountCreated(targetAddress)) + if (coinData.isTargetAccountCreated()) operation = new PaymentOperation.Builder(KeyPair.fromAccountId(targetAddress), new AssetTypeNative(), amountValue.toValueString()).build(); else operation = new CreateAccountOperation.Builder(KeyPair.fromAccountId(targetAddress), amountValue.toValueString()).build(); @@ -414,24 +414,41 @@ public class XlmEngine extends CoinEngine { }; } - - // network call inside, don't use on main thread - private boolean isAccountCreated(String address) { + private void checkTargetAccountCreated(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) { final ServerApiStellar serverApi = new ServerApiStellar(ctx.getBlockchain()); - StellarRequest.Balance request = new StellarRequest.Balance(address); + ServerApiStellar.Listener listener = new ServerApiStellar.Listener() { + @Override + public void onSuccess(StellarRequest.Base request) { + coinData.setTargetAccountCreated(true); + blockchainRequestsCallbacks.onComplete(true); + } - try { - serverApi.doStellarRequest(ctx, request); - } catch (IOException e) { - Log.e(TAG, e.getMessage()); - return true; // suppose account is created if anything goes wrong TODO:check - } - if (request.errorResponse != null && request.errorResponse.getCode() == 404) - return false; - else - return true; + @Override + public void onFail(StellarRequest.Base request) { + Log.i(TAG, "onFail: " + request.getClass().getSimpleName() + " " + request.getError()); + + if (request.errorResponse.getCode() == 404) { + coinData.setTargetAccountCreated(false); + + if (amount.compareTo(coinData.getReserve()) >= 0) { //TODO: take fee inclusion in account, now 1 XLM with fee included will fail after transaction is sent + blockchainRequestsCallbacks.onComplete(true); + } else { + ctx.setError(R.string.confirm_transaction_error_not_enough_xlm_for_create); + blockchainRequestsCallbacks.onComplete(false); + } + } else { // suppose account is created if anything goes wrong + coinData.setTargetAccountCreated(true); + blockchainRequestsCallbacks.onComplete(true); + } + } + }; + + serverApi.setListener(listener); + + serverApi.requestData(ctx, new StellarRequest.Balance(targetAddress)); + } @Override @@ -501,9 +518,8 @@ public class XlmEngine extends CoinEngine { @Override public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) throws Exception { - // TODO: get fee stats? coinData.minFee = coinData.normalFee = coinData.maxFee = coinData.getBaseFee(); - blockchainRequestsCallbacks.onComplete(true); + checkTargetAccountCreated(blockchainRequestsCallbacks, targetAddress, amount); //TODO: move? } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 24812c1f37..53a6ee35ff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -205,10 +205,12 @@ Please wait for confirmation of incoming transaction Not enough ETH funds for fee Not enough RBTC funds for fee + Target account is not created! Send 1+ XLM to create it PIN2 is required to sign the payment Service unavailable You have a risk of delaying transaction + Wallet hasn\'t been yet created Create Wallet