diff --git a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java index 7906770..f32690e 100644 --- a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java +++ b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import okhttp3.OkHttpClient; @@ -26,6 +27,7 @@ import okhttp3.Response; public class InstanceInterface { private static final String API_BASE_URL = globalSettings.apiUrl; private static final String API_KEY = globalSettings.apiKey; + private static final String EARN_API_KEY = globalSettings.earnApiKey; private static final OkHttpClient httpClient = new OkHttpClient(); public static ProfitStatsData getProfitStatsData(boolean retry) throws IOException { @@ -443,7 +445,11 @@ public class InstanceInterface { }); CompletableFuture fundsAvailableFuture = CompletableFuture.supplyAsync(() -> { try { - return getFundsAvailable(exchange,"USDT", true); + if (globalSettings.useEarn) { + return getFundsAvailableWithEarn(exchange,"USDT", true); + } else { + return getFundsAvailable(exchange,"USDT", true); + } } catch (IOException e) { throw new RuntimeException(e); } @@ -491,6 +497,40 @@ public class InstanceInterface { } + private static double getFundsAvailableWithEarn(String exchange, String coin, boolean retry) throws IOException { + String parsedBroker = exchange; + if (Objects.equals(parsedBroker, "okex")) { + parsedBroker = "okx"; + } + + Request fundsRequest = new Request.Builder() + .url(API_BASE_URL + "/earn/get_total_balance?broker=" + parsedBroker) + .header("X-API-KEY", EARN_API_KEY) + .build(); + + try (Response fundsResponse = httpClient.newCall(fundsRequest).execute()) { + if (!fundsResponse.isSuccessful()) { + if (fundsResponse.code() == 503 && retry) { + return getFundsAvailableWithEarn(exchange, coin, false); + } + throw new IOException("Unexpected code " + fundsResponse); + } + String fundsResponseBody = fundsResponse.body().string(); + JsonElement jsonElement = JsonParser.parseString(fundsResponseBody); + if (!jsonElement.isJsonObject()) { + System.err.println("The parsed JSON response is not a JsonObject."); + return 0.0; + } + JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (!jsonObject.has("trading_balance")) { + System.err.println("The parsed JSON response does not contain the balances requested."); + return 0.0; + } + + return jsonObject.get("trading_balance").getAsDouble() + jsonObject.get("earning_balance").getAsDouble(); + } + } + private static double getFundsAvailable(String exchange, String coin, boolean retry) throws IOException { Request fundsRequest = new Request.Builder() .url(API_BASE_URL + "/" + exchange + "/get_balance?coin=" + coin)