Skip to content

Instantly share code, notes, and snippets.

@danielkhoo
Last active September 3, 2022 11:42
Show Gist options
  • Save danielkhoo/0e1ed393cd22f713ee564ddc5c2c18aa to your computer and use it in GitHub Desktop.
Save danielkhoo/0e1ed393cd22f713ee564ddc5c2c18aa to your computer and use it in GitHub Desktop.
Scriptable: Eth Gas Widget
const API_KEY = "" // Add your etherscan API KEY here
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();
async function createWidget() {
// Create new empty ListWidget instance
let listwidget = new ListWidget();
// Set new background color
listwidget.backgroundColor = new Color("#000000");
let response = await fetchData();
displayData(listwidget, response);
// Return the created widget
return listwidget;
}
async function fetchData() {
// Query url
const url = `https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey=${API_KEY}`;
// Initialize new request
const request = new Request(url);
// Execute the request and parse the response as json
const response = await request.loadJSON();
// Return the returned launch data
return response;
}
function displayData(stack, response) {
const { FastGasPrice } = response.result
// Heading
let heading = stack.addText("⛽ Gas");
heading.centerAlignText();
heading.font = Font.lightSystemFont(24);
heading.textColor = new Color("#ffffff");
stack.addSpacer(15);
// Display values
let display = stack.addText(`${FastGasPrice} gwei`);
display.centerAlignText();
display.font = Font.lightSystemFont(28);
display.textColor = new Color("#ffffff");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment