Skip to content

Instantly share code, notes, and snippets.

@AnasQiblawi
Last active March 10, 2021 20:55
Show Gist options
  • Save AnasQiblawi/fae4275514c30b2a3f3c3c31797707ac to your computer and use it in GitHub Desktop.
Save AnasQiblawi/fae4275514c30b2a3f3c3c31797707ac to your computer and use it in GitHub Desktop.
Monero Mining Widget
Change currency on line 4 and change wallet on line 10, script is requesting MineXMR.com, and trade info on exmo.com
const tradeReq = new Request("https://api.exmo.com/v1.1/trades")
tradeReq.method = "POST"
tradeReq.headers["Content-Type"] = "application/x-www-form-urlencoded"
tradeReq.body = "pair=XMR_UAH"
const address = "47wcnDjCDdjATivqH9GjC92jH9Vng7LCBMMxFmTV1Ybf5227MXhyD2gXynLUa9zrh5aPMAnu5npeQ2tLy8Z4pH7461vk6uo"
let widget = new ListWidget()
widget.url = `https://minexmr.com/dashboard?address=${address}`
let padding = 22;
widget.setPadding(padding, padding, padding, padding);
let apiResponse = await loadItems();
let header = widget.addText("📉 Mining".toUpperCase());
header.font = Font.mediumSystemFont(18);
widget.addSpacer(16);
let vStack = widget.addStack();
vStack.layoutHorizontally();
addDataView(vStack, "Hashrate", Math.round(apiResponse.hashrate) + " kh/s");
vStack.addSpacer();
addDataView(vStack, "Balance", apiResponse.balance.toString().substr(0,5));
vStack.addSpacer();
addDataView(vStack, "UAH", apiResponse.balanceCurr.toString().substr(0,5));
vStack.addSpacer();
Script.setWidget(widget);
Script.complete();
widget.presentSmall();
function addDataView(widget, text, value) {
let viewStack = widget.addStack();
viewStack.layoutVertically();
let label = viewStack.addText(text);
label.font = Font.mediumSystemFont(14);
let v = viewStack.addText(value.toString());
v.font = Font.mediumSystemFont(30);
v.textColor = colorForString(value < 10 ? "yellow" : "red");
}
async function fetchJSON(url) {
const req = new Request(url)
const json = await req.loadJSON()
return json
}
async function loadItems() {
let { balance } = await fetchJSON(`https://minexmr.com/api/main/user/stats?address=${address}`)
let workers = await fetchJSON(`https://minexmr.com/api/main/user/workers?address=${address}`)
balance = (parseInt(balance) / 1000000000000)
let hashrate = (workers.map(({ hashrate }) => hashrate).reduce((a, b) => a + b, 0)) / 1000
let {XMR_UAH: [trade]} = await tradeReq.loadJSON()
let balanceCurr = balance * parseFloat(trade.price)
return { balance, balanceCurr, hashrate, workers };
}
function colorForString(colorString) {
if (colorString == "red") {
return Color.red();
}
if (colorString == "yellow") {
return Color.yellow();
}
return Color.green();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment