diff --git a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java index 09ce043..0d4ef3d 100644 --- a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java +++ b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java @@ -2,7 +2,6 @@ package nl.komtek.gpi.controllers; import com.cf.data.map.poloniex.PoloniexDataMapper; import com.cf.data.model.poloniex.PoloniexChartData; -import com.cf.data.model.poloniex.PoloniexCompleteBalance; import com.cf.data.model.poloniex.PoloniexTradeHistory; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -136,10 +135,8 @@ public class GunbotProxyController { String key = request.getHeader("key"); market = gunbotProxyService.getMarket(key); } - String result = gunbotProxyService.getCompleteBalances(market); - result = hideDust(result); - return result; + return gunbotProxyService.getCompleteBalances(market); } @RequestMapping(value = "/tradingApi/**", params = "command=returnOpenOrders") @@ -202,16 +199,6 @@ public class GunbotProxyController { @RequestParam BigDecimal rate, @RequestParam BigDecimal amount) throws IOException { - boolean globalSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty("sellOnlyMode")); - boolean pairSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty(String.format("%s_sellOnlyMode", currencyPair))); - if (globalSellOnlyMode || pairSellOnlyMode) { - JsonObject jsonObject = new JsonObject(); - String message = String.format("You are not allowed to buy. Sell Only mode is active for %s", currencyPair); - jsonObject.addProperty("error", message); - logger.info(jsonObject.toString()); - return jsonObject.toString(); - } - String key = request.getHeader("key"); if (doubleBuyProtection || doubleBuyProtectionSeconds > 0) { return gunbotProxyService.buyOrderWithProtection(key, currencyPair, rate, amount); @@ -299,34 +286,4 @@ public class GunbotProxyController { } return jsonArray; } - - private String hideDust(String result) { - boolean hidedust = Boolean.parseBoolean(util.getConfigurationProperty("hideDust")); - if (!hidedust) { - return result; - } - JsonParser jsonParser = new JsonParser(); - JsonElement jElement = jsonParser.parse(result); - JsonObject jObject = jElement.getAsJsonObject(); - JsonObject filteredObject = new JsonObject(); - for (Map.Entry entry : jObject.entrySet()) { - JsonElement element = (JsonElement) entry.getValue(); - BigDecimal available = BigDecimal.valueOf(element.getAsJsonObject().get("available").getAsDouble()); - BigDecimal onOrders = BigDecimal.valueOf(element.getAsJsonObject().get("onOrders").getAsDouble()); - BigDecimal btcValue = BigDecimal.valueOf(element.getAsJsonObject().get("btcValue").getAsDouble()); - - if (available.doubleValue() == 0) { - filteredObject.add(entry.getKey().toString(), element); - } - - double approximatePrice = btcValue.doubleValue() / available.add(onOrders).doubleValue(); - double availableValue = available.doubleValue() * approximatePrice; - if (availableValue < 0.00015) { - available = BigDecimal.ZERO; - } - PoloniexCompleteBalance balance = new PoloniexCompleteBalance(available, onOrders, btcValue); - filteredObject.add(entry.getKey().toString(), jsonParser.parse(balance.toString())); - } - return filteredObject.toString(); - } } diff --git a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java index 75de4be..69c6efd 100644 --- a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java +++ b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java @@ -3,6 +3,7 @@ package nl.komtek.gpi.services; import com.cf.PriceDataAPIClient; import com.cf.client.poloniex.PoloniexPublicAPIClient; import com.cf.client.poloniex.PoloniexTradingAPIClient; +import com.cf.data.model.poloniex.PoloniexCompleteBalance; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -245,7 +246,8 @@ public class GunbotProxyService { @Cacheable(value = "completeBalances", key = "#market", sync = true) public String getCompleteBalances(String market) { - return getCompleteBalancesScheduled(market); + String result = getCompleteBalancesScheduled(market); + return hideDust(result); } @CachePut(value = "completeBalances", key = "#market") @@ -320,11 +322,22 @@ public class GunbotProxyService { @CacheEvict(value = "completeBalances", allEntries = true)}) public synchronized String buyOrderWithProtection(String key, String currencyPair, BigDecimal buyPrice, BigDecimal amount) { boolean testMode = Boolean.parseBoolean(util.getConfigurationProperty("testMode")); + boolean globalSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty("sellOnlyMode")); + boolean pairSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty(String.format("%s_sellOnlyMode", currencyPair))); + if (testMode) { logger.info("Test mode: We bought {}", currencyPair); return ""; } + if (globalSellOnlyMode || pairSellOnlyMode) { + JsonObject jsonObject = new JsonObject(); + String message = String.format("You are not allowed to buy. Sell Only mode is active for %s", currencyPair); + jsonObject.addProperty("error", message); + logger.info(jsonObject.toString()); + return jsonObject.toString(); + } + PoloniexTradingAPIClient tmpTradingAPIClient; if (isUsingMultipleMarkets()) { tmpTradingAPIClient = getMultiMarketTradingClient(key); @@ -457,4 +470,36 @@ public class GunbotProxyService { .onFailedAttempt(this::handleException) .get(() -> analyzeResult(tradingAPIClient.returnOpenOrders("ALL"))); } + + private String hideDust(String result) { + boolean hidedust = Boolean.parseBoolean(util.getConfigurationProperty("hideDust")); + if (!hidedust) { + return result; + } + JsonParser jsonParser = new JsonParser(); + JsonElement jElement = jsonParser.parse(result); + JsonObject jObject = jElement.getAsJsonObject(); + JsonObject filteredObject = new JsonObject(); + for (Map.Entry entry : jObject.entrySet()) { + JsonElement element = (JsonElement) entry.getValue(); + BigDecimal available = BigDecimal.valueOf(element.getAsJsonObject().get("available").getAsDouble()); + BigDecimal onOrders = BigDecimal.valueOf(element.getAsJsonObject().get("onOrders").getAsDouble()); + BigDecimal btcValue = BigDecimal.valueOf(element.getAsJsonObject().get("btcValue").getAsDouble()); + + if (available.doubleValue() == 0) { + filteredObject.add(entry.getKey().toString(), element); + } + + double approximatePrice = btcValue.doubleValue() / available.add(onOrders).doubleValue(); + double availableValue = available.doubleValue() * approximatePrice; + if (!entry.getKey().equals("USDT") && availableValue < 0.00015) { + available = BigDecimal.ZERO; + } else if (entry.getKey().equals("USDT") && available.doubleValue() < 1) { + available = BigDecimal.ZERO; + } + PoloniexCompleteBalance balance = new PoloniexCompleteBalance(available, onOrders, btcValue); + filteredObject.add(entry.getKey().toString(), jsonParser.parse(balance.toString())); + } + return filteredObject.toString(); + } }