CryptoSharon avatar

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

cryptosharon

Published: 30 Nov 2021 › Updated: 30 Nov 2021How to calculate a Hive account's value with Javascript (+ a live Telegram bot to fetch the values without coding)

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

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

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

Written by

The lost will always find a way.

Read more #programming posts


Best Posts From CryptoSharon

We have not curated any of cryptosharon's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From CryptoSharon