The KE Ratio on PeakD with Tampermonkey - Just a Prototype

in #development2 months ago

Maybe some of you on HIVE have seen it before, there’s something called a KE Ratio for every account. It shows how much a user has earned from author rewards plus curation rewards over time. If you divide this total by the user's current Hive Power, you get the KE Ratio. It’s a small but interesting number that shows how efficiently someone is using their stake.

Some more details about this were shared by @azircon a while ago, and there’s even a cool tool where you can check your own value at: https://beebalanced.streamlit.app.

Today I saw someone asking about it in the Hive Chat, so I thought why not build a small prototype? And that’s what this is. A simple Tampermonkey script that shows the KE Ratio directly on PeakD.com, next to the reputation number of each user in your feed. It works like this: for every user shown in the post list, the script makes a request to the API by @techcoderx, gets the author rewards, curation rewards, and Hive Power, calculates the ratio, and then shows it nicely beside the reputation.

Current Trending - Some Screenshots of the Result

image.png

image.png


image.png

image.png


The Tampermonkey Script

// ==UserScript==
// @name         PeakD KE-Ratio Viewer (Styled)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Display KE-Ratio on PeakD next to user reputation label with consistent styling and tooltip
// @author       louis88
// @match        https://peakd.com/*
// @grant        GM_xmlhttpRequest
// @connect      techcoderx.com
// @run-at       document-end
// ==/UserScript==

(function () {
  'use strict';

  const KE_CACHE = {};

  function getUsernameFromHref(href) {
    const match = href.match(/\/@([\w\-.]+)/);
    return match ? match[1] : null;
  }

  function calculateKERatio(data) {
    const posting = parseFloat(data.posting_rewards) / 1000;
    const curation = parseFloat(data.curation_rewards) / 1000;
    const vesting = parseFloat(data.vesting_balance) / 1000;
    if (vesting === 0) return null;
    return ((posting + curation) / vesting).toFixed(2);
  }

  function fetchKE(username, callback) {
    if (KE_CACHE[username]) {
      callback(KE_CACHE[username]);
      return;
    }

    GM_xmlhttpRequest({
      method: 'GET',
      url: `https://techcoderx.com/hafbe-api/accounts/${username}`,
      onload: function (response) {
        try {
          const json = JSON.parse(response.responseText);
          const ke = calculateKERatio(json);
          KE_CACHE[username] = ke;
          callback(ke);
        } catch (e) {
          console.error("KE parse error", e);
          callback(null);
        }
      },
      onerror: function () {
        console.error("Failed to fetch KE data for", username);
        callback(null);
      }
    });
  }

  function insertKELabel(container, keRatio) {
    if (!keRatio) return;

    const label = document.createElement("span");
    label.textContent = `KE: ${keRatio}`;
    label.classList.add("label");
    label.title = "KE Ratio = (Author Rewards + Curation Rewards) / HP";

    // Match PeakD visual style a bit
    label.style.display = "inline-block";
    label.style.padding = "4px 6px 4px 6px";
    label.style.fontSize = "95%";
    label.style.fontWeight = "500";
    label.style.color = "#fff";
    label.style.backgroundColor = "#313437"; // Muted dark gray
    label.style.borderRadius = "0.25em";
    label.style.marginLeft = "6px";
    label.style.lineHeight = "1";
    label.style.verticalAlign = "baseline";
    label.style.whiteSpace = "nowrap";
    label.style.textAlign = "center";
    label.style.textTransform = "uppercase";
    label.style.letterSpacing = ".1px";

    container.appendChild(label);
  }

  function processUsers() {
    const userLinks = document.querySelectorAll('a[href^="/@"]');
    userLinks.forEach(link => {
      const username = getUsernameFromHref(link.getAttribute("href"));
      if (!username) return;

      const repLabel = link.parentElement?.querySelector(".reputation-label");
      if (!repLabel || repLabel.dataset.keInjected) return;

      repLabel.dataset.keInjected = "true";
      fetchKE(username, (ke) => {
        insertKELabel(repLabel.parentElement, ke);
      });
    });
  }

  // Initial run
  processUsers();

  // Run again on DOM changes (dynamic content)
  const observer = new MutationObserver(() => {
    processUsers();
  });

  observer.observe(document.body, { childList: true, subtree: true });
})();

To try it yourself, just install Tampermonkey on https://www.tampermonkey.net, copy the script into a new userscript, and visit PeakD. It will load automatically for each user you see in the normal Feeds.

That’s all for now. It’s just a prototype, but it works. Maybe I’ll add more features later like color coding or sorting, but for now, it does exactly what it needs to: show the KE Ratio live on PeakD.

More Reads about the KE-Ratio: https://peakd.com/@azircon/ke-ratio-as-a-guide


