From 3fde5d7f1df9159b85cd7c65673dbaedd758b159 Mon Sep 17 00:00:00 2001 From: rdavidek Date: Tue, 18 May 2021 12:28:52 +0200 Subject: [PATCH] added ETH --- src/main/java/cz/kamma/mining/Constants.java | 2 +- .../java/cz/kamma/mining/bean/MiningBean.java | 487 ++++++++++-------- src/main/webapp/ajaxview.jsp | 2 +- src/main/webapp/client.js | 4 +- src/main/webapp/index.jsp | 6 + 5 files changed, 271 insertions(+), 230 deletions(-) diff --git a/src/main/java/cz/kamma/mining/Constants.java b/src/main/java/cz/kamma/mining/Constants.java index dbd3de1..2406681 100644 --- a/src/main/java/cz/kamma/mining/Constants.java +++ b/src/main/java/cz/kamma/mining/Constants.java @@ -2,7 +2,7 @@ package cz.kamma.mining; public interface Constants { - public static final String APP_VERSION = "v0.4"; + public static final String APP_VERSION = "v0.5"; public static final String COPYRIGHT = "2021"; public static final int DEFAULT_HTTP_TIMEOUT = 2000; } diff --git a/src/main/java/cz/kamma/mining/bean/MiningBean.java b/src/main/java/cz/kamma/mining/bean/MiningBean.java index 6c91eb5..80aee51 100644 --- a/src/main/java/cz/kamma/mining/bean/MiningBean.java +++ b/src/main/java/cz/kamma/mining/bean/MiningBean.java @@ -16,266 +16,301 @@ import cz.kamma.mining.vo.KeyValue; public class MiningBean { - public static String updateAll(HttpServletRequest request, HttpServletResponse response) throws Exception { - java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); - List prices = new ArrayList<>(); - List workers = new ArrayList<>(); - List pool = new ArrayList<>(); - List network = new ArrayList<>(); + public static String updateAll(HttpServletRequest request, HttpServletResponse response) throws Exception { + java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); + List prices = new ArrayList<>(); + List workers = new ArrayList<>(); + List pool = new ArrayList<>(); + List network = new ArrayList<>(); - String[] cryptoCur = new String[] { "btc", "ltc", "xrp", "eth" }; - double[] lastPrices = new double[] { 0, 0, 0, 0 }; + String[] cryptoCur = new String[] { "btc", "ltc", "xrp", "eth" }; + double[] lastPrices = new double[] { 0, 0, 0, 0 }; - String apikey = request.getParameter("apikey"); - String ltcWallet = request.getParameter("ltc"); - String xrpWallet = request.getParameter("xrp"); - String currency = request.getParameter("cur"); + String apikey = request.getParameter("apikey"); + String ltcWallet = request.getParameter("ltc"); + String xrpWallet = request.getParameter("xrp"); + String ethWallet = request.getParameter("eth"); + String currency = request.getParameter("cur"); - if (request.getParameter("apikey") == null) { - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid API key"); - return null; - } + if (request.getParameter("apikey") == null) { + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid API key"); + return null; + } - if (currency == null || currency.equals("null") || currency.length() == 0) - currency = "usd"; - else - currency = currency.toLowerCase(); + if (currency == null || currency.equals("null") || currency.length() == 0) + currency = "usd"; + else + currency = currency.toLowerCase(); - String curSym = "$"; + String curSym = "$"; - if (currency.equals("usd")) - curSym = "$"; - else if (currency.equals("eur")) - curSym = "€"; + if (currency.equals("usd")) + curSym = "$"; + else if (currency.equals("eur")) + curSym = "€"; - workers.clear(); - prices.clear(); - network.clear(); - pool.clear(); + workers.clear(); + prices.clear(); + network.clear(); + pool.clear(); - String resultStr; - String restRequest; + String resultStr; + String restRequest; - for (int i = 0; i < cryptoCur.length; i++) { - String ccur = cryptoCur[i]; - restRequest = HttpHelper.getStringResponse("https://www.bitstamp.net/api/v2/ticker/".concat(ccur).concat(currency)); - if (restRequest != null && restRequest.length() > 0) { - JSONObject tmp = new JSONObject(restRequest); - double res = tmp.getDouble("last"); - lastPrices[i] = res; - if (res > 0d) - resultStr = curSym + res; - else - resultStr = "Unknown"; - KeyValue wi = new KeyValue(); - wi.setKey(ccur.toUpperCase().concat("/").concat(currency.toUpperCase()).concat(" (Bitstamp)")); - wi.setValue(resultStr); - prices.add(wi); - } - } + for (int i = 0; i < cryptoCur.length; i++) { + String ccur = cryptoCur[i]; + restRequest = HttpHelper + .getStringResponse("https://www.bitstamp.net/api/v2/ticker/".concat(ccur).concat(currency)); + if (restRequest != null && restRequest.length() > 0) { + JSONObject tmp = new JSONObject(restRequest); + double res = tmp.getDouble("last"); + lastPrices[i] = res; + if (res > 0d) + resultStr = curSym + res; + else + resultStr = "Unknown"; + KeyValue wi = new KeyValue(); + wi.setKey(ccur.toUpperCase().concat("/").concat(currency.toUpperCase()).concat(" (Bitstamp)")); + wi.setValue(resultStr); + prices.add(wi); + } + } - if (ltcWallet != null && !"null".equals(ltcWallet) && ltcWallet.length() > 0) { - restRequest = HttpHelper.getStringResponse("https://chain.so/api/v2/get_address_balance/LTC/".concat(ltcWallet)); - if (restRequest != null && restRequest.length() > 0) { - JSONObject full = new JSONObject(restRequest); - JSONObject data = (JSONObject)full.get("data"); - String confirmedBalance = (String) data.get("confirmed_balance"); + if (ltcWallet != null && !"null".equals(ltcWallet) && ltcWallet.length() > 0) { + restRequest = HttpHelper + .getStringResponse("https://chain.so/api/v2/get_address_balance/LTC/".concat(ltcWallet)); + if (restRequest != null && restRequest.length() > 0) { + JSONObject full = new JSONObject(restRequest); + JSONObject data = (JSONObject) full.get("data"); + String confirmedBalance = (String) data.get("confirmed_balance"); - double res = Double.parseDouble(confirmedBalance); - if (res > 0d) { - resultStr = res + " LTC " + curSym + String.format("%.2f", (res * lastPrices[1])); - } else - resultStr = "Unknown"; - KeyValue wi = new KeyValue(); - wi.setKey("My Balance LTC"); - wi.setValue(resultStr); - prices.add(wi); - } - } + double res = Double.parseDouble(confirmedBalance); + if (res > 0d) { + resultStr = res + " LTC " + curSym + String.format("%.2f", (res * lastPrices[1])); + } else + resultStr = "Unknown"; + KeyValue wi = new KeyValue(); + wi.setKey("My Balance LTC"); + wi.setValue(resultStr); + prices.add(wi); + } + } - if (xrpWallet != null && !"null".equals(xrpWallet) && xrpWallet.length() > 0) { - restRequest = HttpHelper.getStringResponse("https://data.ripple.com/v2/accounts/".concat(xrpWallet).concat("/balances")); - if (restRequest != null && restRequest.length() > 0) { - JSONObject full = new JSONObject(restRequest); - JSONArray balances = (JSONArray) full.get("balances"); - double res = balances.getJSONObject(0).getDouble("value"); - if (res > 0d) { - resultStr = res + " XRP " + curSym + String.format("%.2f", (res * lastPrices[2])); - } else - resultStr = "Unknown"; - KeyValue wi = new KeyValue(); - wi.setKey("My Balance XRP"); - wi.setValue(resultStr); - prices.add(wi); - } - } + if (xrpWallet != null && !"null".equals(xrpWallet) && xrpWallet.length() > 0) { + restRequest = HttpHelper + .getStringResponse("https://data.ripple.com/v2/accounts/".concat(xrpWallet).concat("/balances")); + if (restRequest != null && restRequest.length() > 0) { + JSONObject full = new JSONObject(restRequest); + JSONArray balances = (JSONArray) full.get("balances"); + double res = balances.getJSONObject(0).getDouble("value"); + if (res > 0d) { + resultStr = res + " XRP " + curSym + String.format("%.2f", (res * lastPrices[2])); + } else + resultStr = "Unknown"; + KeyValue wi = new KeyValue(); + wi.setKey("My Balance XRP"); + wi.setValue(resultStr); + prices.add(wi); + } + } - if (apikey != null && !"null".equals(apikey) && apikey.length() > 0) { - String miningJson = HttpHelper.getStringResponse("https://www.litecoinpool.org/api?api_key=".concat(apikey)); - if (miningJson != null && miningJson.length() > 0) { - JSONObject full = new JSONObject(miningJson); - JSONObject user = (JSONObject) full.get("user"); - KeyValue wi = new KeyValue(); - wi.setKey("HashRate"); - if (user.getInt("hash_rate") > 0) - wi.setValue((user.getInt("hash_rate") / 1000) + " MH/s"); - else - wi.setValue("Unknown"); - workers.add(wi); + if (ethWallet != null && !"null".equals(ethWallet) && ethWallet.length() > 0) { + restRequest = HttpHelper + .getStringResponse("https://api.etherscan.io/api?module=account&action=balance&address=" + ethWallet + + "&tag=latest&apikey=N54NVXSVSISMQ4QIM2JVKJW56KHCKMU5T4"); + if (restRequest != null && restRequest.length() > 0) { + JSONObject full = new JSONObject(restRequest); + String confirmedBalance = (String) full.get("result"); - JSONObject workersJson = (JSONObject) full.get("workers"); - for (Iterator keys = workersJson.keys(); keys.hasNext();) { - String key = keys.next(); - JSONObject worker = (JSONObject) workersJson.get(key); - wi = new KeyValue(); - wi.setKey("HashRate " + key); - wi.setValue((worker.getInt("hash_rate") / 1000) + " MH/s"); - workers.add(wi); - } + double res = Double.parseDouble(confirmedBalance); + res = res * 0.000000000000000001; + if (res > 0d) { + resultStr = res + " ETH " + curSym + String.format("%.2f", (res * lastPrices[3])); + } else + resultStr = "Unknown"; + KeyValue wi = new KeyValue(); + wi.setKey("My Balance ETH"); + wi.setValue(resultStr); + prices.add(wi); + } + } - wi = new KeyValue(); - wi.setKey("Paid"); - if (user.getDouble("paid_rewards") > 0) - wi.setValue(user.getDouble("paid_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("paid_rewards") * lastPrices[1]))); - else - wi.setValue("Unknown"); - workers.add(wi); + if (apikey != null && !"null".equals(apikey) && apikey.length() > 0) { + String miningJson = HttpHelper + .getStringResponse("https://www.litecoinpool.org/api?api_key=".concat(apikey)); + if (miningJson != null && miningJson.length() > 0) { + JSONObject full = new JSONObject(miningJson); + JSONObject user = (JSONObject) full.get("user"); + KeyValue wi = new KeyValue(); + wi.setKey("HashRate"); + if (user.getInt("hash_rate") > 0) + wi.setValue((user.getInt("hash_rate") / 1000) + " MH/s"); + else + wi.setValue("Unknown"); + workers.add(wi); - wi = new KeyValue(); - wi.setKey("Unpaid"); - if (user.getDouble("unpaid_rewards") > 0) - wi.setValue(user.getDouble("unpaid_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("unpaid_rewards") * lastPrices[1]))); - else - wi.setValue("Unknown"); - workers.add(wi); + JSONObject workersJson = (JSONObject) full.get("workers"); + for (Iterator keys = workersJson.keys(); keys.hasNext();) { + String key = keys.next(); + JSONObject worker = (JSONObject) workersJson.get(key); + wi = new KeyValue(); + wi.setKey("HashRate " + key); + wi.setValue((worker.getInt("hash_rate") / 1000) + " MH/s"); + workers.add(wi); + } - wi = new KeyValue(); - wi.setKey("Total"); - if (user.getDouble("total_rewards") > 0) - wi.setValue(user.getDouble("total_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("total_rewards") * lastPrices[1]))); - else - wi.setValue("Unknown"); - workers.add(wi); + wi = new KeyValue(); + wi.setKey("Paid"); + if (user.getDouble("paid_rewards") > 0) + wi.setValue(user.getDouble("paid_rewards") + " LTC " + curSym + + String.format("%.2f", (user.getDouble("paid_rewards") * lastPrices[1]))); + else + wi.setValue("Unknown"); + workers.add(wi); - wi = new KeyValue(); - wi.setKey("Expected 24h"); - if (user.getDouble("expected_24h_rewards") > 0) - wi.setValue(user.getDouble("expected_24h_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("expected_24h_rewards") * lastPrices[1]))); - else - wi.setValue("Unknown"); - workers.add(wi); + wi = new KeyValue(); + wi.setKey("Unpaid"); + if (user.getDouble("unpaid_rewards") > 0) + wi.setValue(user.getDouble("unpaid_rewards") + " LTC " + curSym + + String.format("%.2f", (user.getDouble("unpaid_rewards") * lastPrices[1]))); + else + wi.setValue("Unknown"); + workers.add(wi); - wi = new KeyValue(); - wi.setKey("Past 24h"); - if (user.getDouble("past_24h_rewards") > 0) - wi.setValue(user.getDouble("past_24h_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("past_24h_rewards") * lastPrices[1]))); - else - wi.setValue("Unknown"); - workers.add(wi); + wi = new KeyValue(); + wi.setKey("Total"); + if (user.getDouble("total_rewards") > 0) + wi.setValue(user.getDouble("total_rewards") + " LTC " + curSym + + String.format("%.2f", (user.getDouble("total_rewards") * lastPrices[1]))); + else + wi.setValue("Unknown"); + workers.add(wi); - JSONObject net = (JSONObject) full.get("network"); - wi = new KeyValue(); - wi.setKey("Network HashRate"); - if (net.getLong("hash_rate") > 0) - wi.setValue((net.getLong("hash_rate") / 1000000) + " GH/s"); - else - wi.setValue("Unknown"); - network.add(wi); + wi = new KeyValue(); + wi.setKey("Expected 24h"); + if (user.getDouble("expected_24h_rewards") > 0) + wi.setValue(user.getDouble("expected_24h_rewards") + " LTC " + curSym + + String.format("%.2f", (user.getDouble("expected_24h_rewards") * lastPrices[1]))); + else + wi.setValue("Unknown"); + workers.add(wi); - wi = new KeyValue(); - wi.setKey("Difficulty"); - if (net.getDouble("difficulty") > 0) - wi.setValue("" + net.getDouble("difficulty")); - else - wi.setValue("Unknown"); - network.add(wi); + wi = new KeyValue(); + wi.setKey("Past 24h"); + if (user.getDouble("past_24h_rewards") > 0) + wi.setValue(user.getDouble("past_24h_rewards") + " LTC " + curSym + + String.format("%.2f", (user.getDouble("past_24h_rewards") * lastPrices[1]))); + else + wi.setValue("Unknown"); + workers.add(wi); - wi = new KeyValue(); - wi.setKey("Next Difficulty"); - if (net.getDouble("next_difficulty") > 0) - wi.setValue("" + net.getDouble("next_difficulty")); - else - wi.setValue("Unknown"); - network.add(wi); + JSONObject net = (JSONObject) full.get("network"); + wi = new KeyValue(); + wi.setKey("Network HashRate"); + if (net.getLong("hash_rate") > 0) + wi.setValue((net.getLong("hash_rate") / 1000000) + " GH/s"); + else + wi.setValue("Unknown"); + network.add(wi); - wi = new KeyValue(); - wi.setKey("Retarget time"); - if (net.getDouble("retarget_time") > 0) - wi.setValue(splitToComponentTimes(net.getInt("retarget_time"))); - else - wi.setValue("Unknown"); - network.add(wi); + wi = new KeyValue(); + wi.setKey("Difficulty"); + if (net.getDouble("difficulty") > 0) + wi.setValue("" + net.getDouble("difficulty")); + else + wi.setValue("Unknown"); + network.add(wi); - JSONObject poolJson = (JSONObject) full.get("pool"); - wi = new KeyValue(); - wi.setKey("Pool HashRate"); - if (poolJson.getLong("hash_rate") > 0) - wi.setValue((poolJson.getLong("hash_rate") / 1000000) + " GH/s"); - else - wi.setValue("Unknown"); - pool.add(wi); + wi = new KeyValue(); + wi.setKey("Next Difficulty"); + if (net.getDouble("next_difficulty") > 0) + wi.setValue("" + net.getDouble("next_difficulty")); + else + wi.setValue("Unknown"); + network.add(wi); - wi = new KeyValue(); - wi.setKey("Active users"); - if (poolJson.getInt("active_users") > 0) - wi.setValue("" + poolJson.getInt("active_users")); - else - wi.setValue("Unknown"); - pool.add(wi); + wi = new KeyValue(); + wi.setKey("Retarget time"); + if (net.getDouble("retarget_time") > 0) + wi.setValue(splitToComponentTimes(net.getInt("retarget_time"))); + else + wi.setValue("Unknown"); + network.add(wi); - wi = new KeyValue(); - wi.setKey("PPS Ratio"); - if (poolJson.getDouble("pps_ratio") > 0) - wi.setValue("" + poolJson.getDouble("pps_ratio")); - else - wi.setValue("Unknown"); - pool.add(wi); - } - } + JSONObject poolJson = (JSONObject) full.get("pool"); + wi = new KeyValue(); + wi.setKey("Pool HashRate"); + if (poolJson.getLong("hash_rate") > 0) + wi.setValue((poolJson.getLong("hash_rate") / 1000000) + " GH/s"); + else + wi.setValue("Unknown"); + pool.add(wi); - String res = "#PRICES##WORKERS##POOL##NETWORK#"; + wi = new KeyValue(); + wi.setKey("Active users"); + if (poolJson.getInt("active_users") > 0) + wi.setValue("" + poolJson.getInt("active_users")); + else + wi.setValue("Unknown"); + pool.add(wi); - String tmp = ""; - for (KeyValue kv : prices) { - tmp = tmp.concat("").concat(kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); - } - res = res.replace("#PRICES#", tmp); + wi = new KeyValue(); + wi.setKey("PPS Ratio"); + if (poolJson.getDouble("pps_ratio") > 0) + wi.setValue("" + poolJson.getDouble("pps_ratio")); + else + wi.setValue("Unknown"); + pool.add(wi); + } + } - tmp = ""; - for (KeyValue kv : workers) { - tmp = tmp.concat("").concat(kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); - } - res = res.replace("#WORKERS#", tmp); + String res = "#PRICES##WORKERS##POOL##NETWORK#"; - tmp = ""; - for (KeyValue kv : pool) { - tmp = tmp.concat("").concat(kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); - } - res = res.replace("#POOL#", tmp); + String tmp = ""; + for (KeyValue kv : prices) { + tmp = tmp.concat("").concat( + kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); + } + res = res.replace("#PRICES#", tmp); - tmp = ""; - for (KeyValue kv : network) { - tmp = tmp.concat("").concat(kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); - } - res = res.replace("#NETWORK#", tmp); + tmp = ""; + for (KeyValue kv : workers) { + tmp = tmp.concat("").concat( + kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); + } + res = res.replace("#WORKERS#", tmp); - return res; - } + tmp = ""; + for (KeyValue kv : pool) { + tmp = tmp.concat("").concat( + kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); + } + res = res.replace("#POOL#", tmp); - public String getVersion() { - return Constants.APP_VERSION; - } + tmp = ""; + for (KeyValue kv : network) { + tmp = tmp.concat("").concat( + kv.getKey().concat("").concat("").concat(kv.getValue()).concat("")); + } + res = res.replace("#NETWORK#", tmp); - public static String splitToComponentTimes(int longVal) { - int days = longVal / (24 * 3600); - int remainder = longVal - (days * 24 * 3600); - int hours = remainder / 3600; - remainder = remainder - hours * 3600; - int mins = remainder / 60; - //remainder = remainder - mins * 60; - //int secs = remainder; + return res; + } - return (days>0?days+"d ":"")+(hours>0?hours+"h ":"")+(mins>0?mins+"m ":""); - } + public String getVersion() { + return Constants.APP_VERSION; + } + + public static String splitToComponentTimes(int longVal) { + int days = longVal / (24 * 3600); + int remainder = longVal - (days * 24 * 3600); + int hours = remainder / 3600; + remainder = remainder - hours * 3600; + int mins = remainder / 60; + // remainder = remainder - mins * 60; + // int secs = remainder; + + return (days > 0 ? days + "d " : "") + (hours > 0 ? hours + "h " : "") + (mins > 0 ? mins + "m " : ""); + } } diff --git a/src/main/webapp/ajaxview.jsp b/src/main/webapp/ajaxview.jsp index 58323f7..91b32ce 100644 --- a/src/main/webapp/ajaxview.jsp +++ b/src/main/webapp/ajaxview.jsp @@ -24,7 +24,7 @@