use settleAmount (#768)

This commit is contained in:
m2049r 2021-05-21 08:45:45 +02:00 committed by GitHub
parent 2c2a5314d4
commit c4958f6c54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 51 deletions

View File

@ -349,7 +349,7 @@ public class SendAddressWizardFragment extends SendWizardFragment {
private boolean checkAddress() { private boolean checkAddress() {
boolean ok = checkAddressNoError(); boolean ok = checkAddressNoError();
if (!ok) { if (possibleCryptos.isEmpty()) {
etAddress.setError(getString(R.string.send_address_invalid)); etAddress.setError(getString(R.string.send_address_invalid));
} else { } else {
etAddress.setError(null); etAddress.setError(null);

View File

@ -90,7 +90,6 @@ public class SendBtcAmountWizardFragment extends SendWizardFragment {
return view; return view;
} }
@Override @Override
public boolean onValidateFields() { public boolean onValidateFields() {
Timber.i(maxBtc + "/" + minBtc); Timber.i(maxBtc + "/" + minBtc);

View File

@ -359,40 +359,22 @@ public class SendBtcConfirmWizardFragment extends SendWizardFragment implements
} }
private RequestQuote xmrtoQuote = null; private RequestQuote xmrtoQuote = null;
private int stageARetries = 0;
private final int RETRIES = 3;
private double stageAPrice = 0;
private void processStageA(final RequestQuote requestQuote) { private void processStageA(final RequestQuote requestQuote) {
Timber.d("processCreateOrder %s", requestQuote.getId()); Timber.d("processCreateOrder %s", requestQuote.getId());
TxDataBtc txDataBtc = (TxDataBtc) sendListener.getTxData(); 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 (requestQuote.getBtcAmount() != txDataBtc.getBtcAmount()) {
if (--stageARetries <= 0) {
Timber.d("Failed to get quote"); Timber.d("Failed to get quote");
getView().post(() -> getView().post(() -> showStageError(ShiftError.Error.SERVICE.toString(),
showStageError(ShiftError.Error.SERVICE.toString(),
getString(R.string.shift_noquote), getString(R.string.shift_noquote),
getString(R.string.shift_checkamount))); getString(R.string.shift_checkamount)));
return; // just stop for now 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
}
xmrtoQuote = requestQuote; xmrtoQuote = requestQuote;
txDataBtc.setAmount(xmrtoQuote.getXmrAmount());
getView().post(() -> { getView().post(() -> {
// show data from the actual quote as that is what is used to
NumberFormat df = NumberFormat.getInstance(Locale.US); NumberFormat df = NumberFormat.getInstance(Locale.US);
df.setMaximumFractionDigits(12); df.setMaximumFractionDigits(12);
final String btcAmount = df.format(xmrtoQuote.getBtcAmount()); final String btcAmount = df.format(xmrtoQuote.getBtcAmount());
@ -438,18 +420,12 @@ public class SendBtcConfirmWizardFragment extends SendWizardFragment implements
} }
private void stageA() { private void stageA() {
stageARetries = RETRIES;
stageAOneShot();
}
private void stageAOneShot() {
if (!isResumed) return; if (!isResumed) return;
Timber.d("Request Quote"); Timber.d("Request Quote");
xmrtoQuote = null; xmrtoQuote = null;
xmrtoOrder = null; xmrtoOrder = null;
showProgress(1, getString(R.string.label_send_progress_xmrto_create)); showProgress(1, getString(R.string.label_send_progress_xmrto_create));
TxDataBtc txDataBtc = (TxDataBtc) sendListener.getTxData(); TxDataBtc txDataBtc = (TxDataBtc) sendListener.getTxData();
stageAPrice = 0;
ShiftCallback<RequestQuote> callback = new ShiftCallback<RequestQuote>() { ShiftCallback<RequestQuote> callback = new ShiftCallback<RequestQuote>() {
@Override @Override
@ -473,7 +449,7 @@ public class SendBtcConfirmWizardFragment extends SendWizardFragment implements
} }
}; };
getXmrToApi().requestQuote(txDataBtc.getAmountAsDouble(), callback); getXmrToApi().requestQuote(txDataBtc.getBtcAmount(), callback);
} }
private CreateOrder xmrtoOrder = null; private CreateOrder xmrtoOrder = null;

View File

@ -74,10 +74,10 @@ class RequestQuoteImpl implements RequestQuote {
price = jsonObject.getDouble("rate"); 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<RequestQuote> callback) { @NonNull final ShiftCallback<RequestQuote> callback) {
try { try {
final JSONObject request = createRequest(xmrAmount); final JSONObject request = createRequest(btcAmount);
api.call("quotes", request, new NetworkCallback() { api.call("quotes", request, new NetworkCallback() {
@Override @Override
public void onSuccess(JSONObject jsonObject) { public void onSuccess(JSONObject jsonObject) {
@ -104,13 +104,13 @@ class RequestQuoteImpl implements RequestQuote {
* @param xmrAmount how much XMR to shift to BTC * @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(); final JSONObject jsonObject = new JSONObject();
jsonObject.put("depositMethod", "xmr"); jsonObject.put("depositMethod", "xmr");
jsonObject.put("settleMethod", ServiceHelper.ASSET); jsonObject.put("settleMethod", ServiceHelper.ASSET);
// #sideshift is silly and likes numbers as strings // #sideshift is silly and likes numbers as strings
String amount = AmountFormatter.format(xmrAmount); String amount = AmountFormatter.format(btcAmount);
jsonObject.put("depositAmount", amount); jsonObject.put("settleAmount", amount);
return jsonObject; return jsonObject;
} }

View File

@ -64,8 +64,8 @@ public class SideShiftApiImpl implements SideShiftApi, ShiftApiCall {
} }
@Override @Override
public void requestQuote(final double xmrAmount, @NonNull final ShiftCallback<RequestQuote> callback) { public void requestQuote(final double btcAmount, @NonNull final ShiftCallback<RequestQuote> callback) {
RequestQuoteImpl.call(this, xmrAmount, callback); RequestQuoteImpl.call(this, btcAmount, callback);
} }
@Override @Override

View File

@ -95,7 +95,7 @@ public class SideShiftApiRequestQuoteTest {
@Test @Test
public void requestQuote_shouldContainValidBody() throws InterruptedException { 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); xmrToApi.requestQuote(1.01, mockXmrToCallback);
RecordedRequest request = mockWebServer.takeRequest(); RecordedRequest request = mockWebServer.takeRequest();
@ -106,18 +106,18 @@ public class SideShiftApiRequestQuoteTest {
@Test @Test
public void requestQuote_wasSuccessfulShouldRespondWithQuote() public void requestQuote_wasSuccessfulShouldRespondWithQuote()
throws TimeoutException { throws TimeoutException {
final double xmrAmount = 1.01; final double btcAmount = 1.01;
final double rate = 0.00397838; final double rate = 0.00397838;
final String uuid = "66fc0749-f320-4361-b0fb-7873576cba67"; final String uuid = "66fc0749-f320-4361-b0fb-7873576cba67";
MockResponse jsonMockResponse = new MockResponse().setBody( MockResponse jsonMockResponse = new MockResponse().setBody(
createMockRequestQuoteResponse(xmrAmount, rate, uuid)); createMockRequestQuoteResponse(btcAmount, rate, uuid));
mockWebServer.enqueue(jsonMockResponse); mockWebServer.enqueue(jsonMockResponse);
xmrToApi.requestQuote(xmrAmount, new ShiftCallback<RequestQuote>() { xmrToApi.requestQuote(btcAmount, new ShiftCallback<RequestQuote>() {
@Override @Override
public void onSuccess(final RequestQuote quote) { public void onSuccess(final RequestQuote quote) {
waiter.assertEquals(quote.getBtcAmount(), xmrAmount * rate); waiter.assertEquals(quote.getXmrAmount(), btcAmount / rate);
waiter.assertEquals(quote.getXmrAmount(), xmrAmount); waiter.assertEquals(quote.getBtcAmount(), btcAmount);
waiter.assertEquals(quote.getId(), uuid); waiter.assertEquals(quote.getId(), uuid);
waiter.resume(); waiter.resume();
} }
@ -181,17 +181,17 @@ public class SideShiftApiRequestQuoteTest {
waiter.await(); waiter.await();
} }
private String createMockRequestQuoteResponse(final double xmrAmount, final double rate, private String createMockRequestQuoteResponse(final double btcAmount, final double rate,
final String uuid) { final String uuid) {
return "{\n" + return "{\n" +
"\"createdAt\":\"2021-02-04T13:09:14.484Z\",\n" + "\"createdAt\":\"2021-02-04T13:09:14.484Z\",\n" +
"\"depositAmount\":\"" + xmrAmount + "\",\n" + "\"settleAmount\":\"" + btcAmount + "\",\n" +
"\"depositMethod\":\"xmr\",\n" + "\"depositMethod\":\"xmr\",\n" +
"\"expiresAt\":\"2021-02-04T13:24:14.484Z\",\n" + "\"expiresAt\":\"2021-02-04T13:24:14.484Z\",\n" +
"\"id\":\"" + uuid + "\",\n" + "\"id\":\"" + uuid + "\",\n" +
"\"rate\":\"" + rate + "\",\n" + "\"rate\":\"" + rate + "\",\n" +
"\"settleAmount\":\"" + (xmrAmount * rate) + "\",\n" + "\"depositAmount\":\"" + (btcAmount / rate) + "\",\n" +
"\"settleMethod\":\"btc\"\n" + "\"settleMethod\":\"btc\"\n" +
"}"; "}";
} }