SteemJs Programming: What Happens on the Steem Blockchain in the

  • 时间:2020-09-08 11:19:41
  • 分类:网络文摘
  • 阅读:80 次

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).

1
2
3
4
5
6
7
8
9
10
11
function getBlockchainData() {
    return new Promise((resolve, reject) => {
      steem.api.getDynamicGlobalProperties(function(err, result) {
          if (!err) {
              resolve(result);
          } else {
              reject(err);
          }
      });
    });
}
function getBlockchainData() {
    return new Promise((resolve, reject) => {
      steem.api.getDynamicGlobalProperties(function(err, result) {
          if (!err) {
              resolve(result);
          } else {
              reject(err);
          }
      });
    });
}

The current block number can be obtained by:

1
2
const blockchain = await getBlockchainData();
const maxBlock = blockchain.head_block_number;
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”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
          }        
        });
    });
}
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(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 = await 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);
        }
    }  
})();
(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 = await 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 with the blockchain.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
Using CloudFlare Worker Serverless Technology to Deploy a Load B  Algorithms to Compute the Math Power(a, n) in logarithm Complexi  Multithreading Testing using Pythons’ Low Level _threading  Algorithm to Count the Number of Substrings With Only 1s  How to Find Root of N-Ary Tree using the Hash Set?  How Many Blocks and Total Rewards for a Steem Witness in the Pas  Compute the Maximum Score After Splitting a String  A Simple HTML Entity Parser in C++  Dynamic Algorithm to Compute the Longest Common Subsequence  Algorithms to Locate the Leftmost Column with at Least a One in  
评论列表
添加评论