Skip to content

Instantly share code, notes, and snippets.

@shivkanthb
Last active March 5, 2024 12:54
Show Gist options
  • Save shivkanthb/4433a3d23685941679ba938b91fe4178 to your computer and use it in GitHub Desktop.
Save shivkanthb/4433a3d23685941679ba938b91fe4178 to your computer and use it in GitHub Desktop.
Eth gas prices - Scriptable
"use strict";
async function getGasPrices() {
const url = "https://ethgasstation.info/api/ethgasAPI.json?api-key=blahblah";
// Initialize new request
const request = new Request(url);
// Execute the request and parse the response as json
const response = await request.loadJSON();
return response;
}
async function getEthPriceUSD() {
const url = 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd';
const request = new Request(url);
const response = await request.loadJSON();
return response['ethereum']['usd'];
}
async function createWidget() {
// Create new empty ListWidget instance
let listwidget = new ListWidget();
// listwidget.useDefaultPadding();
try {
const ethPrice = await getEthPriceUSD();
let gasPrices = await getGasPrices();
let fastGwei = gasPrices['fast'] / 10;
let avgGwei = gasPrices['average'] / 10;
let slowGwei = gasPrices['safeLow'] / 10;
let fastFee = Math.round(fastGwei * ethPrice / 1000000 * 21, 2)
let averageFee = Math.round(avgGwei * ethPrice / 1000000 * 21, 2)
let slowFee = Math.round(slowGwei * ethPrice / 1000000 * 21, 2)
// console.log(ethPrice);
// console.log(fastFee);
// console.log(averageFee);
// BACKGROUND
const startColor = Color.dynamic(new Color('83f5e5'), new Color('e761bd'));
const endColor = Color.dynamic(new Color('e761bd'), new Color('83f5e5'));
const textColor = Color.dynamic(new Color('ffffff'), new Color('ffffff'));
const gradient = new LinearGradient();
gradient.colors = [startColor, endColor];
gradient.locations = [0.0, 1];
listwidget.backgroundGradient = gradient;
// HEADER
const headStack = listwidget.addStack();
headStack.layoutHorizontally();
headStack.topAlignContent();
headStack.setPadding(0, 0, 0, 0);
const textStack = headStack.addStack();
textStack.layoutVertically();
textStack.topAlignContent();
textStack.setPadding(0, 0, 0, 0);
const header = textStack.addText('Ξ');
header.textColor = textColor;
header.font = Font.regularSystemFont(14);
header.minimumScaleFactor = 1;
const ethPriceText = textStack.addText(`$${ethPrice}`);
ethPriceText.textColor = textColor;
ethPriceText.font = Font.semiboldSystemFont(25);
ethPriceText.minimumScaleFactor = 0.5;
headStack.addSpacer(30);
// Gas Fees
const slowFeeText = listwidget.addText(`Slow 🐢: $${slowFee}`);
slowFeeText.textColor = textColor;
slowFeeText.font = Font.regularSystemFont(14);
slowFeeText.minimumScaleFactor = 0.5;
const avgFeeText = listwidget.addText(`Avg🚶: $${averageFee}`);
avgFeeText.textColor = textColor;
avgFeeText.font = Font.regularSystemFont(14);
avgFeeText.minimumScaleFactor = 0.5;
const fastFeeText = listwidget.addText(`Fast 🏃: $${fastFee}`);
fastFeeText.textColor = textColor;
fastFeeText.font = Font.regularSystemFont(14);
fastFeeText.minimumScaleFactor = 0.5;
// Updated at
listwidget.addSpacer(10);
let date = new Date();
const updatedAt = new Date().toLocaleTimeString([], {
hour: 'numeric',
minute: '2-digit',
});
const updatedTimeText = listwidget.addText(`Updated ${updatedAt}`);
updatedTimeText.textColor = textColor;
updatedTimeText.font = Font.regularSystemFont(10);
updatedTimeText.minimumScaleFactor = 0.5;
} catch (error) {
console.log(error);
const updatedTimeText = listwidget.addText(`Unable to fetch data`);
updatedTimeText.textColor = new Color('ffffff');
updatedTimeText.font = Font.regularSystemFont(10);
updatedTimeText.minimumScaleFactor = 0.5;
}
// Return the created widget
return listwidget;
}
async function run() {
let widget = await createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Runs inside a widget so add it to the homescreen widget
Script.setWidget(widget);
} else {
// Show the medium widget inside the app
widget.presentSmall();
}
Script.complete();
}
await run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment