Save Your Curation Rewards Grouped By Date Using Node JS

in #steem3 years ago


We all have seen a rewards table right at the top of our profile page. You can get a idea about your daily curation and author SP, and earnings of SBD and STEEM for last 6 days. This is an awesome website to check anyone's earnings stats and activities.

This got me thinking how much curation rewards I have earned daily from the day I joined Steem. So, I wrote this Node.JS script to loop through my account history to check and group curations rewards by their date.

This function do all the looping through - 5000 items per loop. It checked for curation reward entries and push them to an array with their date for further processing. When looping is completed the callback groups all the records by their dates from that array and returns an object.

const getCuration = async (account, start, callback) => {
  let lastTrans = start;

  await'get_account_history', [account, start, (start < 0) ? 5000 : Math.min(start, 5000)])
    .then((result) => {

      for (let i = 0; i < result.length; i += 1) {
        const trans = result[i];
        const { op } = trans[1];

        // Checking on curation rewards and pushing them to an array by their date
        if (op[0] === 'curation_reward') {
          const rdate = new Date(`${trans[1].timestamp}Z`);
          const ndate = `${rdate.getMonth() + 1}/${rdate.getDate()}/${rdate.getFullYear()}`;

          curationReward.push({ time: ndate, reward: op[1].reward });

        // Save the ID of the last transaction that was processed.
        [lastTrans] = trans;

      if (lastTrans > 0 && lastTrans !== start) {
        // Looping through all the account history
        getCuration(account, lastTrans, callback);
      } else {
        // Grouping the rewards by date and making a new object
        callback(curationReward.reduce((acc, obj) => {
          const key = obj.time;
          if (!acc[key]) {
            acc[key] = [];
          acc[key].push(obj.reward.replace(' VESTS', ''));
          return acc;
        }, {}));

In the Self-Invoking anonymous function we call the above function and process the returned object. We add all the VEST for each day, convert them to SP, and save them to another array with their dates to write to a readable JSON file.

  // Replace `reazuliqbal` with your username
  await getCuration('reazuliqbal', -1, (data) => {
    const rewards = [];

    // Adding all rewards for each day and converting them to SP
    Object.keys(data).forEach((d) => {
      const reward = data[d].reduce((a, b) => a + parseFloat(b), 0);
      rewards.push({ date: d, reward: `${vestToSP(reward)} SP` });

    // Writing the result to a readable JSON file
    fs.writeFileSync('rewards.json', JSON.stringify(rewards, null, 4));

For a old account with lots of activities it will take a long time depending on your internet connection and device. For my account in my laptop it takes 30 seconds or so.

Check out the full code on this Gist and if you have any suggestions, feel free to comment.

Code screenshot was taken using this awesome project.


To listen to the audio version of this article click on the play image.

Brought to you by @tts. If you find it useful please consider upvoting this reply.


you just received a 15.80% upvote from @steemhq - Community Bot!

Wanna join and receive free upvotes yourself?
Vote for steemhq.witness on Steemit or directly on SteemConnect and join the Community Witness.

This service was brought to you by

You have recieved a free upvote from @saiduzzaman . For Nice Article . Carry On Brother