Exchange balances pulled from earn (if enabled)

This commit is contained in:
Nicolás Sánchez 2025-01-08 21:43:45 -03:00
parent 39b6ec0d35
commit be7a85664a
1 changed files with 41 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -26,6 +27,7 @@ import okhttp3.Response;
public class InstanceInterface { public class InstanceInterface {
private static final String API_BASE_URL = globalSettings.apiUrl; private static final String API_BASE_URL = globalSettings.apiUrl;
private static final String API_KEY = globalSettings.apiKey; private static final String API_KEY = globalSettings.apiKey;
private static final String EARN_API_KEY = globalSettings.earnApiKey;
private static final OkHttpClient httpClient = new OkHttpClient(); private static final OkHttpClient httpClient = new OkHttpClient();
public static ProfitStatsData getProfitStatsData(boolean retry) throws IOException { public static ProfitStatsData getProfitStatsData(boolean retry) throws IOException {
@ -443,7 +445,11 @@ public class InstanceInterface {
}); });
CompletableFuture<Double> fundsAvailableFuture = CompletableFuture.supplyAsync(() -> { CompletableFuture<Double> fundsAvailableFuture = CompletableFuture.supplyAsync(() -> {
try { try {
return getFundsAvailable(exchange,"USDT", true); if (globalSettings.useEarn) {
return getFundsAvailableWithEarn(exchange,"USDT", true);
} else {
return getFundsAvailable(exchange,"USDT", true);
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(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 { private static double getFundsAvailable(String exchange, String coin, boolean retry) throws IOException {
Request fundsRequest = new Request.Builder() Request fundsRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/get_balance?coin=" + coin) .url(API_BASE_URL + "/" + exchange + "/get_balance?coin=" + coin)