From bf6e16fafad7faabd06d9df765cafed47a45e060 Mon Sep 17 00:00:00 2001 From: elroy Date: Sat, 29 Jul 2017 11:21:24 +0200 Subject: [PATCH] Add support for gunbot 3.3.3 and implement some missing polo endpoints --- pom.xml | 4 +- .../komtek/gpi/application/Application.java | 4 ++ .../controllers/GunbotProxyController.java | 60 +++++++++++++++---- .../gpi/services/GunbotProxyService.java | 34 +++++++++++ 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index 646b3b2..7f31cc0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ nl.komtek GunbotProxyCommunity - 0.9.8 + 0.9.9 jar GunbotProxyCommunity @@ -83,7 +83,7 @@ com.cf PoloniexClient - 1.1.2 + 1.1.3 diff --git a/src/main/java/nl/komtek/gpi/application/Application.java b/src/main/java/nl/komtek/gpi/application/Application.java index fce8eb8..320acc7 100644 --- a/src/main/java/nl/komtek/gpi/application/Application.java +++ b/src/main/java/nl/komtek/gpi/application/Application.java @@ -54,6 +54,8 @@ public class Application { Cache completeBalancesCache = new Cache("completeBalances", 5, false, true, 0, 0); Cache chartDataCache = new Cache("chartData", 500, false, false, 30, 30); Cache balancesCache = new Cache("balances", 5, false, true, 0, 0); + Cache orderBookCache = new Cache("orderBook", 5, false, true, 0, 0); + Cache publicTradeHistoryCache = new Cache("publicTradeHistory", 5, false, true, 0, 0); cacheManager.addCache(tickerCache); @@ -62,6 +64,8 @@ public class Application { cacheManager.addCache(completeBalancesCache); cacheManager.addCache(chartDataCache); cacheManager.addCache(balancesCache); + cacheManager.addCache(orderBookCache); + cacheManager.addCache(publicTradeHistoryCache); if (doubleBuyProtectionSeconds > 0) { Cache buyOrderProtectionCache = new Cache("buyOrderProtection", 100, false, false, doubleBuyProtectionSeconds, doubleBuyProtectionSeconds); diff --git a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java index 1306278..fa81685 100644 --- a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java +++ b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.math.BigDecimal; import java.time.ZoneOffset; import java.util.Arrays; +import java.util.Calendar; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -49,10 +50,53 @@ public class GunbotProxyController { @RequestMapping(value = "/public/**") @ResponseBody public String interceptAllCalls(HttpServletRequest request) { - logger.debug("intercepted -- " + request.getRequestURL() + "?" + request.getQueryString() + "?command=" + request.getParameter("command")); + logger.info("intercepted -- " + request.getRequestURL() + "?" + request.getQueryString() + "?command=" + request.getParameter("command")); return "intercepted"; } + @RequestMapping(value = "/tradingApi/**") + @ResponseBody + public String tradingRequests(HttpServletRequest request) { + logger.debug(request.getRequestURL() + "??command=" + request.getParameter("command")); + request.getParameterMap().keySet().forEach((e) -> System.out.print(e + "-")); + return "trading api intercepted"; + } + + @RequestMapping(value = "/public/**", params = "command=return24hVolume") + @ResponseBody + public String publicRequestOrderBook(@RequestParam String currencyPair) { + return gunbotProxyService.getOrderBook(currencyPair); + } + + @RequestMapping(value = "/public/**", params = "command=return24hVolume") + @ResponseBody + public String publicRequest24hVolume() { + return gunbotProxyService.get24hVolume(); + } + + @RequestMapping(value = "/public/**", params = "command=returnTradeHistory") + @ResponseBody + public String publicRequestTradeHistory(@RequestParam String currencyPair, + @RequestParam(required = false) String start, + @RequestParam(required = false) String end) { + final long startLong; + if (start.indexOf(".") > 0) { + startLong = Long.valueOf(start.substring(0, start.indexOf("."))); + } else { + startLong = Long.valueOf(start); + } + final long endLong; + if (end == null) { + Calendar cal = Calendar.getInstance(); + endLong = cal.getTimeInMillis() / 1000; + } else if (end.indexOf(".") > 0) { + endLong = Long.valueOf(end.substring(0, end.indexOf("."))); + } else { + endLong = Long.valueOf(end); + } + return gunbotProxyService.getPublicTradeHistory(currencyPair, startLong, endLong); + } + @RequestMapping(value = "/public/**", params = "command=returnChartData") @ResponseBody public String publicRequestChartData(HttpServletRequest request, @@ -71,14 +115,6 @@ public class GunbotProxyController { return gunbotProxyService.getTicker(); } - @RequestMapping(value = "/tradingApi/**") - @ResponseBody - public String tradingRequests(HttpServletRequest request) { - logger.debug(request.getRequestURL() + "??command=" + request.getParameter("command")); - request.getParameterMap().keySet().stream().forEach((e) -> System.out.print(e + "-")); - return "trading api"; - } - @RequestMapping(value = "/tradingApi/**", params = "command=returnCompleteBalances") @ResponseBody public String tradingRequestCompleteBalances(HttpServletRequest request) { @@ -235,14 +271,14 @@ public class GunbotProxyController { return filteredjArray.toString(); } - private JsonArray hideOpenOrders(JsonArray jsonArray){ + private JsonArray hideOpenOrders(JsonArray jsonArray) { for (Iterator it = jsonArray.iterator(); it.hasNext(); ) { JsonElement element = it.next(); JsonObject jsonObject = element.getAsJsonObject(); String orderNumber = jsonObject.get("orderNumber").getAsString(); - String[] orderNumbersToHide = StringUtils.trimAllWhitespace(util.getConfigurationProperty("hideOrders","")).split(","); - if (Arrays.asList(orderNumbersToHide).contains(orderNumber)){ + String[] orderNumbersToHide = StringUtils.trimAllWhitespace(util.getConfigurationProperty("hideOrders", "")).split(","); + if (Arrays.asList(orderNumbersToHide).contains(orderNumber)) { it.remove(); } } diff --git a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java index 0bcc674..7e25486 100644 --- a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java +++ b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java @@ -180,6 +180,40 @@ public class GunbotProxyService { return result; } + @Cacheable(value = "orderBook", key = "#currencyPair") + public String getOrderBook(String currencyPair) { + return getOrderBookScheduled(currencyPair); + } + + public String get24hVolume() { + return publicClient.return24Volume(); + } + + @CachePut(value = "orderBook", key = "#currencyPair") + public String getOrderBookScheduled(String currencyPair) { + String result = Failsafe.with(retryPolicy) + .onFailedAttempt(this::handleException) + .get(() -> analyzeResult(publicClient.returnOrderBook(currencyPair))); + + logger.debug("OrderBook: " + result); + + return result; + } + + @Cacheable(value = "publicTradeHistory", key = "#currencyPair", sync = true) + public String getPublicTradeHistory(String currencyPair, long start, long end) { + return getPublicTradeHistoryScheduled(currencyPair, start, end); + } + + @CachePut(value = "publicTradeHistory", key = "#market") + public String getPublicTradeHistoryScheduled(String currencyPair, long start, long end) { + String result = Failsafe.with(retryPolicy) + .onFailedAttempt(this::handleException) + .get(() -> analyzeResult(publicClient.returnTradeHistory(currencyPair, start, end))); + logger.debug(currencyPair + "-" + "public trade history: " + result); + return result; + } + @Cacheable(value = "tradeHistory", key = "#market", sync = true) public String getTradeHistory(String market) { return getTradeHistoryScheduled(market);