SteemJs Programming: What Happens on the Steem Blockchain in the Last 24 Hours?


Many questions are interesting but the answers are not obvious or easy to get. For example: Who comments most in the last 24 hours?

I am going to show you a pattern to find out the answers to such questions using SteemJs.

Get Current Lastest Block

We need to get the latest block number on the steem blockchain, and we can work out roughly the last 24 hours block range by subtracting 206024 (3 seconds a block).

function getBlockchainData() {
    return new Promise((resolve, reject) => {
      steem.api.getDynamicGlobalProperties(function(err, result) {
          if (!err) {
              resolve(result);
          } else {
              reject(err);
          }
      });
    });
}

The current block number is:

    const blockchain = await getBlockchainData();
    const maxBlock = blockchain.head_block_number;

Getting the Block Content

Each block contains transactions, here you would need to parse the list of transactions and look for the particular ones that you are interested. For example, if we want to compute the total producer rewards by witnesses, we can look for "producer_reward".

function getBlock(block) {
    return new Promise((resolve, reject) => {
        steem.api.getOpsInBlock(block, false, function(err, result) {
          if (!err) {
              const txCount = result.filter(x=>x.virtual_op===0).length;
              const producer = result.filter(x=>x.op[0] === "producer_reward");
              const witness = producer[0].op[1].producer;
              const reward = producer[0].op[1].vesting_shares;
              resolve({
                count: txCount,
                witness: witness,
                reward: reward.replace(" VESTS", ""),
                time: producer[0].timestamp.replace("T", " ")
              });
          } else {
              reject(err);
          }        
        });
    });
}

Writing to Database or a File

We need to write the data into a database, or for simplicity, we can write to a CSV file for later process.

(async function() {
    const blockchain = await getBlockchainData();
    const maxBlock = blockchain.head_block_number;
    for (let block = maxBlock - 20*60*24; block <= maxBlock; ++ block) {
        try {
            const data  getBlock(block);
            const s  + data.time + '",' + block + ',"' + data.witness + '",' + data.count + "," + data.reward + "\n";
            log(s);
            fs.appendFileSync('data.csv', s);
        } catch (e) {
            log(e);
        }
    }  
})();

Of course, if you want to further develop into a tool, you would need a database, and a background daemon that automatically syncs wiith the [blockchain


Every little helps! I hope this helps!

*Steem On!~](https://helloacm.com/illustrating-the-blockchain-via-steemjs-blocks-are-chained/).


Every little helps! I hope this helps!

Steem On!~Reposted to Blog---------------

If you like my work, please consider voting for me, thanks!
https://steemit.com/~witnesses type in justyy and click VOTE



Alternatively, you could proxy to me if you are too lazy to vote!

Also: you can vote me at the tool I made: https://steemyy.com/witness-voting/?witness=justyy

Visit me at: https://steemyy.com


Comments 13


This is too complicated for me. Unfortunately, I didn’t understand anything. Thank you for your work. Good luck.

11.06.2020 19:53
0

Thanks for dropping by :)

11.06.2020 20:39
0

Same, my brain isn't geared for it. I'd need a translation in more simple terms what all that actually means.

12.06.2020 02:33
0

keep up the good work ,, respect , can you please use the tag #codeonsteem from next time you post any tools or tutorials ,, would be awesome

12.06.2020 00:53
0

Thank you. Good idea!

12.06.2020 11:56
0

i have applied for curator account and want to reward different level of developers and teachers on steem so we using the #codeonsteem

https://goldvoice.club/steem/@steem-supporter/application-for-community-curator-for-july

12.06.2020 14:55
0

Nice!

12.06.2020 19:18
0

给行长拍手支持👏!shop

12.06.2020 10:51
0

谢谢,回赞。

12.06.2020 11:56
0
Hi~ justyy! @annepink has gifted you 1 SHOP! ![]() Currently you have: 3 SHOP

View or Exchange SHOP Please go to steem-engine.com.

Are you bored? Play Rock,Paper,Scissors game with me!
12.06.2020 10:52
0

the last 24 hours block range by subtracting 206024

206024?

17.06.2020 05:32
0

My math is terrible... 20 x 60 x 24😅

17.06.2020 17:49
0

👍👏

18.06.2020 09:45
0