Modern Monetary Reality on your lockscreen. #bitcoin
“BTC Price in Gold Ounces
27.79 XAU”
Code for iphone via #einundzwanzig🧡:
👇🏻
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: bolt;
// Change currency to XAU
let currency = "XAU";
// Fetch current BTC price in USD from Coingecko
let req_btc = new Request('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd');
let btcPrice;
try {
let btcData = await req_btc.loadJSON();
btcPrice = btcData.bitcoin.usd;
console.log("BTC Price in USD: " + btcPrice);
} catch (error) {
console.error("Error fetching BTC price: " + error);
}
// Fetch current gold price in USD from Finnhub
let req_gold = new Request('https://finnhub.io/api/v1/quote?symbol=GC=F&token=c34391qad3i8edlcgrgg');
let goldPrice;
try {
let goldData = await req_gold.loadJSON();
goldPrice = goldData.c; // 'c' is the current price
console.log("Gold Price per ounce in USD: " + goldPrice);
} catch (error) {
console.error("Error fetching gold price: " + error);
}
// Check if prices are correctly fetched and parsed
if (typeof btcPrice === 'number' && typeof goldPrice === 'number') {
// Calculate BTC price in terms of gold ounces
let btcInGoldOunces = btcPrice / goldPrice;
console.log("BTC Price in Gold Ounces: " + btcInGoldOunces);
// Create widget
let widget = new ListWidget();
widget.backgroundColor = new Color("#151515");
// Title
let title = widget.addText("BTC Price in Gold Ounces");
title.centerAlignText();
title.font = Font.boldSystemFont(16);
title.textColor = new Color("#FFFFFF");
widget.addSpacer(8);
// BTC in Gold Ounces
let btcGoldText = widget.addText(btcInGoldOunces.toFixed(2) + " XAU");
btcGoldText.centerAlignText();
btcGoldText.font = Font.boldSystemFont(24);
btcGoldText.textColor = new Color("#F7931A");
// Refresh widget
let nextRefresh = Date.now() + 1000*60*10; // Refresh every 10 minutes
widget.refreshAfterDate = new Date(nextRefresh);
// 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.presentMedium();
}
Script.complete();
} else {
console.error("Invalid data fetched for prices. BTC: " + btcPrice + ", Gold: " + goldPrice);
} 