added ETH

This commit is contained in:
rdavidek 2021-05-18 12:28:52 +02:00
parent 00f5522167
commit 3fde5d7f1d
5 changed files with 271 additions and 230 deletions

View File

@ -2,7 +2,7 @@ package cz.kamma.mining;
public interface Constants { 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 String COPYRIGHT = "2021";
public static final int DEFAULT_HTTP_TIMEOUT = 2000; public static final int DEFAULT_HTTP_TIMEOUT = 2000;
} }

View File

@ -16,266 +16,301 @@ import cz.kamma.mining.vo.KeyValue;
public class MiningBean { public class MiningBean {
public static String updateAll(HttpServletRequest request, HttpServletResponse response) throws Exception { public static String updateAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
List<KeyValue> prices = new ArrayList<>(); List<KeyValue> prices = new ArrayList<>();
List<KeyValue> workers = new ArrayList<>(); List<KeyValue> workers = new ArrayList<>();
List<KeyValue> pool = new ArrayList<>(); List<KeyValue> pool = new ArrayList<>();
List<KeyValue> network = new ArrayList<>(); List<KeyValue> network = new ArrayList<>();
String[] cryptoCur = new String[] { "btc", "ltc", "xrp", "eth" }; String[] cryptoCur = new String[] { "btc", "ltc", "xrp", "eth" };
double[] lastPrices = new double[] { 0, 0, 0, 0 }; double[] lastPrices = new double[] { 0, 0, 0, 0 };
String apikey = request.getParameter("apikey"); String apikey = request.getParameter("apikey");
String ltcWallet = request.getParameter("ltc"); String ltcWallet = request.getParameter("ltc");
String xrpWallet = request.getParameter("xrp"); String xrpWallet = request.getParameter("xrp");
String currency = request.getParameter("cur"); String ethWallet = request.getParameter("eth");
String currency = request.getParameter("cur");
if (request.getParameter("apikey") == null) { if (request.getParameter("apikey") == null) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid API key"); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid API key");
return null; return null;
} }
if (currency == null || currency.equals("null") || currency.length() == 0) if (currency == null || currency.equals("null") || currency.length() == 0)
currency = "usd"; currency = "usd";
else else
currency = currency.toLowerCase(); currency = currency.toLowerCase();
String curSym = "$"; String curSym = "$";
if (currency.equals("usd")) if (currency.equals("usd"))
curSym = "$"; curSym = "$";
else if (currency.equals("eur")) else if (currency.equals("eur"))
curSym = ""; curSym = "";
workers.clear(); workers.clear();
prices.clear(); prices.clear();
network.clear(); network.clear();
pool.clear(); pool.clear();
String resultStr; String resultStr;
String restRequest; String restRequest;
for (int i = 0; i < cryptoCur.length; i++) { for (int i = 0; i < cryptoCur.length; i++) {
String ccur = cryptoCur[i]; String ccur = cryptoCur[i];
restRequest = HttpHelper.getStringResponse("https://www.bitstamp.net/api/v2/ticker/".concat(ccur).concat(currency)); restRequest = HttpHelper
if (restRequest != null && restRequest.length() > 0) { .getStringResponse("https://www.bitstamp.net/api/v2/ticker/".concat(ccur).concat(currency));
JSONObject tmp = new JSONObject(restRequest); if (restRequest != null && restRequest.length() > 0) {
double res = tmp.getDouble("last"); JSONObject tmp = new JSONObject(restRequest);
lastPrices[i] = res; double res = tmp.getDouble("last");
if (res > 0d) lastPrices[i] = res;
resultStr = curSym + res; if (res > 0d)
else resultStr = curSym + res;
resultStr = "Unknown"; else
KeyValue wi = new KeyValue(); resultStr = "Unknown";
wi.setKey(ccur.toUpperCase().concat("/").concat(currency.toUpperCase()).concat(" (Bitstamp)")); KeyValue wi = new KeyValue();
wi.setValue(resultStr); wi.setKey(ccur.toUpperCase().concat("/").concat(currency.toUpperCase()).concat(" (Bitstamp)"));
prices.add(wi); wi.setValue(resultStr);
} prices.add(wi);
} }
}
if (ltcWallet != null && !"null".equals(ltcWallet) && ltcWallet.length() > 0) { if (ltcWallet != null && !"null".equals(ltcWallet) && ltcWallet.length() > 0) {
restRequest = HttpHelper.getStringResponse("https://chain.so/api/v2/get_address_balance/LTC/".concat(ltcWallet)); restRequest = HttpHelper
if (restRequest != null && restRequest.length() > 0) { .getStringResponse("https://chain.so/api/v2/get_address_balance/LTC/".concat(ltcWallet));
JSONObject full = new JSONObject(restRequest); if (restRequest != null && restRequest.length() > 0) {
JSONObject data = (JSONObject)full.get("data"); JSONObject full = new JSONObject(restRequest);
String confirmedBalance = (String) data.get("confirmed_balance"); JSONObject data = (JSONObject) full.get("data");
String confirmedBalance = (String) data.get("confirmed_balance");
double res = Double.parseDouble(confirmedBalance); double res = Double.parseDouble(confirmedBalance);
if (res > 0d) { if (res > 0d) {
resultStr = res + " LTC " + curSym + String.format("%.2f", (res * lastPrices[1])); resultStr = res + " LTC " + curSym + String.format("%.2f", (res * lastPrices[1]));
} else } else
resultStr = "Unknown"; resultStr = "Unknown";
KeyValue wi = new KeyValue(); KeyValue wi = new KeyValue();
wi.setKey("My Balance LTC"); wi.setKey("My Balance LTC");
wi.setValue(resultStr); wi.setValue(resultStr);
prices.add(wi); prices.add(wi);
} }
} }
if (xrpWallet != null && !"null".equals(xrpWallet) && xrpWallet.length() > 0) { if (xrpWallet != null && !"null".equals(xrpWallet) && xrpWallet.length() > 0) {
restRequest = HttpHelper.getStringResponse("https://data.ripple.com/v2/accounts/".concat(xrpWallet).concat("/balances")); restRequest = HttpHelper
if (restRequest != null && restRequest.length() > 0) { .getStringResponse("https://data.ripple.com/v2/accounts/".concat(xrpWallet).concat("/balances"));
JSONObject full = new JSONObject(restRequest); if (restRequest != null && restRequest.length() > 0) {
JSONArray balances = (JSONArray) full.get("balances"); JSONObject full = new JSONObject(restRequest);
double res = balances.getJSONObject(0).getDouble("value"); JSONArray balances = (JSONArray) full.get("balances");
if (res > 0d) { double res = balances.getJSONObject(0).getDouble("value");
resultStr = res + " XRP " + curSym + String.format("%.2f", (res * lastPrices[2])); if (res > 0d) {
} else resultStr = res + " XRP " + curSym + String.format("%.2f", (res * lastPrices[2]));
resultStr = "Unknown"; } else
KeyValue wi = new KeyValue(); resultStr = "Unknown";
wi.setKey("My Balance XRP"); KeyValue wi = new KeyValue();
wi.setValue(resultStr); wi.setKey("My Balance XRP");
prices.add(wi); wi.setValue(resultStr);
} prices.add(wi);
} }
}
if (apikey != null && !"null".equals(apikey) && apikey.length() > 0) { if (ethWallet != null && !"null".equals(ethWallet) && ethWallet.length() > 0) {
String miningJson = HttpHelper.getStringResponse("https://www.litecoinpool.org/api?api_key=".concat(apikey)); restRequest = HttpHelper
if (miningJson != null && miningJson.length() > 0) { .getStringResponse("https://api.etherscan.io/api?module=account&action=balance&address=" + ethWallet
JSONObject full = new JSONObject(miningJson); + "&tag=latest&apikey=N54NVXSVSISMQ4QIM2JVKJW56KHCKMU5T4");
JSONObject user = (JSONObject) full.get("user"); if (restRequest != null && restRequest.length() > 0) {
KeyValue wi = new KeyValue(); JSONObject full = new JSONObject(restRequest);
wi.setKey("HashRate"); String confirmedBalance = (String) full.get("result");
if (user.getInt("hash_rate") > 0)
wi.setValue((user.getInt("hash_rate") / 1000) + " MH/s");
else
wi.setValue("Unknown");
workers.add(wi);
JSONObject workersJson = (JSONObject) full.get("workers"); double res = Double.parseDouble(confirmedBalance);
for (Iterator<String> keys = workersJson.keys(); keys.hasNext();) { res = res * 0.000000000000000001;
String key = keys.next(); if (res > 0d) {
JSONObject worker = (JSONObject) workersJson.get(key); resultStr = res + " ETH " + curSym + String.format("%.2f", (res * lastPrices[3]));
wi = new KeyValue(); } else
wi.setKey("HashRate " + key); resultStr = "Unknown";
wi.setValue((worker.getInt("hash_rate") / 1000) + " MH/s"); KeyValue wi = new KeyValue();
workers.add(wi); wi.setKey("My Balance ETH");
} wi.setValue(resultStr);
prices.add(wi);
}
}
wi = new KeyValue(); if (apikey != null && !"null".equals(apikey) && apikey.length() > 0) {
wi.setKey("Paid"); String miningJson = HttpHelper
if (user.getDouble("paid_rewards") > 0) .getStringResponse("https://www.litecoinpool.org/api?api_key=".concat(apikey));
wi.setValue(user.getDouble("paid_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("paid_rewards") * lastPrices[1]))); if (miningJson != null && miningJson.length() > 0) {
else JSONObject full = new JSONObject(miningJson);
wi.setValue("Unknown"); JSONObject user = (JSONObject) full.get("user");
workers.add(wi); 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(); JSONObject workersJson = (JSONObject) full.get("workers");
wi.setKey("Unpaid"); for (Iterator<String> keys = workersJson.keys(); keys.hasNext();) {
if (user.getDouble("unpaid_rewards") > 0) String key = keys.next();
wi.setValue(user.getDouble("unpaid_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("unpaid_rewards") * lastPrices[1]))); JSONObject worker = (JSONObject) workersJson.get(key);
else wi = new KeyValue();
wi.setValue("Unknown"); wi.setKey("HashRate " + key);
workers.add(wi); wi.setValue((worker.getInt("hash_rate") / 1000) + " MH/s");
workers.add(wi);
}
wi = new KeyValue(); wi = new KeyValue();
wi.setKey("Total"); wi.setKey("Paid");
if (user.getDouble("total_rewards") > 0) if (user.getDouble("paid_rewards") > 0)
wi.setValue(user.getDouble("total_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("total_rewards") * lastPrices[1]))); wi.setValue(user.getDouble("paid_rewards") + " LTC " + curSym
else + String.format("%.2f", (user.getDouble("paid_rewards") * lastPrices[1])));
wi.setValue("Unknown"); else
workers.add(wi); wi.setValue("Unknown");
workers.add(wi);
wi = new KeyValue(); wi = new KeyValue();
wi.setKey("Expected 24h"); wi.setKey("Unpaid");
if (user.getDouble("expected_24h_rewards") > 0) if (user.getDouble("unpaid_rewards") > 0)
wi.setValue(user.getDouble("expected_24h_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("expected_24h_rewards") * lastPrices[1]))); wi.setValue(user.getDouble("unpaid_rewards") + " LTC " + curSym
else + String.format("%.2f", (user.getDouble("unpaid_rewards") * lastPrices[1])));
wi.setValue("Unknown"); else
workers.add(wi); wi.setValue("Unknown");
workers.add(wi);
wi = new KeyValue(); wi = new KeyValue();
wi.setKey("Past 24h"); wi.setKey("Total");
if (user.getDouble("past_24h_rewards") > 0) if (user.getDouble("total_rewards") > 0)
wi.setValue(user.getDouble("past_24h_rewards") + " LTC " + curSym + String.format("%.2f", (user.getDouble("past_24h_rewards") * lastPrices[1]))); wi.setValue(user.getDouble("total_rewards") + " LTC " + curSym
else + String.format("%.2f", (user.getDouble("total_rewards") * lastPrices[1])));
wi.setValue("Unknown"); else
workers.add(wi); wi.setValue("Unknown");
workers.add(wi);
JSONObject net = (JSONObject) full.get("network"); wi = new KeyValue();
wi = new KeyValue(); wi.setKey("Expected 24h");
wi.setKey("Network HashRate"); if (user.getDouble("expected_24h_rewards") > 0)
if (net.getLong("hash_rate") > 0) wi.setValue(user.getDouble("expected_24h_rewards") + " LTC " + curSym
wi.setValue((net.getLong("hash_rate") / 1000000) + " GH/s"); + String.format("%.2f", (user.getDouble("expected_24h_rewards") * lastPrices[1])));
else else
wi.setValue("Unknown"); wi.setValue("Unknown");
network.add(wi); workers.add(wi);
wi = new KeyValue(); wi = new KeyValue();
wi.setKey("Difficulty"); wi.setKey("Past 24h");
if (net.getDouble("difficulty") > 0) if (user.getDouble("past_24h_rewards") > 0)
wi.setValue("" + net.getDouble("difficulty")); wi.setValue(user.getDouble("past_24h_rewards") + " LTC " + curSym
else + String.format("%.2f", (user.getDouble("past_24h_rewards") * lastPrices[1])));
wi.setValue("Unknown"); else
network.add(wi); wi.setValue("Unknown");
workers.add(wi);
wi = new KeyValue(); JSONObject net = (JSONObject) full.get("network");
wi.setKey("Next Difficulty"); wi = new KeyValue();
if (net.getDouble("next_difficulty") > 0) wi.setKey("Network HashRate");
wi.setValue("" + net.getDouble("next_difficulty")); if (net.getLong("hash_rate") > 0)
else wi.setValue((net.getLong("hash_rate") / 1000000) + " GH/s");
wi.setValue("Unknown"); else
network.add(wi); wi.setValue("Unknown");
network.add(wi);
wi = new KeyValue(); wi = new KeyValue();
wi.setKey("Retarget time"); wi.setKey("Difficulty");
if (net.getDouble("retarget_time") > 0) if (net.getDouble("difficulty") > 0)
wi.setValue(splitToComponentTimes(net.getInt("retarget_time"))); wi.setValue("" + net.getDouble("difficulty"));
else else
wi.setValue("Unknown"); wi.setValue("Unknown");
network.add(wi); network.add(wi);
JSONObject poolJson = (JSONObject) full.get("pool"); wi = new KeyValue();
wi = new KeyValue(); wi.setKey("Next Difficulty");
wi.setKey("Pool HashRate"); if (net.getDouble("next_difficulty") > 0)
if (poolJson.getLong("hash_rate") > 0) wi.setValue("" + net.getDouble("next_difficulty"));
wi.setValue((poolJson.getLong("hash_rate") / 1000000) + " GH/s"); else
else wi.setValue("Unknown");
wi.setValue("Unknown"); network.add(wi);
pool.add(wi);
wi = new KeyValue(); wi = new KeyValue();
wi.setKey("Active users"); wi.setKey("Retarget time");
if (poolJson.getInt("active_users") > 0) if (net.getDouble("retarget_time") > 0)
wi.setValue("" + poolJson.getInt("active_users")); wi.setValue(splitToComponentTimes(net.getInt("retarget_time")));
else else
wi.setValue("Unknown"); wi.setValue("Unknown");
pool.add(wi); network.add(wi);
wi = new KeyValue(); JSONObject poolJson = (JSONObject) full.get("pool");
wi.setKey("PPS Ratio"); wi = new KeyValue();
if (poolJson.getDouble("pps_ratio") > 0) wi.setKey("Pool HashRate");
wi.setValue("" + poolJson.getDouble("pps_ratio")); if (poolJson.getLong("hash_rate") > 0)
else wi.setValue((poolJson.getLong("hash_rate") / 1000000) + " GH/s");
wi.setValue("Unknown"); else
pool.add(wi); wi.setValue("Unknown");
} pool.add(wi);
}
String res = "<response><prices>#PRICES#</prices><workers>#WORKERS#</workers><pool>#POOL#</pool><network>#NETWORK#</network></response>"; 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 = ""; wi = new KeyValue();
for (KeyValue kv : prices) { wi.setKey("PPS Ratio");
tmp = tmp.concat("<pair><key>").concat(kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>")); if (poolJson.getDouble("pps_ratio") > 0)
} wi.setValue("" + poolJson.getDouble("pps_ratio"));
res = res.replace("#PRICES#", tmp); else
wi.setValue("Unknown");
pool.add(wi);
}
}
tmp = ""; String res = "<response><prices>#PRICES#</prices><workers>#WORKERS#</workers><pool>#POOL#</pool><network>#NETWORK#</network></response>";
for (KeyValue kv : workers) {
tmp = tmp.concat("<pair><key>").concat(kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>"));
}
res = res.replace("#WORKERS#", tmp);
tmp = ""; String tmp = "";
for (KeyValue kv : pool) { for (KeyValue kv : prices) {
tmp = tmp.concat("<pair><key>").concat(kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>")); tmp = tmp.concat("<pair><key>").concat(
} kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>"));
res = res.replace("#POOL#", tmp); }
res = res.replace("#PRICES#", tmp);
tmp = ""; tmp = "";
for (KeyValue kv : network) { for (KeyValue kv : workers) {
tmp = tmp.concat("<pair><key>").concat(kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>")); tmp = tmp.concat("<pair><key>").concat(
} kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>"));
res = res.replace("#NETWORK#", tmp); }
res = res.replace("#WORKERS#", tmp);
return res; tmp = "";
} for (KeyValue kv : pool) {
tmp = tmp.concat("<pair><key>").concat(
kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>"));
}
res = res.replace("#POOL#", tmp);
public String getVersion() { tmp = "";
return Constants.APP_VERSION; for (KeyValue kv : network) {
} tmp = tmp.concat("<pair><key>").concat(
kv.getKey().concat("</key>").concat("<value>").concat(kv.getValue()).concat("</value></pair>"));
}
res = res.replace("#NETWORK#", tmp);
public static String splitToComponentTimes(int longVal) { return res;
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 ":""); 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 " : "");
}
} }

View File

@ -24,7 +24,7 @@
<script language=javascript type='text/javascript'> <script language=javascript type='text/javascript'>
function asynchUpdate() { function asynchUpdate() {
AX_asynchUpdate("<%=request.getParameter("apikey")%>", "<%=request.getParameter("ltc")%>", "<%=request.getParameter("xrp")%>", "<%=request.getParameter("cur")%>"); AX_asynchUpdate("<%=request.getParameter("apikey")%>", "<%=request.getParameter("ltc")%>", "<%=request.getParameter("eth")%>", "<%=request.getParameter("xrp")%>", "<%=request.getParameter("cur")%>");
setTimeout('asynchUpdate();', 5000); setTimeout('asynchUpdate();', 5000);
} }
asynchUpdate(); asynchUpdate();

View File

@ -73,8 +73,8 @@ function ajaxGetXmlResponse() {
} }
} }
function AX_asynchUpdate(apikey, ltc, xrp, cur) { function AX_asynchUpdate(apikey, ltc, eth, xrp, cur) {
var data = 'ajaxMethod=asynchUpdate&apikey=' + apikey + '&ltc=' + ltc var data = 'ajaxMethod=asynchUpdate&apikey=' + apikey + '&ltc=' + ltc + '&eth=' + eth
+ '&xrp=' + xrp+ '&cur=' + cur; + '&xrp=' + xrp+ '&cur=' + cur;
ajaxSendRequest(data, AX_asynchUpdateResponse); ajaxSendRequest(data, AX_asynchUpdateResponse);
} }

View File

@ -27,6 +27,12 @@
style='transition: text-shadow 0.7s ease-in-out;'><input style='transition: text-shadow 0.7s ease-in-out;'><input
type="text" name="ltc" size="40" /></td> type="text" name="ltc" size="40" /></td>
</tr> </tr>
<tr>
<td class='group'>Ethereum wallet address (optional)</td>
<td align="right" class='group'
style='transition: text-shadow 0.7s ease-in-out;'><input
type="text" name="eth" size="40" /></td>
</tr>
<tr> <tr>
<td class='group'>Ripple wallet address (optional)</td> <td class='group'>Ripple wallet address (optional)</td>
<td align="right" class='group' <td align="right" class='group'