diff --git a/app/src/main/java/com/tangem/wallet/xrp/XrpData.java b/app/src/main/java/com/tangem/wallet/xrp/XrpData.java index 3df5e26532..1b10a221a3 100644 --- a/app/src/main/java/com/tangem/wallet/xrp/XrpData.java +++ b/app/src/main/java/com/tangem/wallet/xrp/XrpData.java @@ -17,7 +17,7 @@ public class XrpData extends CoinData { private Long reserve = 20000000L; - private Boolean accountNotFound = false; + private Boolean accountNotFound, targetAccountCreated = false; @Override public void loadFromBundle(Bundle B) { @@ -32,7 +32,9 @@ public class XrpData extends CoinData { if (B.containsKey("Reserve")) reserve = B.getLong("Reserve"); else reserve = 20000000L; if (B.containsKey("AccoundNotFound")) accountNotFound = B.getBoolean("AccoundNotFound"); - else reserve = 20000000L; + else accountNotFound = false; + if (B.containsKey("TargetAccountCreated")) targetAccountCreated = B.getBoolean("TargetAccountCreated"); + else targetAccountCreated = false; } @Override @@ -44,6 +46,7 @@ public class XrpData extends CoinData { if (sequence != null) B.putLong("Sequence", sequence); if (reserve != null) B.putLong("Reserve", reserve); if (accountNotFound != null) B.putBoolean("AccoundNotFound", accountNotFound); + if (targetAccountCreated != null) B.putBoolean("TargetAccountCreated", targetAccountCreated); } catch (Exception e) { Log.e("Can't save to bundle ", e.getMessage()); } @@ -57,6 +60,7 @@ public class XrpData extends CoinData { sequence = null; reserve = 20000000L; accountNotFound = false; + targetAccountCreated = false; } // balanceUnconfirmed is just the latest balance, it equals balanceConfirmed if no unconfirmed transaction present @@ -105,6 +109,14 @@ public class XrpData extends CoinData { this.accountNotFound = accountFound; } + public Boolean isTargetAccountCreated() { + return targetAccountCreated; + } + + public void setTargetAccountCreated(boolean targetAccountCreated) { + this.targetAccountCreated = targetAccountCreated; + } + public boolean hasBalanceInfo() { return balanceConfirmed != null || balanceUnconfirmed != null; } @@ -112,5 +124,4 @@ public class XrpData extends CoinData { public boolean hasUnconfirmed() { return !balanceConfirmed.equals(balanceUnconfirmed); } - } diff --git a/app/src/main/java/com/tangem/wallet/xrp/XrpEngine.java b/app/src/main/java/com/tangem/wallet/xrp/XrpEngine.java index c5bd2f5815..f6941d71fc 100644 --- a/app/src/main/java/com/tangem/wallet/xrp/XrpEngine.java +++ b/app/src/main/java/com/tangem/wallet/xrp/XrpEngine.java @@ -342,6 +342,11 @@ public class XrpEngine extends CoinEngine { public SignTask.TransactionToSign constructTransaction(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception { checkBlockchainDataExists(); + Amount reserve = convertToAmount(coinData.getReserveInInternalUnits()); + if (!coinData.isTargetAccountCreated() && amountValue.compareTo(reserve) < 0) { + throw new Exception("Target account is not created. Amount should be " + reserve.toDescriptionString(getDecimals()) + " or more"); + } + String amount, fee; if (IncFee) { @@ -522,19 +527,50 @@ public class XrpEngine extends CoinEngine { ServerApiRipple.ResponseListener rippleListener = new ServerApiRipple.ResponseListener() { @Override public void onSuccess(String method, RippleResponse rippleResponse) { - try { - InternalAmount minFee = new InternalAmount(Long.valueOf(rippleResponse.getResult().getDrops().getMinimum_fee()), "Drops"); - InternalAmount normalFee = new InternalAmount(Long.valueOf(rippleResponse.getResult().getDrops().getOpen_ledger_fee()), "Drops"); - InternalAmount maxFee = new InternalAmount(Long.valueOf(rippleResponse.getResult().getDrops().getMedian_fee()), "Drops"); + Log.i(TAG, "onSuccess: " + method); + switch (method) { + case ServerApiRipple.RIPPLE_ACCOUNT_INFO: { + try { + if (rippleResponse.getResult().getError_code().equals(19)) { // "Account not found" + coinData.setTargetAccountCreated(false); + } else { + coinData.setTargetAccountCreated(true); + } + } catch (Exception e) { + coinData.setTargetAccountCreated(true); //expected behaviour, if account exists, there should be no error code -> null pointer + } - coinData.minFee = convertToAmount(minFee); - coinData.normalFee = convertToAmount(normalFee); - coinData.maxFee = convertToAmount(maxFee); + if (serverApiRipple.isRequestsSequenceCompleted()) { + blockchainRequestsCallbacks.onComplete(!ctx.hasError()); + } else { + blockchainRequestsCallbacks.onProgress(); + } + } + break; - blockchainRequestsCallbacks.onComplete(true); - } catch (Exception e) { - e.printStackTrace(); - Log.e(TAG, "FAIL RIPPLE_FEE Exception"); + case ServerApiRipple.RIPPLE_FEE: { + try { + InternalAmount minFee = new InternalAmount(Long.valueOf(rippleResponse.getResult().getDrops().getMinimum_fee()), "Drops"); + InternalAmount normalFee = new InternalAmount(Long.valueOf(rippleResponse.getResult().getDrops().getOpen_ledger_fee()), "Drops"); + InternalAmount maxFee = new InternalAmount(Long.valueOf(rippleResponse.getResult().getDrops().getMedian_fee()), "Drops"); + + coinData.minFee = convertToAmount(minFee); + coinData.normalFee = convertToAmount(normalFee); + coinData.maxFee = convertToAmount(maxFee); + + } catch (Exception e) { + e.printStackTrace(); + Log.e(TAG, "FAIL RIPPLE_FEE Exception"); + ctx.setError(e.getMessage()); + } + + if (serverApiRipple.isRequestsSequenceCompleted()) { + blockchainRequestsCallbacks.onComplete(!ctx.hasError()); + } else { + blockchainRequestsCallbacks.onProgress(); + } + } + break; } } @@ -548,6 +584,7 @@ public class XrpEngine extends CoinEngine { serverApiRipple.setResponseListener(rippleListener); + serverApiRipple.requestData(ServerApiRipple.RIPPLE_ACCOUNT_INFO, targetAddress, ""); serverApiRipple.requestData(ServerApiRipple.RIPPLE_FEE, "", ""); }