image.png

Vote for my Hive Witness

U can vote for my Witness using Hive Keychain here: https://vote.hive.uno/@louis.witness

image.png

Vote for my Hive Engine Witness

Vote for my Witness on Hive-Engine using Primersion Tool: https://primersion.com/he-witnesses Enter your Username and search for louis.witness

Sort:  

Awesome work.

The KE will be visible for everyone here in the next PeakD update 👍

image.png

Noice!

From Beta :)
image.png

This looks great, it's nice to see the KE number directly from the profile page now. Thanks for adding this feature and thanks @louis88 for making the script.

That is great, is it possible to make KE visible in feed views as well to help curating better?

Well, this thing is not telling the full story, to the curation + posting rewards add the witness rewards, and all transfers from curation programs. One can power up millions, delegate to the curation rewards, take millions out, and have a low score.

Loading...

Will it show up on comments, too? :)

Yes :)

Great now every can see how much I've powered down :D lol

I request the PeakD folks to put this in directly as a number on the front end @asgarth . There is enough momentum on this already and almost everyone is using it now. So whether people like it or not people are checking it and find this valuable.

In the next update KE will be visible on the User profile page. Already visible on https://beta.peakd.com

Could there be a user settings option to have it visible by all account names, so not having to go to the profile?

Possible, but would be much easier if implemented as a standard Hive API.

I can use the same approach showcased by @louis88 in the post, but that will put additional load on the API nodes.

That would be perfect I think

Greetings @asgarth ,

Is it possible for a user to remove the coloured KE notice from the profile screen.....for Bleujay the colour is distracting.....please assist.

Kindest Regards,

Bleujay

Oh, you mean the yellowish tint? Something more neutral/gray?

Yes....something more neutral...yes...gray similar to 'stats, 'rep number' box....yes please.

Refresh the page and let me know 👍

How very lovely!.....Thank you @asgarth !

You are the best!

Cool! KE Ratio on PeakD with Tampermonkey? 🐴💡 Checking my efficiency now! Thanks for sharing, might tweak my strategy!

Thank you so much for this work man, you've definitely inspired @asgarth. I don't know if KE is a sufficient indicator on its own, but it certainly says something.

That's pretty cool. It would be neat if you could color code it so that people who aren't familiar with the KE index can intuitively know if the score is good or bad? Like a green yellow red sort of thing.

I don't get the idea behind KE ratio at all. Seems like comparing past harvest of apples and plums to current number of orange trees.

Let's look at apples (author rewards) first. Author rewards depend on power of voters your post attracted, has no connection to your own stake at all. You can also get author-type rewards through mechanism of beneficiaries. Sum of author rewards is a reflection of your past performance as an author.

Now plums (curation rewards). While at first sight it looks like there is connection between your stake and curation, it is not entirely true. Curation depends on your voting power and you can get that through delegation. Accounts like @ocdb will have vastly inflated KE ratio because of that. On the other hand you can also delegate out your vesting shares which lessens your effective voting power and thus lowers your KE ratio. Like in case of author rewards, sum of curation rewards is a reflection of your past performance.

Vesting shares is a data on your current stake. If you power up a lot, it will deflate KE ratio, if you power down, it will get inflated.

Will try this later, great work... thanks!

EDIT: However I cant get it to work, after creating a copy. Nothing happens. I am missing something.

image.png

Using Brave, does it work with that browser?

I'm using chrome - Looks like it's missing the grant in line 8 and the connect techcoders domain on top

It looks like peakd already did this as standard, though yours shows more in the feed.

Very cool! It would be nice to have such feature implemented into the frontends, one day.

Cool! It would be even better, if it got implemented straight to the front end though :)

I love it. It is exactly what I was hoping that @peakd (@asgarth) would tackle in their next release!
Saves a lot of time for me as a curator, and people who don´t PD will benefit.

Congratulations @louis88! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You made more than 6500 comments.
Your next target is to reach 7000 comments.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Great work, simple but effective tool!

Excellent initiative, @louis88! Integrating the KE Ratio directly into PeakD with a Tampermonkey script is a valuable tool for the Hive community. It facilitates quick assessment of users' investment behavior, promoting greater transparency and engagement within the platform. Thank you for sharing this prototype; it will undoubtedly be of great use to many.

Way above my pay grade, to technical for my brain , but others think it’s good so I will follow the leader

This is extremely useful, thank you to everyone involved in working this out.

I say it and didn't like it...

Y yo pensando que había echo algo mal porque me salía ese recuadro junto a mi reputación 😅

This is just a flawed concept. If we want to tackle it seriously, just add the witness rewards and also the one coming from the curation accounts, to the total, then you have a real factor, everything else is just a showoff.

Loading...
Loading...