Hive Developer logo

Hive Developer Portal

JS: Account Reputation

Learn how to interpret account reputation.

Full, runnable src of Account Reputation can be downloaded as part of: tutorials/javascript.

This tutorial runs on the main Hive blockchain. And accounts queried are real users with reputation.

Intro

This tutorial will show the method of capturing a queried tag name and matching it to the Hive. We are using the call function provided by the dsteem library to pull accounts from the Hive blockchain. A simple HTML interface is used to both capture the string query as well as display the completed search.

steps

  1. App setup Configuration of dsteem to use the proper connection and network.
  2. Search account Collecting the relevant search criteria
  3. Interpret account reputation Running the search and interpreting reputation.
  4. Output Displaying the results

1. App setup

Below we have dsteem pointing to the production network with the proper chainId, addressPrefix, and endpoint. There is a public/app.js file which holds the Javascript segment of this tutorial. In the first few lines we define the configured library and packages:

const dsteem = require('dsteem');
let opts = {};
//connect to production server
opts.addressPrefix = 'STM';
opts.chainId =
    'beeab0de00000000000000000000000000000000000000000000000000000000';
//connect to server which is connected to the network/production
const client = new dsteem.Client('https://api.hive.blog');

2. Search account

Collecting of the search criteria happens via an HTML input. The form can be found in the index.html file. The values are pulled from that screen with the below:

const max = 5;
window.submitAcc = async () => {
    const accSearch = document.getElementById('username').value;

3. Interpret account reputation

In order to get accounts, we run the search with the search field and maximum list items as parameters.

const _accounts = await client.database.call('lookup_accounts',[accSearch, max]);

The result of the search is an array of accounts. After that we use get_accounts to pull account data from Hive.

const acc = await client.database.call('get_accounts',[_accounts]);

And we loop through each account to convert their reputation to human readable format with following function:

function log10(str) {
    const leadingDigits = parseInt(str.substring(0, 4));
    const log = Math.log(leadingDigits) / Math.LN10 + 0.00000001;
    const n = str.length - 1;
    return n + (log - parseInt(log));
}

export const repLog10 = rep2 => {
    if (rep2 == null) return rep2;
    let rep = String(rep2);
    const neg = rep.charAt(0) === '-';
    rep = neg ? rep.substring(1) : rep;

    let out = log10(rep);
    if (isNaN(out)) out = 0;
    out = Math.max(out - 9, 0); // @ -9, $0.50 earned is approx magnitude 1
    out = (neg ? -1 : 1) * out;
    out = out * 9 + 25; // 9 points per magnitude. center at 25
    // base-line 0 to darken and < 0 to auto hide (grep rephide)
    out = parseInt(out);
    return out;
};

4. Output

After each account’s reputation is interpreted we can then display them on screen with readable reputation.

//disply list of account names and reputation with line breaks
for (var i = 0; i < _accounts.length; i++) {
    _accounts[i] = `${_accounts[i]} - ${repLog10(acc[i].reputation)}`;
}
document.getElementById('accList').innerHTML = _accounts.join('<br/>');

That’s it!

To run this tutorial

  1. git clone https://gitlab.syncad.com/hive/devportal.git
  2. cd devportal/tutorials/javascript/20_account_reputation
  3. npm i
  4. npm i
  5. npm run dev-server or npm run start
  6. After a few moments, the server should be running at http://localhost:3000/