From c4958f6c542420b15fd6b05419a6c2cd95828431 Mon Sep 17 00:00:00 2001 From: m2049r Date: Fri, 21 May 2021 08:45:45 +0200 Subject: [PATCH] use settleAmount (#768) --- .../send/SendAddressWizardFragment.java | 2 +- .../send/SendBtcAmountWizardFragment.java | 1 - .../send/SendBtcConfirmWizardFragment.java | 42 ++++--------------- .../sideshift/network/RequestQuoteImpl.java | 10 ++--- .../sideshift/network/SideShiftApiImpl.java | 4 +- .../SideShiftApiRequestQuoteTest.java | 18 ++++---- 6 files changed, 26 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendAddressWizardFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendAddressWizardFragment.java index 38feb6a906..56a12e3995 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendAddressWizardFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendAddressWizardFragment.java @@ -349,7 +349,7 @@ private boolean checkAddressNoError() { private boolean checkAddress() { boolean ok = checkAddressNoError(); - if (!ok) { + if (possibleCryptos.isEmpty()) { etAddress.setError(getString(R.string.send_address_invalid)); } else { etAddress.setError(null); diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcAmountWizardFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcAmountWizardFragment.java index 0a90c2a0e4..8f1616fb09 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcAmountWizardFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcAmountWizardFragment.java @@ -90,7 +90,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, return view; } - @Override public boolean onValidateFields() { Timber.i(maxBtc + "/" + minBtc); diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcConfirmWizardFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcConfirmWizardFragment.java index b552854f69..94d2e8e603 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcConfirmWizardFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendBtcConfirmWizardFragment.java @@ -359,40 +359,22 @@ SendFragment.Listener getActivityCallback() { } private RequestQuote xmrtoQuote = null; - private int stageARetries = 0; - private final int RETRIES = 3; - private double stageAPrice = 0; private void processStageA(final RequestQuote requestQuote) { Timber.d("processCreateOrder %s", requestQuote.getId()); TxDataBtc txDataBtc = (TxDataBtc) sendListener.getTxData(); - // verify the BTC amount is correct (price can change and we can only specify XMR amount) + // verify the BTC amount is correct if (requestQuote.getBtcAmount() != txDataBtc.getBtcAmount()) { - if (--stageARetries <= 0) { - Timber.d("Failed to get quote"); - getView().post(() -> - showStageError(ShiftError.Error.SERVICE.toString(), - getString(R.string.shift_noquote), - getString(R.string.shift_checkamount))); - return; // just stop for now - } - if (stageAPrice == requestQuote.getPrice()) { - // same price but different BTC amount - something else is wrong (e.g. too many decimals) - Timber.d("Price unchanged"); - getView().post(() -> - showStageError(ShiftError.Error.SERVICE.toString(), - getString(R.string.shift_noquote), - getString(R.string.shift_checkamount))); - return; // just stop for now - } - stageAPrice = requestQuote.getPrice(); - // recalc XMR and try again - txDataBtc.setAmount(txDataBtc.getBtcAmount() / requestQuote.getPrice()); - getView().post(this::stageAOneShot); - return; // stageA will run in the main thread + Timber.d("Failed to get quote"); + getView().post(() -> showStageError(ShiftError.Error.SERVICE.toString(), + getString(R.string.shift_noquote), + getString(R.string.shift_checkamount))); + return; // just stop for now } xmrtoQuote = requestQuote; + txDataBtc.setAmount(xmrtoQuote.getXmrAmount()); getView().post(() -> { + // show data from the actual quote as that is what is used to NumberFormat df = NumberFormat.getInstance(Locale.US); df.setMaximumFractionDigits(12); final String btcAmount = df.format(xmrtoQuote.getBtcAmount()); @@ -438,18 +420,12 @@ private void processStageAError(final Exception ex) { } private void stageA() { - stageARetries = RETRIES; - stageAOneShot(); - } - - private void stageAOneShot() { if (!isResumed) return; Timber.d("Request Quote"); xmrtoQuote = null; xmrtoOrder = null; showProgress(1, getString(R.string.label_send_progress_xmrto_create)); TxDataBtc txDataBtc = (TxDataBtc) sendListener.getTxData(); - stageAPrice = 0; ShiftCallback callback = new ShiftCallback() { @Override @@ -473,7 +449,7 @@ public void onError(Exception ex) { } }; - getXmrToApi().requestQuote(txDataBtc.getAmountAsDouble(), callback); + getXmrToApi().requestQuote(txDataBtc.getBtcAmount(), callback); } private CreateOrder xmrtoOrder = null; diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/RequestQuoteImpl.java b/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/RequestQuoteImpl.java index 6a1b80233b..c87465e6f4 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/RequestQuoteImpl.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/RequestQuoteImpl.java @@ -74,10 +74,10 @@ class RequestQuoteImpl implements RequestQuote { price = jsonObject.getDouble("rate"); } - public static void call(@NonNull final ShiftApiCall api, final double xmrAmount, + public static void call(@NonNull final ShiftApiCall api, final double btcAmount, @NonNull final ShiftCallback callback) { try { - final JSONObject request = createRequest(xmrAmount); + final JSONObject request = createRequest(btcAmount); api.call("quotes", request, new NetworkCallback() { @Override public void onSuccess(JSONObject jsonObject) { @@ -104,13 +104,13 @@ public void onError(Exception ex) { * @param xmrAmount how much XMR to shift to BTC */ - static JSONObject createRequest(final double xmrAmount) throws JSONException { + static JSONObject createRequest(final double btcAmount) throws JSONException { final JSONObject jsonObject = new JSONObject(); jsonObject.put("depositMethod", "xmr"); jsonObject.put("settleMethod", ServiceHelper.ASSET); // #sideshift is silly and likes numbers as strings - String amount = AmountFormatter.format(xmrAmount); - jsonObject.put("depositAmount", amount); + String amount = AmountFormatter.format(btcAmount); + jsonObject.put("settleAmount", amount); return jsonObject; } diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/SideShiftApiImpl.java b/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/SideShiftApiImpl.java index 6cec57438c..b27ae60e06 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/SideShiftApiImpl.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/shift/sideshift/network/SideShiftApiImpl.java @@ -64,8 +64,8 @@ public void queryOrderParameters(@NonNull final ShiftCallback callback) { - RequestQuoteImpl.call(this, xmrAmount, callback); + public void requestQuote(final double btcAmount, @NonNull final ShiftCallback callback) { + RequestQuoteImpl.call(this, btcAmount, callback); } @Override diff --git a/app/src/test/java/com/m2049r/xmrwallet/service/shift/sideshift/SideShiftApiRequestQuoteTest.java b/app/src/test/java/com/m2049r/xmrwallet/service/shift/sideshift/SideShiftApiRequestQuoteTest.java index e6980a86fd..d5a112ee43 100644 --- a/app/src/test/java/com/m2049r/xmrwallet/service/shift/sideshift/SideShiftApiRequestQuoteTest.java +++ b/app/src/test/java/com/m2049r/xmrwallet/service/shift/sideshift/SideShiftApiRequestQuoteTest.java @@ -95,7 +95,7 @@ public void requestQuote_shouldBeContentTypeJson() @Test public void requestQuote_shouldContainValidBody() throws InterruptedException { - final String validBody = "{\"depositAmount\":\"1.01\",\"settleMethod\":\"btc\",\"depositMethod\":\"xmr\"}"; + final String validBody = "{\"settleAmount\":\"1.01\",\"settleMethod\":\"btc\",\"depositMethod\":\"xmr\"}"; xmrToApi.requestQuote(1.01, mockXmrToCallback); RecordedRequest request = mockWebServer.takeRequest(); @@ -106,18 +106,18 @@ public void requestQuote_shouldContainValidBody() throws InterruptedException { @Test public void requestQuote_wasSuccessfulShouldRespondWithQuote() throws TimeoutException { - final double xmrAmount = 1.01; + final double btcAmount = 1.01; final double rate = 0.00397838; final String uuid = "66fc0749-f320-4361-b0fb-7873576cba67"; MockResponse jsonMockResponse = new MockResponse().setBody( - createMockRequestQuoteResponse(xmrAmount, rate, uuid)); + createMockRequestQuoteResponse(btcAmount, rate, uuid)); mockWebServer.enqueue(jsonMockResponse); - xmrToApi.requestQuote(xmrAmount, new ShiftCallback() { + xmrToApi.requestQuote(btcAmount, new ShiftCallback() { @Override public void onSuccess(final RequestQuote quote) { - waiter.assertEquals(quote.getBtcAmount(), xmrAmount * rate); - waiter.assertEquals(quote.getXmrAmount(), xmrAmount); + waiter.assertEquals(quote.getXmrAmount(), btcAmount / rate); + waiter.assertEquals(quote.getBtcAmount(), btcAmount); waiter.assertEquals(quote.getId(), uuid); waiter.resume(); } @@ -181,17 +181,17 @@ public void onError(final Exception e) { waiter.await(); } - private String createMockRequestQuoteResponse(final double xmrAmount, final double rate, + private String createMockRequestQuoteResponse(final double btcAmount, final double rate, final String uuid) { return "{\n" + "\"createdAt\":\"2021-02-04T13:09:14.484Z\",\n" + - "\"depositAmount\":\"" + xmrAmount + "\",\n" + + "\"settleAmount\":\"" + btcAmount + "\",\n" + "\"depositMethod\":\"xmr\",\n" + "\"expiresAt\":\"2021-02-04T13:24:14.484Z\",\n" + "\"id\":\"" + uuid + "\",\n" + "\"rate\":\"" + rate + "\",\n" + - "\"settleAmount\":\"" + (xmrAmount * rate) + "\",\n" + + "\"depositAmount\":\"" + (btcAmount / rate) + "\",\n" + "\"settleMethod\":\"btc\"\n" + "}"; }