Skip to content

Instantly share code, notes, and snippets.

@inertia186
Last active December 14, 2023 22:37
Show Gist options
  • Save inertia186/cf00c4829d22055eb5ca0d867d83ec05 to your computer and use it in GitHub Desktop.
Save inertia186/cf00c4829d22055eb5ca0d867d83ec05 to your computer and use it in GitHub Desktop.
/*
* This script implements a Hive Ticker widget with the Scriptable API. The script is meant to be used with a widget
* configured on the Home Screen. To install, copy this script into Scriptable (available in the Apple App Store)
* and add a "small" Scriptable widget on the Home Screen. Then select this script.
*/
// === Remote Data ===
const rpc = hiveRequest('https://api.hive.blog');
const hive = await new Request('https://api.coingecko.com/api/v3/coins/hive').loadJSON();
const hbd = await new Request('https://api.coingecko.com/api/v3/coins/hive_dollar').loadJSON();
const dgpo = await loadDynamicGlobalProperties(rpc);
const feed_history = await loadFeedHistory(rpc);
// === Financial Data ===
const usdt_hive_ticker = findRandomTicker('HIVE', 'USDT');
const usdt_hive = usdt_hive_ticker['last'];
const market_name = usdt_hive_ticker['market']['name'];
const total_vesting_fund_base = parseAsset(dgpo['total_vesting_fund_hive']);
const total_vesting_shares_mvest = parseAsset(dgpo['total_vesting_shares']) / 1e6;
const base_per_mvest = (total_vesting_fund_base / total_vesting_shares_mvest).toFixed(3);
const current_median_history = feed_history['current_median_history'];
const base = parseAsset(current_median_history['base']), quote = parseAsset(current_median_history['quote']);
const base_per_debt = ((base / quote) * base_per_mvest).toFixed(3);
const base_per_debt_as_usdt = (base_per_mvest * usdt_hive).toFixed(3);
// === Visual Layout ===
const widget = hiveWidget(), stack = addStack(widget);
const image = await addImageUrl(stack, hive["image"]["large"]);
const infoStack = stack.addStack(), priceStack = addPriceStack(infoStack);
const headBlockNum = infoStack.addText(`Block: ${dgpo['head_block_number']}`);
const hivePrice = priceStack.addText(`HIVE: $${hive["market_data"]["current_price"]["usd"].toFixed(3)}`);
const hbdPrice = priceStack.addText(`HBD: $${hbd["market_data"]["current_price"]["usd"].toFixed(3)}`);
const mvests = widget.addText(`1MV\n = 1M VESTS\n = ${base_per_mvest} HIVE\n = $${base_per_debt}\n = $${base_per_debt_as_usdt} on ${market_name}`);
hivePrice.font = Font.systemFont(10);
hbdPrice.font = Font.systemFont(10);
headBlockNum.font = Font.systemFont(7);
mvests.font = Font.regularMonospacedSystemFont(11);
Script.setWidget(widget);
Script.complete();
widget.presentSmall();
function findRandomTicker ( base, target ) {
const tickers = hive['tickers'];
let ticker = null;
while ( ticker == null ) {
const random = Math.floor(Math.random() * tickers.length);
const randomTicker = tickers[random];
if ( randomTicker['base'] === base && randomTicker['target'] === target ) {
ticker = randomTicker;
}
}
return ticker;
}
function hiveWidget ( ) {
const widget = new ListWidget();
widget.refreshAfter = new Date(Date.now() + (15 * 60 * 1000));
widget.url = 'https://coingecko.com/en/coins/hive';
return widget;
}
function addStack ( widget ) {
const stack = widget.addStack();
stack.layoutHorizontally();
stack.centerAlignContent();
return stack;
}
async function addImageUrl ( stack, url ) {
const image = await new Request(url).loadImage();
stack.addImage(image);
stack.addSpacer();
return image;
}
function addPriceStack ( stack ) {
const priceStack = stack.addStack();
stack.layoutVertically();
priceStack.layoutVertically();
return priceStack;
}
function hiveRequest ( url ) {
const rpc = new Request(url);
rpc.method = 'POST';
rpc.headers = {"Content-Type":"application/json"};
return rpc;
}
/**
* See: https://developers.hive.io/apidefinitions/#database_api.get_dynamic_global_properties
*/
async function loadDynamicGlobalProperties ( rpc ) {
rpc.body = '{"jsonrpc":"2.0", "method":"database_api.get_dynamic_global_properties", "id":1}';
const response = await rpc.loadJSON();
return response['result']
}
/**
* See: https://developers.hive.io/apidefinitions/#database_api.get_feed_history
*/
async function loadFeedHistory ( rpc ) {
rpc.body = '{"jsonrpc":"2.0", "method":"database_api.get_feed_history", "id":1}';
const response = await rpc.loadJSON();
return response['result'];
}
function parseAsset ( value ) {
return parseFloat(value['amount']) / value['precision'] ** 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment