How to calculate a Hive account's value with Javascript (+ a live Telegram bot to fetch the values without coding)

in GEMS2 years ago (edited)

I know most people just open a browser to check how much their account is worth, and what the current prices are for Hive and HBD.

But... that's still too hard sometimes! Or too easy!

I made a small Telegram bot (@HiveToolkitBot) that so far only has one command. It is /value. (here's the source code)

image.png

Get the balances from your account

  • HBD and Hive

Getting the HBD and Hive balances is very easy:

const result = await client.database.getAccounts(["cryptosharon"])
const hbdBalance = parseFloat(result.hbd_balance);
const hiveBalance = parseFloat(result.balance);
  • Hive Power (Vests)

Getting the Hive Power is a tiny bit harder. Here's a condensed version in Javascript.

const { Client } = require("@hiveio/dhive");
const client = new Client("https://api.hive.blog");
async function main() {
    const { total_vesting_fund_hive, total_vesting_shares } = await client.database.call("get_dynamic_global_properties");
    const hivePerMvest = parseFloat(total_vesting_fund_hive) /
        (parseFloat(total_vesting_shares) / 1e6);
    const accs = await client.database.getAccounts(["cryptosharon"]);
    const vests = parseFloat(accs[0].vesting_shares);
    return (vests / 1e6) * hivePerMvest;
}

I actually made it in Typescript and then compiled to the above version using the Typescript Playground. I took all of these formulas from pharesim's hive-python. Hopefully one day it's rewritten (even by me!) to Typescript.

import { Client, DynamicGlobalProperties } from "@hiveio/dhive";

const client = new Client("https://api.hive.blog");

async function main() {
  const { total_vesting_fund_hive, total_vesting_shares } =
    (await client.database.call(
      "get_dynamic_global_properties"
    )) as DynamicGlobalProperties;
  const hivePerMvest =
    parseFloat(total_vesting_fund_hive as string) /
    (parseFloat(total_vesting_shares as string) / 1e6);
  const accs = await client.database.getAccounts(["cryptosharon"]);
  const vests = parseFloat(accs[0].vesting_shares as string);
  return (vests / 1e6) * hivePerMvest;
}
  • Savings HBD balances

This one I had forgotten, but it is important because now many people have some money saved up!

async function getSavingsBalance(client, account) {
    const acc = await client.database.getAccounts([account]);
    return {
        hbdPrice: parseFloat(acc[0].savings_hbd_balance),
        hivePrice: parseFloat(acc[0].savings_balance),
    };
}

Get the price of each token on Coingecko

import axios from "axios";

export async function getCoingeckoPrices(): Promise<{
  hbd: number;
  hive: number;
}> {
  const { data } = await axios.get(
    "https://api.coingecko.com/api/v3/simple/price?ids=hive,hive_dollar&vs_currencies=usd"
  );
  const { hive_dollar, hive } = data;
  return { hbd: hive_dollar.usd, hive: hive.usd };
}

Sum it all up

const valueInUsd = hbdPrice * (hbd + hbdSavings) + hivePrice * (hive + hp + hiveSavings);

This is very useful to make all kinds of tools, such as browser extensions, bots, alternative front-ends, etc.

If I learn how to get Hive token prices and other such things, I'll make sure to add them to the bot! If you know these things, please leave me a comment; I'd love to learn. :)


All of this code and a few more things are in this github repo if you'd like to check out the Typescript source: https://github.com/CryptoSharon/telegram-hive-account-value-bot

The code is also currently live as of Nov 30 2021 (and as long as my VPS survives) on https://t.me/HiveToolkitBot

Sort:  

This looks a very useful and simple bot, hope it stays up forever <3.

What license did you release your bot's source code as? Maybe I skipped over something but I couldn't tell from the Github repo.

And thanks i will strive to embiggen it and immortalize it.

Licenses are a spook. Just do whatever you want with the code you read.

It's been a while since this post but I wanted to suggest https://github.com/ccxt/ccxt as an easy solution to getting the current price information.

least of my problems for this project but for others i will find it useful. thank you very much for sharing this with me :)