WorkerInterface methods mostly populated

This commit is contained in:
Nicolás Sánchez 2024-12-19 16:43:50 -03:00
parent 1803b09126
commit d812811ed5
2 changed files with 310 additions and 26 deletions

View File

@ -407,7 +407,8 @@ public class InstanceInterface {
try {
boolean isShort = value.has("is_short") && value.get("is_short").isJsonPrimitive() && value.get("is_short").getAsBoolean();
boolean isBoosted = value.has("is_boosted") && value.get("is_boosted").isJsonPrimitive() && value.get("is_boosted").getAsBoolean();
boolean isAutoSwitchEnabled = value.has("is_auto_switch_enabled") && value.get("is_auto_switch_enabled").isJsonPrimitive() && value.get("is_auto_switch_enabled").getAsBoolean();
boolean isAutoSwitchEnabled = value.has("autoswitch") && value.get("autoswitch").isJsonPrimitive() && value.get("autoswitch").getAsBoolean();
boolean isPaused = value.has("is_paused") && value.get("is_paused").isJsonPrimitive() && value.get("is_paused").getAsBoolean();
boolean stopWhenProfit = value.has("stop_when_profit") && value.get("stop_when_profit").isJsonPrimitive() && value.get("stop_when_profit").getAsBoolean();
double orderSize = value.has("order_size") && value.get("order_size").isJsonPrimitive() ? value.get("order_size").getAsDouble() : 0.0;
double quoteSpent = value.has("quote_spent") && value.get("quote_spent").isJsonPrimitive() ? value.get("quote_spent").getAsDouble() : 0.0;
@ -425,10 +426,8 @@ public class InstanceInterface {
double takeProfitPrice = value.has("take_profit_price") && value.get("take_profit_price").isJsonPrimitive() ? value.get("take_profit_price").getAsDouble() : 0.0;
double nextSoPrice = value.has("next_so_price") && value.get("next_so_price").isJsonPrimitive() ? value.get("next_so_price").getAsDouble() : 0.0;
String tpOrderId = value.has("tp_order_id") && value.get("tp_order_id").isJsonPrimitive() ? value.get("tp_order_id").getAsString() : null;
//String takeProfitOrder = value.has("take_profit_order") && value.get("take_profit_order").isJsonPrimitive() ? value.get("take_profit_order").getAsString() : null;
JsonObject takeProfitOrder = value.get("take_profit_order").getAsJsonObject();
String safetyOrderId = value.has("safety_order_id") && value.get("safety_order_id").isJsonPrimitive() ? value.get("safety_order_id").getAsString() : null;
//String safetyOrder = value.has("safety_order") && value.get("safety_order").isJsonPrimitive() ? value.get("safety_order").getAsString() : null;
JsonObject safetyOrder = value.get("safety_order").getAsJsonObject();
double feesPaidInBase = value.has("fees_paid_in_base") && value.get("fees_paid_in_base").isJsonPrimitive() ? value.get("fees_paid_in_base").getAsDouble() : 0.0;
double feesPaidInQuote = value.has("fees_paid_in_quote") && value.get("fees_paid_in_quote").isJsonPrimitive() ? value.get("fees_paid_in_quote").getAsDouble() : 0.0;
@ -454,6 +453,7 @@ public class InstanceInterface {
key,
isShort,
isBoosted,
isPaused,
isAutoSwitchEnabled,
stopWhenProfit,
orderSize,
@ -621,7 +621,7 @@ public class InstanceInterface {
workerStatsData.getIsBoosted(),
workerStatsData.getAutoSwitchEnabled(),
//Let's deal with this later
false,
workerStatsData.getIsPaused(),
workerStatsData.getStopWhenProfit(),
workerStatsData.getOldLongDictionary());
workerDataList.add(workerData);
@ -790,6 +790,7 @@ public class InstanceInterface {
private final String pair;
private final boolean isShort;
private final boolean isBoosted;
private final boolean isPaused;
private final boolean autoSwitchEnabled;
private final boolean stopWhenProfit;
private final double orderSize;
@ -821,10 +822,11 @@ public class InstanceInterface {
private final OldLongDictionary oldLongDictionary; //Change type
private final String statusString;
public WorkerStatsData(String pair, boolean isShort, boolean isBoosted, boolean autoSwitchEnabled, boolean stopWhenProfit, double orderSize, double quoteSpent, double baseBought, int soAmount, int numberOfSafetyOrders, int tpMode, String profitTable, double startTime, double startPrice, double dealStartTime, double dealUptime, double totalUptime, double price, double takeProfitPrice, double nextSoPrice, String tpOrderId, JsonObject takeProfitOrder, String soOrderId, JsonObject safetyOrder, double feesPaidInBase, double feesPaidInQuote, double partialProfit, String safetyPriceTable, String dealOrderHistory, String pauseReason, OldLongDictionary oldLong, String statusString) {
public WorkerStatsData(String pair, boolean isShort, boolean isBoosted, boolean isPaused, boolean autoSwitchEnabled, boolean stopWhenProfit, double orderSize, double quoteSpent, double baseBought, int soAmount, int numberOfSafetyOrders, int tpMode, String profitTable, double startTime, double startPrice, double dealStartTime, double dealUptime, double totalUptime, double price, double takeProfitPrice, double nextSoPrice, String tpOrderId, JsonObject takeProfitOrder, String soOrderId, JsonObject safetyOrder, double feesPaidInBase, double feesPaidInQuote, double partialProfit, String safetyPriceTable, String dealOrderHistory, String pauseReason, OldLongDictionary oldLong, String statusString) {
this.pair = pair;
this.isShort = isShort;
this.isBoosted = isBoosted;
this.isPaused = isPaused;
this.autoSwitchEnabled = autoSwitchEnabled;
this.stopWhenProfit = stopWhenProfit;
this.orderSize = orderSize;
@ -888,6 +890,7 @@ public class InstanceInterface {
public String getPauseReason() { return pauseReason; }
public OldLongDictionary getOldLongDictionary() { return oldLongDictionary; }
public String getStatusString() { return statusString; }
public boolean getIsPaused() { return isPaused; }
}
public static class Order {

View File

@ -3,17 +3,15 @@ package com.example.dcav2gui;
import static com.example.dcav2gui.MainActivity.globalSettings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class WorkerInterface {
@ -57,7 +55,8 @@ public class WorkerInterface {
try {
boolean isShort = value.has("is_short") && value.get("is_short").isJsonPrimitive() && value.get("is_short").getAsBoolean();
boolean isBoosted = value.has("is_boosted") && value.get("is_boosted").isJsonPrimitive() && value.get("is_boosted").getAsBoolean();
boolean isAutoSwitchEnabled = value.has("is_auto_switch_enabled") && value.get("is_auto_switch_enabled").isJsonPrimitive() && value.get("is_auto_switch_enabled").getAsBoolean();
boolean isPaused = value.has("is_paused") && value.get("is_paused").isJsonPrimitive() && value.get("is_paused").getAsBoolean();
boolean isAutoSwitchEnabled = value.has("autoswitch") && value.get("autoswitch").isJsonPrimitive() && value.get("autoswitch").getAsBoolean();
boolean stopWhenProfit = value.has("stop_when_profit") && value.get("stop_when_profit").isJsonPrimitive() && value.get("stop_when_profit").getAsBoolean();
double orderSize = value.has("order_size") && value.get("order_size").isJsonPrimitive() ? value.get("order_size").getAsDouble() : 0.0;
double quoteSpent = value.has("quote_spent") && value.get("quote_spent").isJsonPrimitive() ? value.get("quote_spent").getAsDouble() : 0.0;
@ -102,6 +101,7 @@ public class WorkerInterface {
pair,
isShort,
isBoosted,
isPaused,
isAutoSwitchEnabled,
stopWhenProfit,
orderSize,
@ -138,37 +138,318 @@ public class WorkerInterface {
return valueToReturn;
}
public static JsonObject addTrader(String exchange, String pair) throws IOException {
public static JsonObject addTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request addTraderRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/add_pair")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response addTraderResponse = httpClient.newCall(addTraderRequest).execute()) {
if (!addTraderResponse.isSuccessful()) {
if (addTraderResponse.code() == 503 && retry) {
return addTrader(exchange, pair, false);
}
throw new IOException("Unexpected code " + addTraderResponse);
}
String addTraderResponseBody = addTraderResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(addTraderResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
public static JsonObject removeTrader(String exchange, String pair) throws IOException {
return null;
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
public static JsonObject restartTrader(String exchange, String pair) throws IOException {
return null;
//If no error, the response is {"Success":"Pair added"}
return jsonObject;
}
}
public static JsonObject importTrader(String exchange, String pair) throws IOException {
public static JsonObject removeTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request removeTraderRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/add_pair")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response removeTraderResponse = httpClient.newCall(removeTraderRequest).execute()) {
if (!removeTraderResponse.isSuccessful()) {
if (removeTraderResponse.code() == 503 && retry) {
return removeTrader(exchange, pair, false);
}
throw new IOException("Unexpected code " + removeTraderResponse);
}
String removeTraderResponseBody = removeTraderResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(removeTraderResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
public static JsonObject togglePause(String exchange, String pair) throws IOException {
return null;
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
public static JsonObject toggleAutoswitch(String exchange, String pair) throws IOException {
return null;
//If no error, the response is {"Success":"Pair to be removed"}
return jsonObject;
}
}
public static JsonObject toggleCleanup(String exchange, String pair) throws IOException {
public static JsonObject restartTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request restartTraderRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/add_pair")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response restartTraderResponse = httpClient.newCall(restartTraderRequest).execute()) {
if (!restartTraderResponse.isSuccessful()) {
if (restartTraderResponse.code() == 503 && retry) {
return restartTrader(exchange, pair, false);
}
throw new IOException("Unexpected code " + restartTraderResponse);
}
String restartTraderResponseBody = restartTraderResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(restartTraderResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
public static JsonObject toggleLastCall(String exchange, String pair) throws IOException {
//If no error, the response is {"Success":"Pair restarted"}
return jsonObject;
}
}
public static JsonObject importTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request importTraderRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/add_pair")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response importTraderResponse = httpClient.newCall(importTraderRequest).execute()) {
if (!importTraderResponse.isSuccessful()) {
if (importTraderResponse.code() == 503 && retry) {
return importTrader(exchange, pair, false);
}
throw new IOException("Unexpected code " + importTraderResponse);
}
String importTraderResponseBody = importTraderResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(importTraderResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
//If no error, the response is {"Success":"Pair imported successfully"}
return jsonObject;
}
}
public static JsonObject togglePause(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request togglePauseRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/toggle_pause")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response togglePauseResponse = httpClient.newCall(togglePauseRequest).execute()) {
if (!togglePauseResponse.isSuccessful()) {
if (togglePauseResponse.code() == 503 && retry) {
return togglePause(exchange, pair, false);
}
throw new IOException("Unexpected code " + togglePauseResponse);
}
String togglePauseResponseBody = togglePauseResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(togglePauseResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
//If no error, the response is {"Success":"Trader will be paused"} or {"Success":"Trader will be resumed"}
return jsonObject;
}
}
public static JsonObject toggleAutoswitch(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request toggleAutoswitchRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/toggle_autoswitch")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response toggleAutoswitchResponse = httpClient.newCall(toggleAutoswitchRequest).execute()) {
if (!toggleAutoswitchResponse.isSuccessful()) {
if (toggleAutoswitchResponse.code() == 503 && retry) {
return toggleAutoswitch(exchange, pair, false);
}
throw new IOException("Unexpected code " + toggleAutoswitchResponse);
}
String toggleAutoswitchResponseBody = toggleAutoswitchResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(toggleAutoswitchResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
//If no error, the response is {"Success":"Autoswitch is now ON"} or {"Success":"Autoswitch is now OFF"}
return jsonObject;
}
}
public static JsonObject toggleCleanup(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request toggleCleanupRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/toggle_cleanup")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response toggleCleanupResponse = httpClient.newCall(toggleCleanupRequest).execute()) {
if (!toggleCleanupResponse.isSuccessful()) {
if (toggleCleanupResponse.code() == 503 && retry) {
return toggleCleanup(exchange, pair, false);
}
throw new IOException("Unexpected code " + toggleCleanupResponse);
}
String toggleCleanupResponseBody = toggleCleanupResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(toggleCleanupResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
//If no error, the response is {"Success":"Cleanup turned ON"} or {"Success":"Cleanup turned OFF"}
return jsonObject;
}
}
public static JsonObject toggleLastCall(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("base", base);
formBuilder.add("quote", quote);
RequestBody formBody = formBuilder.build();
Request toggleLastCallRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/last_call")
.header("X-API-KEY", API_KEY)
.post(formBody)
.build();
try (Response toggleLastCallResponse = httpClient.newCall(toggleLastCallRequest).execute()) {
if (!toggleLastCallResponse.isSuccessful()) {
if (toggleLastCallResponse.code() == 503 && retry) {
return toggleLastCall(exchange, pair, false);
}
throw new IOException("Unexpected code " + toggleLastCallResponse);
}
String toggleLastCallResponseBody = toggleLastCallResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(toggleLastCallResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
//If no error, the response is {"Success":"Trader scheduled to go offline when profit is reached"} or {"Success":"Last call cancelled"}
return jsonObject;
}
}
public static JsonObject switchToLong(String exchange, String pair) throws IOException {
return null;