Skip to content

Instantly share code, notes, and snippets.

@arielgk
Created October 30, 2022 21:30
Show Gist options
  • Select an option

  • Save arielgk/a11e30fab438488338f1ac6b971d8d49 to your computer and use it in GitHub Desktop.

Select an option

Save arielgk/a11e30fab438488338f1ac6b971d8d49 to your computer and use it in GitHub Desktop.
Blue Widget
const params = args.widgetParameter ? args.widgetParameter.split(",") : [];
const isDarkTheme = params?.[0] === "dark";
const padding = 1;
const widget = new ListWidget();
if (isDarkTheme) {
widget.backgroundColor = new Color("#1C1C1E");
}
widget.setPadding(padding, padding, padding, padding);
widget.url = "https://www.coingecko.com/en/coins/rubic";
const headerStack = widget.addStack();
headerStack.setPadding(10, 0, 10, 0);
const headerText = headerStack.addText("BLUE");
headerText.font = Font.mediumSystemFont(16);
if (isDarkTheme) {
headerText.textColor = new Color("#FFFFFF");
}
async function buildWidget() {
const bluePriceInfo = await getTokenPriceInfo();
addCrypto("BLUE ", bluePriceInfo.avg, bluePriceInfo.buy, bluePriceInfo.sell);
}
function addCrypto(symbol, avg, buy, sell) {
const rowStack = widget.addStack();
rowStack.setPadding(0, 0, 0, 0);
rowStack.layoutHorizontally();
const buyStack = rowStack.addStack();
const sellStack = rowStack.addStack();
buyStack.setPadding(10, 0, 10, 10);
sellStack.setPadding(10, 0, 10, 10);
const buyText = buyStack.addText("" + buy.toString());
buyText.font = Font.mediumSystemFont(16);
const sellText = sellStack.addText("" + sell.toString());
sellText.font = Font.mediumSystemFont(16);
buyText.textColor = new Color("#FFF000");
if (isDarkTheme) {
buyText.textColor = new Color("#FFF000");
sellText.textColor = new Color("#FFFFFF");
}
}
async function getTokenPriceInfo() {
const url = "https://api.bluelytics.com.ar/v2/latest";
const req = new Request(url);
const apiResult = await req.loadJSON();
return {
avg: apiResult.blue.value_avg,
sell: apiResult.blue.value_sell,
buy: apiResult.blue.value_buy
};
}
async function loadImage(imgUrl) {
const req = new Request(imgUrl);
return await req.loadImage();
}
await buildWidget();
Script.setWidget(widget);
Script.complete();
widget.presentMedium();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment