diff --git a/src/main/java/nl/komtek/gpi/schedules/AllScheduledTasks.java b/src/main/java/nl/komtek/gpi/schedules/AllScheduledTasks.java new file mode 100644 index 0000000..900280b --- /dev/null +++ b/src/main/java/nl/komtek/gpi/schedules/AllScheduledTasks.java @@ -0,0 +1,37 @@ +package nl.komtek.gpi.schedules; + +import nl.komtek.gpi.services.GunbotProxyService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Created by Elroy on 23-6-2017. + */ +@Component +public class AllScheduledTasks { + + private Logger logger = LogManager.getLogger(AllScheduledTasks.class); + + @Autowired + private GunbotProxyService gunbotProxyService; + + @Scheduled(fixedRate = Long.MAX_VALUE) + public void itWorks() { + if (gunbotProxyService.isUsingMultipleMarkets()) { + logger.info("Running in multimarket MODE!"); + } + logger.info("Good job, It works!"); + } + + @Scheduled(fixedDelay = 2000) + public void updateTicker() { + try { + gunbotProxyService.getTickerScheduled(); + } catch (Exception e) { + logger.error(e); + } + } +} diff --git a/src/main/java/nl/komtek/gpi/schedules/BTCScheduledTasks.java b/src/main/java/nl/komtek/gpi/schedules/BTCScheduledTasks.java new file mode 100644 index 0000000..4886321 --- /dev/null +++ b/src/main/java/nl/komtek/gpi/schedules/BTCScheduledTasks.java @@ -0,0 +1,69 @@ +package nl.komtek.gpi.schedules; + +import nl.komtek.gpi.services.GunbotProxyService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Created by Elroy on 23-6-2017. + */ +@Component +@ConditionalOnProperty(name="default_BTC_apiKey", havingValue = "") +public class BTCScheduledTasks { + + private static final String market = "BTC"; + private Logger logger = LogManager.getLogger(BTCScheduledTasks.class); + + @Autowired + private GunbotProxyService gunbotProxyService; + + @Scheduled(fixedDelay = 9000) + public void updateCompleteBalances() { + + try { + if (gunbotProxyService.isActiveMarket(market)) { + gunbotProxyService.getCompleteBalancesScheduled(market); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 1500) + public void updateTradeHistory() { + try { + if (gunbotProxyService.isActiveMarket(market)) { + gunbotProxyService.getTradeHistoryScheduled(market); + } + } catch (Exception e) { + logger.error(e); + } + } + + + @Scheduled(fixedDelay = 2000) + public void updateOpenOrders() { + try { + if (gunbotProxyService.isActiveMarket(market)) { + gunbotProxyService.getOpenOrdersScheduled(market); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 10000) + public void updateBalancesBTC() { + try { + if (gunbotProxyService.isActiveMarket(market)) { + gunbotProxyService.getBalancesScheduled(market); + } + } catch (Exception e) { + logger.error(e); + } + } +} diff --git a/src/main/java/nl/komtek/gpi/schedules/DefaultScheduledTasks.java b/src/main/java/nl/komtek/gpi/schedules/DefaultScheduledTasks.java new file mode 100644 index 0000000..350a97d --- /dev/null +++ b/src/main/java/nl/komtek/gpi/schedules/DefaultScheduledTasks.java @@ -0,0 +1,59 @@ +package nl.komtek.gpi.schedules; + +import nl.komtek.gpi.services.GunbotProxyService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Created by Elroy on 23-6-2017. + */ +@Component +@ConditionalOnProperty(name="default_apiKey", havingValue = "") +public class DefaultScheduledTasks { + + private static final String market = "default"; + private Logger logger = LogManager.getLogger(DefaultScheduledTasks.class); + @Autowired + private GunbotProxyService gunbotProxyService; + + @Scheduled(fixedDelay = 9000) + public void updateCompleteBalances() { + + try { + gunbotProxyService.getCompleteBalancesScheduled(market); + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 1500) + public void updateTradeHistory() { + try { + gunbotProxyService.getTradeHistoryScheduled(market); + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 2000) + public void updateOpenOrders() { + try { + gunbotProxyService.getOpenOrdersScheduled(market); + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 10000) + public void updateBalances() { + try { + gunbotProxyService.getBalancesScheduled(market); + } catch (Exception e) { + logger.error(e); + } + } +} diff --git a/src/main/java/nl/komtek/gpi/schedules/ETHScheduledTasks.java b/src/main/java/nl/komtek/gpi/schedules/ETHScheduledTasks.java new file mode 100644 index 0000000..74bacaa --- /dev/null +++ b/src/main/java/nl/komtek/gpi/schedules/ETHScheduledTasks.java @@ -0,0 +1,66 @@ +package nl.komtek.gpi.schedules; + +import nl.komtek.gpi.services.GunbotProxyService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Created by Elroy on 23-6-2017. + */ +@Component +@ConditionalOnProperty(name = "default_ETH_apiKey", havingValue = "") +public class ETHScheduledTasks { + + private Logger logger = LogManager.getLogger(ETHScheduledTasks.class); + + @Autowired + private GunbotProxyService gunbotProxyService; + + @Scheduled(fixedDelay = 9200) + public void updateCompleteBalancesETH() { + try { + if (gunbotProxyService.isActiveMarket("ETH")) { + gunbotProxyService.getCompleteBalancesScheduled("ETH"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 1550) + public void updateTradeHistoryETH() { + try { + if (gunbotProxyService.isActiveMarket("ETH")) { + gunbotProxyService.getTradeHistoryScheduled("ETH"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 2050) + public void updateOpenOrdersETH() { + try { + if (gunbotProxyService.isActiveMarket("ETH")) { + gunbotProxyService.getOpenOrdersScheduled("ETH"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 10000) + public void updateBalancesETH() { + try { + if (gunbotProxyService.isActiveMarket("ETH")) { + gunbotProxyService.getBalancesScheduled("ETH"); + } + } catch (Exception e) { + logger.error(e); + } + } +} diff --git a/src/main/java/nl/komtek/gpi/schedules/USDTScheduledTasks.java b/src/main/java/nl/komtek/gpi/schedules/USDTScheduledTasks.java new file mode 100644 index 0000000..9d3f1cc --- /dev/null +++ b/src/main/java/nl/komtek/gpi/schedules/USDTScheduledTasks.java @@ -0,0 +1,66 @@ +package nl.komtek.gpi.schedules; + +import nl.komtek.gpi.services.GunbotProxyService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Created by Elroy on 23-6-2017. + */ +@Component +@ConditionalOnProperty(name="default_USDT_apiKey", havingValue = "") +public class USDTScheduledTasks { + + private Logger logger = LogManager.getLogger(USDTScheduledTasks.class); + + @Autowired + private GunbotProxyService gunbotProxyService; + + @Scheduled(fixedDelay = 9600) + public void updateCompleteBalancesUSDT() { + try { + if (gunbotProxyService.isActiveMarket("USDT")) { + gunbotProxyService.getCompleteBalancesScheduled("USDT"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 1650) + public void updateTradeHistoryUSDT() { + try { + if (gunbotProxyService.isActiveMarket("USDT")) { + gunbotProxyService.getTradeHistoryScheduled("USDT"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 2150) + public void updateOpenOrdersUSDT() { + try { + if (gunbotProxyService.isActiveMarket("USDT")) { + gunbotProxyService.getOpenOrdersScheduled("USDT"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 10000) + public void updateBalancesUSDT() { + try { + if (gunbotProxyService.isActiveMarket("USDT")) { + gunbotProxyService.getBalancesScheduled("USDT"); + } + } catch (Exception e) { + logger.error(e); + } + } +} diff --git a/src/main/java/nl/komtek/gpi/schedules/XMRScheduledTasks.java b/src/main/java/nl/komtek/gpi/schedules/XMRScheduledTasks.java new file mode 100644 index 0000000..e2f8bd6 --- /dev/null +++ b/src/main/java/nl/komtek/gpi/schedules/XMRScheduledTasks.java @@ -0,0 +1,66 @@ +package nl.komtek.gpi.schedules; + +import nl.komtek.gpi.services.GunbotProxyService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Created by Elroy on 23-6-2017. + */ +@Component +@ConditionalOnProperty(name="default_XMR_apiKey", havingValue = "") +public class XMRScheduledTasks { + + private Logger logger = LogManager.getLogger(XMRScheduledTasks.class); + + @Autowired + private GunbotProxyService gunbotProxyService; + + @Scheduled(fixedDelay = 9400) + public void updateCompleteBalancesXMR() { + try { + if (gunbotProxyService.isActiveMarket("XMR")) { + gunbotProxyService.getCompleteBalancesScheduled("XMR"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 1600) + public void updateTradeHistoryXMR() { + try { + if (gunbotProxyService.isActiveMarket("XMR")) { + gunbotProxyService.getTradeHistoryScheduled("XMR"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 2100) + public void updateOpenOrdersXMR() { + try { + if (gunbotProxyService.isActiveMarket("XMR")) { + gunbotProxyService.getOpenOrdersScheduled("XMR"); + } + } catch (Exception e) { + logger.error(e); + } + } + + @Scheduled(fixedDelay = 10000) + public void updateBalancesXMR() { + try { + if (gunbotProxyService.isActiveMarket("XMR")) { + gunbotProxyService.getBalancesScheduled("XMR"); + } + } catch (Exception e) { + logger.error(e); + } + } +} diff --git a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java index 52aac4b..8231d10 100644 --- a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java +++ b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java @@ -9,6 +9,7 @@ import com.google.gson.JsonParser; import net.jodah.failsafe.Failsafe; import net.jodah.failsafe.RetryPolicy; import nl.komtek.gpi.utils.ProxyHandledException; +import nl.komtek.gpi.utils.Util; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -19,7 +20,6 @@ import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.context.ApplicationContext; -import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @@ -39,7 +39,7 @@ public class GunbotProxyService { @Autowired private ApplicationContext applicationContext; @Autowired - Environment environment; + Util util; private Map poloniexDefaultAPIClients = new HashMap<>(); private Map poloniexTradingAPIClients = new HashMap<>(); private Map marketMapping = new HashMap<>(); @@ -55,17 +55,11 @@ public class GunbotProxyService { private void initService() { if (!createMarketMapping()) { - String apiKey = getEnvProperty("default_apiKey"); - String apiSecret = getEnvProperty("default_apiSecret"); + String apiKey = util.getEnvProperty("default_apiKey"); + String apiSecret = util.getEnvProperty("default_apiSecret"); poloniexDefaultAPIClients.put("default", new PoloniexTradingAPIClient(apiKey, apiSecret)); - if (poloniexDefaultAPIClients.get("default") == null) { - logger.error("The default apiKey and Secret is required"); - SpringApplication.exit(applicationContext); - return; - } - if (!createDefaultTradingClients()) { - return; - } + createDefaultTradingClients(); + } else { if (!createMarketDefaultApiClients("BTC")) { return; @@ -91,8 +85,8 @@ public class GunbotProxyService { } private boolean createMarketDefaultApiClients(String market) { - String apiKey = getEnvProperty("default_" + market + "_apiKey"); - String apiSecret = getEnvProperty("default_" + market + "_apiSecret"); + String apiKey = util.getEnvProperty("default_" + market + "_apiKey"); + String apiSecret = util.getEnvProperty("default_" + market + "_apiSecret"); if (!StringUtils.isEmpty(apiKey) && !StringUtils.isEmpty(apiSecret)) { if (!marketMapping.values().contains(market)) { logger.error(String.format("Please setup the %s market correctly", market)); @@ -110,8 +104,8 @@ public class GunbotProxyService { for (String market : markets) { for (int i = 1; i <= 10; i++) { - String apiKey = getEnvProperty(String.format("%s_apiKey%d", market, i)); - String apiSecret = getEnvProperty(String.format("%s_apiSecret%d", market, i)); + String apiKey = util.getEnvProperty(String.format("%s_apiKey%d", market, i)); + String apiSecret = util.getEnvProperty(String.format("%s_apiSecret%d", market, i)); if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(apiSecret)) { break; } @@ -123,19 +117,15 @@ public class GunbotProxyService { } private boolean createDefaultTradingClients() { - for (int i = 1; i <= 100; i++) { - String apiKey = getEnvProperty(String.format("apiKey%d", i)); - String apiSecret = getEnvProperty(String.format("apiSecret%d", i)); + for (int i = 1; i <= 10; i++) { + String apiKey = util.getEnvProperty(String.format("apiKey%d", i)); + String apiSecret = util.getEnvProperty(String.format("apiSecret%d", i)); if (apiKey == null || apiSecret == null) { break; } poloniexTradingAPIClients.put(apiKey, new PoloniexTradingAPIClient(apiKey, apiSecret)); } - if (poloniexTradingAPIClients.size() == 0) { - logger.error("Please setup at least 1 other apiKey for a better experience"); - SpringApplication.exit(applicationContext); - } return poloniexTradingAPIClients.size() > 0; } @@ -329,7 +319,7 @@ public class GunbotProxyService { } private PoloniexTradingAPIClient getTradingClient(String currencyPair) { - String apiKey = getEnvProperty(currencyPair + "_apiKey"); + String apiKey = util.getEnvProperty(currencyPair + "_apiKey"); if (apiKey != null) { return poloniexTradingAPIClients.get(apiKey); } else { @@ -352,11 +342,13 @@ public class GunbotProxyService { return tradingAPIClient; } - private String analyzeResult(String result) { - if (result != null && result.contains("Nonce")) { - throw new ProxyHandledException("nonce error: " + result); - } else if (result == null) { + public String analyzeResult(String result) { + if (result == null) { throw new ProxyHandledException("No value was returned"); + } else if (result.contains("Nonce")) { + throw new ProxyHandledException("nonce error: " + result); + } else if (result.contains("error")) { + throw new RuntimeException(result); } return result; } @@ -396,8 +388,19 @@ public class GunbotProxyService { return market; } - private String getEnvProperty(String key) { - String value = StringUtils.trimAllWhitespace(environment.getProperty(key)); - return StringUtils.replace(value, "\"", ""); + public String checkDefaultKey(String market) { + PoloniexTradingAPIClient tradingAPIClient = getMarketDefaultTradingClient(market); + String result = Failsafe.with(retryPolicy) + .onFailedAttempt(this::handleException) + .get(() -> analyzeResult(tradingAPIClient.returnOpenOrders("ALL"))); + return result; + } + + public String checkTradingKey(String apiKey) { + PoloniexTradingAPIClient tradingAPIClient = poloniexTradingAPIClients.get(apiKey); + String result = Failsafe.with(retryPolicy) + .onFailedAttempt(this::handleException) + .get(() -> analyzeResult(tradingAPIClient.returnOpenOrders("ALL"))); + return result; } } diff --git a/src/main/java/nl/komtek/gpi/utils/ScheduledTasks.java b/src/main/java/nl/komtek/gpi/utils/ScheduledTasks.java deleted file mode 100644 index 1d626be..0000000 --- a/src/main/java/nl/komtek/gpi/utils/ScheduledTasks.java +++ /dev/null @@ -1,189 +0,0 @@ -package nl.komtek.gpi.utils; - -import nl.komtek.gpi.services.GunbotProxyService; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -/** - * Created by Elroy on 23-6-2017. - */ -@Component -public class ScheduledTasks { - - private Logger logger = LogManager.getLogger(ScheduledTasks.class); - - @Autowired - private GunbotProxyService gunbotProxyService; - - @Scheduled(fixedRate = Long.MAX_VALUE) - public void itWorks() { - if (gunbotProxyService.isUsingMultipleMarkets()) { - logger.info("Running in multimarket MODE!"); - } - logger.info("Good job, It works!"); - } - - @Scheduled(fixedDelay = 2000) - public void updateTicker() { - try { - gunbotProxyService.getTickerScheduled(); - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 9000) - public void updateCompleteBalances() { - - try { - String market = "default"; - if (gunbotProxyService.isUsingMultipleMarkets()) { - market = "BTC"; - } - gunbotProxyService.getCompleteBalancesScheduled(market); - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 9200) - public void updateCompleteBalancesETH() { - try { - if (gunbotProxyService.isActiveMarket("ETH")) { - gunbotProxyService.getCompleteBalancesScheduled("ETH"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 9400) - public void updateCompleteBalancesXMR() { - try { - if (gunbotProxyService.isActiveMarket("XMR")) { - gunbotProxyService.getCompleteBalancesScheduled("XMR"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 9600) - public void updateCompleteBalancesUSDT() { - try { - if (gunbotProxyService.isActiveMarket("USDT")) { - gunbotProxyService.getCompleteBalancesScheduled("USDT"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 1500) - public void updateTradeHistory() { - try { - String market = "default"; - if (gunbotProxyService.isUsingMultipleMarkets()) { - market = "BTC"; - } - gunbotProxyService.getTradeHistoryScheduled(market); - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 1550) - public void updateTradeHistoryETH() { - try { - if (gunbotProxyService.isActiveMarket("ETH")) { - gunbotProxyService.getTradeHistoryScheduled("ETH"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 1600) - public void updateTradeHistoryXMR() { - try { - if (gunbotProxyService.isActiveMarket("XMR")) { - gunbotProxyService.getTradeHistoryScheduled("XMR"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 1650) - public void updateTradeHistoryUSDT() { - try { - if (gunbotProxyService.isActiveMarket("USDT")) { - gunbotProxyService.getTradeHistoryScheduled("USDT"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 2000) - public void updateOpenOrders() { - try { - String market = "default"; - if (gunbotProxyService.isUsingMultipleMarkets()) { - market = "BTC"; - } - gunbotProxyService.getOpenOrdersScheduled(market); - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 2050) - public void updateOpenOrdersETH() { - try { - if (gunbotProxyService.isActiveMarket("ETH")) { - gunbotProxyService.getOpenOrdersScheduled("ETH"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 2100) - public void updateOpenOrdersXMR() { - try { - if (gunbotProxyService.isActiveMarket("XMR")) { - gunbotProxyService.getOpenOrdersScheduled("XMR"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 2150) - public void updateOpenOrdersUSDT() { - try { - if (gunbotProxyService.isActiveMarket("USDT")) { - gunbotProxyService.getOpenOrdersScheduled("USDT"); - } - } catch (Exception e) { - logger.error(e); - } - } - - @Scheduled(fixedDelay = 10000) - public void updateBalances() { - try { - String market = "default"; - if (gunbotProxyService.isUsingMultipleMarkets()) { - market = "BTC"; - } - gunbotProxyService.getBalancesScheduled(market); - } catch (Exception e) { - logger.error(e); - } - } -} diff --git a/src/main/java/nl/komtek/gpi/utils/Util.java b/src/main/java/nl/komtek/gpi/utils/Util.java new file mode 100644 index 0000000..1b6cca3 --- /dev/null +++ b/src/main/java/nl/komtek/gpi/utils/Util.java @@ -0,0 +1,21 @@ +package nl.komtek.gpi.utils; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +/** + * Created by Elroy on 10-7-2017. + */ +@Component +public class Util { + + @Autowired + private Environment environment; + + public String getEnvProperty(String key) { + String value = StringUtils.trimAllWhitespace(environment.getProperty(key)); + return StringUtils.replace(value, "\"", ""); + } +}