Last active
May 2, 2022 17:15
-
-
Save cjinghong/3d7ccc7423039c07bf580611bdb0a47d to your computer and use it in GitHub Desktop.
ETH Gas Prices on Scriptable
This file contains 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: gem; | |
const widget = new ListWidget() | |
// widget.setPadding(16,16,16,16) | |
const { widgetFamily, runsInWidget } = config | |
const isSmallWidget = runsInWidget | |
? widgetFamily === 'small' | |
: false // TO SIMULATE WIDGET SIZE WHEN ON DEBUG MODE | |
const currencies = [ | |
'usd', | |
'myr' | |
] | |
const { | |
rapid, | |
fast, | |
standard, | |
slow, | |
} = await fetchEthGas() | |
const ethPriceData = await fetchEthPrice() | |
await createWidget() | |
// used for debugging if script runs inside the app | |
if (!runsInWidget) { | |
// await widget.presentSmall() | |
await widget.presentMedium() | |
} | |
widget.url = 'https://gasnow.org/' | |
Script.setWidget(widget) | |
Script.complete() | |
function createLabel(stack, text, size, isBold) { | |
const label = stack.addText(text) | |
if (isBold) { | |
label.font = Font.boldRoundedSystemFont(size || 12) | |
} else { | |
label.font = Font.regularRoundedSystemFont(size || 12) | |
} | |
return label | |
} | |
function createGasColumn(stack, title, gasPrice, waitTime) { | |
const standardSize = isSmallWidget ? 12 : 14 | |
const col = stack.addStack() | |
col.layoutVertically() | |
col.centerAlignContent() | |
createLabel(col, title, standardSize) | |
col.addSpacer(8) | |
const gasPriceUnit = isSmallWidget | |
? gasPrice | |
: `${gasPrice} gwei` | |
createLabel(col, gasPriceUnit, standardSize, true) | |
if (!isSmallWidget) { | |
createLabel(col, waitTime) | |
} | |
} | |
// build the content of the widget | |
async function createWidget() { | |
let icon = widget.addStack() | |
// Setup image icon | |
const coin = await getImage('eth') | |
const coinImg = icon.addImage(coin) | |
coinImg.imageSize = new Size(30, 30) | |
icon.layoutHorizontally() | |
icon.centerAlignContent() | |
icon.addSpacer(8) | |
const title = icon.addStack() | |
title.layoutVertically() | |
createLabel(title, 'ETH Gas', 14, true) | |
createLabel(title, `Price ${isSmallWidget ? '(Gwei)' : ''}`, 14, true) | |
widget.addSpacer(24) | |
let row = widget.addStack() | |
row.layoutHorizontally() | |
row.addSpacer(null) | |
createGasColumn(row, '🚀', rapid.toString(), '15 secs') | |
row.addSpacer(null) | |
createGasColumn(row, '✈️', fast.toString(), '1 min') | |
row.addSpacer(null) | |
createGasColumn(row, '🚙', standard.toString(), '3 mins') | |
row.addSpacer(null) | |
createGasColumn(row, '🐢', slow.toString(), '>10 mins') | |
row.addSpacer(null) | |
} | |
async function fetchEthPrice() { | |
const currencyString = currencies.join(',') | |
const id = 'ethereum' | |
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=${currencyString}` | |
const req = new Request(url) | |
const response = await req.loadJSON() | |
const prices = response[id] | |
console.log('PRICE') | |
console.log(response) | |
} | |
function toGwei(wei) { | |
return Math.round(wei / 1000000000) | |
} | |
async function fetchEthGas() { | |
const url = 'https://www.gasnow.org/api/v3/gas/price' | |
const req = new Request(url) | |
const response = await req.loadJSON() | |
const { | |
rapid, | |
fast, | |
standard, | |
slow, | |
timestamp, | |
} = response.data | |
const gasResponse = { | |
rapid: toGwei(rapid), | |
fast: toGwei(fast), | |
standard: toGwei(standard), | |
slow: toGwei(slow), | |
} | |
console.log('GAS RESULT') | |
console.log(gasResponse) | |
return gasResponse | |
} | |
// get images from local filestore or download them once | |
async function getImage(image) { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(dir, image) | |
if (fm.fileExists(path)) { | |
return fm.readImage(path) | |
} else { | |
// download once | |
let imageUrl | |
switch (image) { | |
case 'eth': | |
imageUrl = "https://icons.iconarchive.com/icons/cjdowner/cryptocurrency-flat/1024/Ethereum-ETH-icon.png" | |
break | |
default: | |
console.log(`Sorry, couldn't find ${image}.`); | |
} | |
let iconImage = await loadImage(imageUrl) | |
fm.writeImage(path, iconImage) | |
return iconImage | |
} | |
} | |
// helper function to download an image from a given url | |
async function loadImage(imgUrl) { | |
const req = new Request(imgUrl) | |
return await req.loadImage() | |
} | |
// end of script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment