Skip to content

Instantly share code, notes, and snippets.

@abuvanth
Created February 8, 2024 12:01
Show Gist options
  • Save abuvanth/2c30f66d8c1109e51ac20cd63cd08bef to your computer and use it in GitHub Desktop.
Save abuvanth/2c30f66d8c1109e51ac20cd63cd08bef to your computer and use it in GitHub Desktop.
scriptable ios widget for showing cyberfly price in ios home
const params = args.widgetParameter ? args.widgetParameter.split(",") : [];
const isDarkTheme = params?.[0] === 'dark';
const padding = 2;
const widget = new ListWidget();
if (isDarkTheme) {
widget.backgroundColor = new Color('#1C1C1E');;
}
widget.setPadding(padding, padding, padding, padding);
widget.url = 'https://swap.ecko.finance/?token0=KDA&token1=CFLY'
const headerStack = widget.addStack();
headerStack.setPadding(0, 0, 25, 0);
const headerText = headerStack.addText("Crypto price");
headerText.font = Font.mediumSystemFont(16);
if (isDarkTheme) {
headerText.textColor = new Color('#FFFFFF');
}
async function buildWidget() {
const ethereumImage = await loadImage('https://cyberfly.io/assets/images/icons/cyberfly-logo-updated.png');
const ethereumPriceInfo = await getTokenPriceInfo();
const roundedEthereumPrice = Math.round(ethereumPriceInfo.price);
addCrypto(ethereumImage, 'CFLY',roundedEthereumPrice.toString()+'/kda', ethereumPriceInfo.grow);
}
function addCrypto(image, symbol, price, grow) {
const rowStack = widget.addStack();
rowStack.setPadding(0, 0, 20, 0);
rowStack.layoutHorizontally();
const imageStack = rowStack.addStack();
const symbolStack = rowStack.addStack();
const priceStack = rowStack.addStack();
imageStack.setPadding(0, 0, 0, 10);
symbolStack.setPadding(0, 0, 0, 8);
const imageNode = imageStack.addImage(image);
imageNode.imageSize = new Size(20, 20);
imageNode.leftAlignImage();
const symbolText = symbolStack.addText(symbol);
symbolText.font = Font.mediumSystemFont(16);
const priceText = priceStack.addText(price);
priceText.font = Font.mediumSystemFont(16);
if (isDarkTheme) {
symbolText.textColor = new Color('#FFFFFF');
}
if (grow) {
priceText.textColor = new Color('#4AA956');
} else {
priceText.textColor = new Color('#D22E2E');
}
}
async function getTokenPriceInfo() {
const url = 'https://backend2.euclabs.net/kadena-indexer/v1/account/obtKlgZj3CJcF2IzEkC4NC4qz2JLAZZZmLUb4oFCF5w'
const req = new Request(url)
const apiResult = await req.loadJSON()
return { price: apiResult['assets'][1].totalBalance/apiResult['assets'][0].totalBalance};
}
async function loadImage(imgUrl) {
const req = new Request(imgUrl)
return await req.loadImage()
}
await buildWidget();
Script.setWidget(widget);
Script.complete();
widget.presentSmall();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment