Implement Sell only mode and Sell only mode for specified currency

Improve checkSetup page and implement special linux version
MaxRetries is now configurable.
This commit is contained in:
elroy 2017-07-13 23:16:03 +02:00
parent 361b0f4d80
commit 194890b022
7 changed files with 97 additions and 18 deletions

View File

@ -7,7 +7,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

1
configuration.properties Normal file
View File

@ -0,0 +1 @@
sellOnlyMode=true

View File

@ -7,9 +7,11 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
@ -19,17 +21,35 @@ import java.util.Map;
* Created by Elroy on 10-7-2017.
*/
@Controller
@RequestMapping("/checkSetup/**")
public class CheckSetupController {
@Autowired
private GunbotProxyService gunbotProxyService;
@Autowired
private Util util;
private Map<String, String> setupData = new HashMap<>();
@RequestMapping
@RequestMapping("/checkSetup/**")
public ModelAndView checkSetup(ModelMap modelMap) {
modelMap.put("setupData", setupData());
return new ModelAndView("setupData", modelMap);
}
@ResponseBody
@RequestMapping("/checkSetupLinux/**")
public String checkSetupLinux() throws IOException {
StringBuilder stringData = new StringBuilder();
stringData.append("\n\n");
Map<String, String> setupData = setupData();
for (Map.Entry entry : setupData.entrySet()) {
stringData.append(String.format("%s -- %s \n", entry.getKey(), entry.getValue()));
}
return stringData.toString();
}
private Map<String, String> setupData() {
Map<String, String> setupData = new HashMap<>();
try {
String keyName = "default_apiKey";
String secretName = "default_apiSecret";
@ -69,9 +89,9 @@ public class CheckSetupController {
try {
InetAddress address = InetAddress.getByName("poloniex.com");
if (address.getHostAddress().equals("127.0.0.1")) {
setupData.put("hostfile", "Looking good!");
setupData.put(String.format("hostfile (%s)", address.getHostAddress()), "Looking good!");
} else {
setupData.put("hostfile", "poloniex.com is not pointing to 127.0.0.1");
setupData.put(String.format("hostfile (%s)", address.getHostAddress()), "poloniex.com is not pointing to 127.0.0.1");
}
} catch (UnknownHostException e) {
setupData.put("hostfile", e.getMessage());
@ -80,9 +100,9 @@ public class CheckSetupController {
try {
InetAddress address = InetAddress.getByName("www.poloniex.com");
if (!address.getHostAddress().equals("127.0.0.1")) {
setupData.put("www.poloniex.com", "Looking good!");
setupData.put(String.format("www.poloniex.com (%s)", address.getHostAddress()), "Looking good!");
} else {
setupData.put("www.poloniex.com", "www.poloniex.com should not point to 127.0.0.1");
setupData.put(String.format("www.poloniex.com (%s)", address.getHostAddress()), "www.poloniex.com should not point to 127.0.0.1");
}
} catch (UnknownHostException e) {
setupData.put("www.poloniex.com", e.getMessage());
@ -91,20 +111,18 @@ public class CheckSetupController {
String gunbotLocation = util.getEnvProperty("gunbot.location");
if (!StringUtils.isEmpty(gunbotLocation.length())) {
if (!gunbotLocation.startsWith("file://")) {
setupData.put("Gunbot location", "Your file location should start with 'file://'");
setupData.put("Gunbot location (Optional)", "Your file location should start with 'file://'");
} else {
File file = new File(gunbotLocation.replace("file://",""));
File file = new File(gunbotLocation.replace("file://", ""));
if (file.exists()) {
setupData.put("Gunbot location", "Looking good!");
setupData.put("Gunbot location (Optional)", "Looking good!");
} else {
setupData.put("Gunbot location", "The specified location does not exist");
setupData.put("Gunbot location (Optional)", "The specified location does not exist");
}
}
} else {
setupData.put("Gunbot location", "You cannot use monitoring without this");
setupData.put("Gunbot location (Optional)", "You cannot use monitoring without this");
}
modelMap.put("setupData", setupData);
return new ModelAndView("setupData", modelMap);
return setupData;
}
}

View File

@ -8,6 +8,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import nl.komtek.gpi.services.GunbotProxyService;
import nl.komtek.gpi.utils.Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.ZoneOffset;
import java.util.List;
@ -35,6 +37,8 @@ public class GunbotProxyController {
private boolean doubleBuyProtection;
@Value("${doubleBuyProtectionSeconds:0}")
private int doubleBuyProtectionSeconds;
@Autowired
private Util util;
private Logger logger = LogManager.getLogger(GunbotProxyController.class);
private PoloniexDataMapper mapper = new PoloniexDataMapper();
@ -138,10 +142,20 @@ public class GunbotProxyController {
@RequestMapping(value = "/tradingApi/**", params = "command=buy")
@ResponseBody
public String tradingRequestBuy(HttpServletRequest request,
HttpServletResponse response,
@RequestParam String currencyPair,
@RequestParam BigDecimal rate,
@RequestParam BigDecimal amount) {
boolean globalSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty("sellOnlyMode"));
boolean pairSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty(String.format("%s_sellOnlyMode", currencyPair)));
if (globalSellOnlyMode || pairSellOnlyMode) {
String message = String.format("You are not allowed to buy. Sell Only mode is active for %s", currencyPair);
logger.info(message);
response.setStatus(403);
return message;
}
String key = request.getHeader("key");
if (doubleBuyProtection || doubleBuyProtectionSeconds > 0) {
return gunbotProxyService.buyOrderWithProtection(key, currencyPair, rate, amount);

View File

@ -13,6 +13,7 @@ import nl.komtek.gpi.utils.Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
@ -40,6 +41,8 @@ public class GunbotProxyService {
private ApplicationContext applicationContext;
@Autowired
private Util util;
@Value("${connection.maxRetries:6}")
private int maxRetries;
private Map<String, PoloniexTradingAPIClient> poloniexDefaultAPIClients = new HashMap<>();
private Map<String, PoloniexTradingAPIClient> poloniexTradingAPIClients = new HashMap<>();
private Map<String, String> marketMapping = new HashMap<>();
@ -81,7 +84,7 @@ public class GunbotProxyService {
retryPolicy = new RetryPolicy()
.retryOn(failure -> failure instanceof Exception)
.withDelay(500, TimeUnit.MILLISECONDS)
.withMaxRetries(6);
.withMaxRetries(maxRetries);
}
private boolean createMarketDefaultApiClients(String market) {

View File

@ -0,0 +1,24 @@
package nl.komtek.gpi.utils;
/**
* Created by Elroy on 13-7-2017.
*/
import java.util.Properties;
public class CaseLessProperties extends Properties {
public Object put(Object key, Object value) {
String lowercase = ((String) key).toLowerCase();
return super.put(lowercase, value);
}
public String getProperty(String key) {
String lowercase = key.toLowerCase();
return super.getProperty(lowercase);
}
public String getProperty(String key, String defaultValue) {
String lowercase = key.toLowerCase();
return super.getProperty(lowercase, defaultValue);
}
}

View File

@ -1,10 +1,16 @@
package nl.komtek.gpi.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
/**
* Created by Elroy on 10-7-2017.
*/
@ -13,9 +19,23 @@ public class Util {
@Autowired
private Environment environment;
private Logger logger = LogManager.getLogger(Util.class);
public String getEnvProperty(String key) {
String value = StringUtils.trimAllWhitespace(environment.getProperty(key));
return StringUtils.replace(value, "\"", "");
}
public String getConfigurationProperty(String key) {
CaseLessProperties prop = new CaseLessProperties();
String value = null;
try (InputStream input = new FileInputStream("configuration.properties")) {
prop.load(input);
value = prop.getProperty(key);
logger.debug(String.format("reading property key %s -- value %s"), key, value);
} catch (Exception e) {
logger.error("Error reading configuration file", e.getMessage());
}
return value;
}
}