2017-06-30 23:24:19 +02:00
|
|
|
package nl.komtek.gpi.controllers;
|
|
|
|
|
2017-07-11 22:36:22 +02:00
|
|
|
import com.cf.data.map.poloniex.PoloniexDataMapper;
|
|
|
|
import com.cf.data.model.poloniex.PoloniexChartData;
|
|
|
|
import com.cf.data.model.poloniex.PoloniexTradeHistory;
|
2017-06-30 23:24:19 +02:00
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import nl.komtek.gpi.services.GunbotProxyService;
|
2017-07-11 22:36:22 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2017-06-30 23:24:19 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2017-07-04 00:23:39 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2017-06-30 23:24:19 +02:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import java.math.BigDecimal;
|
2017-07-11 22:36:22 +02:00
|
|
|
import java.time.ZoneOffset;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-06-30 23:24:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Elroy on 17-6-2017.
|
|
|
|
*/
|
|
|
|
@Controller
|
|
|
|
public class GunbotProxyController {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GunbotProxyService gunbotProxyService;
|
2017-07-04 00:23:39 +02:00
|
|
|
@Value("${doubleBuyProtection:false}")
|
|
|
|
private boolean doubleBuyProtection;
|
2017-07-11 22:36:22 +02:00
|
|
|
private Logger logger = LogManager.getLogger(GunbotProxyController.class);
|
|
|
|
private PoloniexDataMapper mapper = new PoloniexDataMapper();
|
2017-06-30 23:24:19 +02:00
|
|
|
|
|
|
|
@RequestMapping(value = "/public/**")
|
|
|
|
@ResponseBody
|
|
|
|
public String interceptAllCalls(HttpServletRequest request) {
|
2017-07-11 22:36:22 +02:00
|
|
|
logger.debug("intercepted -- " + request.getRequestURL() + "?" + request.getQueryString() + "?command=" + request.getParameter("command"));
|
2017-06-30 23:24:19 +02:00
|
|
|
return "intercepted";
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/public/**", params = "command=returnChartData")
|
|
|
|
@ResponseBody
|
|
|
|
public String publicRequestChartData(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam String start,
|
|
|
|
@RequestParam long period) throws InterruptedException {
|
|
|
|
|
2017-07-11 22:36:22 +02:00
|
|
|
String result = gunbotProxyService.getChartData(currencyPair, period);
|
|
|
|
|
|
|
|
return filterChartDataByDate(start,result);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/public/**", params = "command=returnTicker")
|
|
|
|
@ResponseBody
|
|
|
|
public String publicRequestTicker() {
|
|
|
|
return gunbotProxyService.getTicker();
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequests(HttpServletRequest request) {
|
2017-07-11 22:36:22 +02:00
|
|
|
logger.debug(request.getRequestURL() + "??command=" + request.getParameter("command"));
|
2017-06-30 23:24:19 +02:00
|
|
|
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) {
|
2017-07-04 00:23:39 +02:00
|
|
|
|
|
|
|
String market = "default";
|
|
|
|
if (gunbotProxyService.isUsingMultipleMarkets()) {
|
|
|
|
String key = request.getHeader("key");
|
|
|
|
market = gunbotProxyService.getMarket(key);
|
|
|
|
}
|
|
|
|
return gunbotProxyService.getCompleteBalances(market);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=returnOpenOrders")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestOpenOrders(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair) {
|
2017-07-04 00:23:39 +02:00
|
|
|
String market = "default";
|
|
|
|
if (gunbotProxyService.isUsingMultipleMarkets()) {
|
|
|
|
String key = request.getHeader("key");
|
|
|
|
market = gunbotProxyService.getMarket(key);
|
|
|
|
}
|
|
|
|
String result = gunbotProxyService.getOpenOrders(market);
|
2017-06-30 23:24:19 +02:00
|
|
|
|
2017-07-11 22:36:22 +02:00
|
|
|
JsonElement jElement = new JsonParser().parse(result);
|
|
|
|
JsonObject jObject = jElement.getAsJsonObject();
|
|
|
|
JsonArray jArray = jObject.getAsJsonArray(currencyPair);
|
|
|
|
return jArray != null ? jArray.toString() : "[]";
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=returnTradeHistory")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestTradeHistory(HttpServletRequest request,
|
2017-07-11 22:36:22 +02:00
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam(required = false) String start) {
|
2017-06-30 23:24:19 +02:00
|
|
|
|
2017-07-04 00:23:39 +02:00
|
|
|
String market = "default";
|
|
|
|
if (gunbotProxyService.isUsingMultipleMarkets()) {
|
|
|
|
String key = request.getHeader("key");
|
|
|
|
market = gunbotProxyService.getMarket(key);
|
|
|
|
}
|
|
|
|
String result = gunbotProxyService.getTradeHistory(market);
|
2017-06-30 23:24:19 +02:00
|
|
|
|
2017-07-11 22:36:22 +02:00
|
|
|
return filterTradeHistoryByDate(currencyPair, start, result);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=cancelOrder")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestCancelOrder(HttpServletRequest request,
|
|
|
|
@RequestParam String orderNumber) {
|
|
|
|
|
2017-07-04 00:23:39 +02:00
|
|
|
String key = request.getHeader("key");
|
|
|
|
return gunbotProxyService.cancelOrder(key, orderNumber);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=sell")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestSell(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam BigDecimal rate,
|
|
|
|
@RequestParam BigDecimal amount) {
|
2017-07-04 00:23:39 +02:00
|
|
|
String key = request.getHeader("key");
|
|
|
|
return gunbotProxyService.sellOrder(key, currencyPair, rate, amount);
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/tradingApi/**", params = "command=buy")
|
|
|
|
@ResponseBody
|
|
|
|
public String tradingRequestBuy(HttpServletRequest request,
|
|
|
|
@RequestParam String currencyPair,
|
|
|
|
@RequestParam BigDecimal rate,
|
|
|
|
@RequestParam BigDecimal amount) {
|
|
|
|
|
2017-07-04 00:23:39 +02:00
|
|
|
String key = request.getHeader("key");
|
|
|
|
if (doubleBuyProtection) {
|
|
|
|
return gunbotProxyService.buyOrderWithProtection(key, currencyPair, rate, amount);
|
|
|
|
} else {
|
|
|
|
return gunbotProxyService.buyOrder(key, currencyPair, rate, amount);
|
|
|
|
}
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|
2017-07-11 22:36:22 +02:00
|
|
|
|
|
|
|
private String filterTradeHistoryByDate(String currency, String start, String result) {
|
|
|
|
final long startLong;
|
|
|
|
if (start.indexOf(".") > 0) {
|
|
|
|
startLong = Long.valueOf(start.substring(0, start.indexOf(".")));
|
|
|
|
} else {
|
|
|
|
startLong = Long.valueOf(start);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.equals("[]")) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
JsonElement jElement = jsonParser.parse(result);
|
|
|
|
JsonObject jObject = jElement.getAsJsonObject();
|
|
|
|
JsonObject filteredjObject = new JsonObject();
|
|
|
|
JsonArray filteredjArray = new JsonArray();
|
|
|
|
for (Map.Entry entry : jObject.entrySet()) {
|
|
|
|
if (!currency.equalsIgnoreCase("all") && !currency.equalsIgnoreCase(entry.getKey().toString())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
List<PoloniexTradeHistory> tradeHistory = mapper.mapTradeHistory(entry.getValue().toString());
|
|
|
|
|
|
|
|
tradeHistory.stream()
|
|
|
|
.filter(e -> e.date.toEpochSecond(ZoneOffset.UTC) >= startLong)
|
|
|
|
.map(e -> jsonParser.parse(e.toString()))
|
|
|
|
.forEach(filteredjArray::add);
|
|
|
|
|
|
|
|
filteredjObject.add(entry.getKey().toString(), filteredjArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currency.equals("all")) {
|
|
|
|
return (filteredjObject.entrySet().size() == 0) ? "[]" : filteredjObject.toString();
|
|
|
|
} else {
|
|
|
|
return (filteredjArray.size() == 0) ? "[]" : filteredjArray.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String filterChartDataByDate(String start, String result) {
|
|
|
|
final long startLong;
|
|
|
|
if (start.indexOf(".") > 0) {
|
|
|
|
startLong = Long.valueOf(start.substring(0, start.indexOf(".")));
|
|
|
|
} else {
|
|
|
|
startLong = Long.valueOf(start);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.equals("[]")) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<PoloniexChartData> chartData = mapper.mapChartData(result);
|
|
|
|
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
JsonArray filteredjArray = new JsonArray();
|
|
|
|
chartData.stream()
|
|
|
|
.filter(e -> e.date >= startLong)
|
|
|
|
.map(e -> jsonParser.parse(e.toString()))
|
|
|
|
.forEach(filteredjArray::add);
|
|
|
|
|
|
|
|
return filteredjArray.toString();
|
|
|
|
}
|
2017-06-30 23:24:19 +02:00
|
|
|
}
|