Created
May 17, 2021 14:04
-
-
Save moesalih/ef46c3bb12fbef63369ca21ad2b321b8 to your computer and use it in GitHub Desktop.
Ethereum Gas Price - Scriptable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: deep-blue; icon-glyph: link; | |
const req = new Request('https://www.gasnow.org/api/v3/gas/price') | |
let json = await req.loadJSON() | |
let price = Math.round(json.data.fast/1000000000) | |
let hueRange = Math.min(price / 300, 1) | |
let hue = hueRange * -120 + 120 | |
let color = new Color(HSLToHex(hue, 50, 50), 1) | |
let listWidget = createWidget({ | |
url: 'https://www.gasnow.org/', | |
header: 'Ethereum\nGas Price', | |
body: '' + price, | |
footer: 'Gwei', | |
textColor: color, | |
}) | |
if (config.runsInApp) { | |
listWidget.presentSmall() | |
} | |
Script.setWidget(listWidget) | |
Script.complete() | |
function createWidget(config) { | |
const listWidget = new ListWidget() | |
listWidget.url = config.url | |
listWidget.useDefaultPadding() | |
listWidget.backgroundColor = Color.black() | |
const header = listWidget.addText(config.header) | |
header.textColor = config.textColor | |
header.font = Font.semiboldSystemFont(11) | |
header.minimumScaleFactor = 0.50 | |
header.centerAlignText() | |
listWidget.addSpacer(10) | |
const body = listWidget.addText(config.body) | |
body.textColor = config.textColor | |
body.font = Font.boldSystemFont(36) | |
body.minimumScaleFactor = 0.50 | |
body.centerAlignText() | |
listWidget.addSpacer(0) | |
const footer = listWidget.addText(config.footer) | |
footer.textColor = config.textColor | |
footer.font = Font.semiboldSystemFont(11) | |
footer.minimumScaleFactor = 0.50 | |
footer.centerAlignText() | |
listWidget.addSpacer(20) | |
const updatedAt = new Date().toLocaleTimeString([], { hour: "numeric", minute: "2-digit", }) | |
const widgetText = listWidget.addText(`Updated ${updatedAt}`) | |
widgetText.textColor = config.textColor | |
widgetText.textOpacity = 0.6 | |
widgetText.font = Font.regularSystemFont(9) | |
widgetText.minimumScaleFactor = 0.6 | |
widgetText.centerAlignText() | |
return listWidget | |
} | |
function HSLToHex(h,s,l) { | |
s /= 100; | |
l /= 100; | |
let c = (1 - Math.abs(2 * l - 1)) * s, | |
x = c * (1 - Math.abs((h / 60) % 2 - 1)), | |
m = l - c/2, | |
r = 0, | |
g = 0, | |
b = 0; | |
if (0 <= h && h < 60) { | |
r = c; g = x; b = 0; | |
} else if (60 <= h && h < 120) { | |
r = x; g = c; b = 0; | |
} else if (120 <= h && h < 180) { | |
r = 0; g = c; b = x; | |
} else if (180 <= h && h < 240) { | |
r = 0; g = x; b = c; | |
} else if (240 <= h && h < 300) { | |
r = x; g = 0; b = c; | |
} else if (300 <= h && h < 360) { | |
r = c; g = 0; b = x; | |
} | |
// Having obtained RGB, convert channels to hex | |
r = Math.round((r + m) * 255).toString(16); | |
g = Math.round((g + m) * 255).toString(16); | |
b = Math.round((b + m) * 255).toString(16); | |
// Prepend 0s, if necessary | |
if (r.length == 1) | |
r = "0" + r; | |
if (g.length == 1) | |
g = "0" + g; | |
if (b.length == 1) | |
b = "0" + b; | |
return "#" + r + g + b; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment