steem.api.streamOperations() for monitoring the steem blockchain with steem-js

in #utopian-io7 years ago (edited)

The steem-js library provides a handy mechanism that can be used to monitor the blockchain, but it's largely undocumented. This tutorial is meant to serve as a gentle introduction.

The method I'm talking about is:

steem.api.streamOperations()

streamOperations() takes a single callback function as an argument:

steem.api.streamOperations((err, result) => {
        console.log(err, result);
});

The output is a stream of STEEM blockchain operations as they happen in near realtime. Each operation is represented as a two field array where the first field is the operation type (string) and the second field is an object with the relevant metadata. If you run the function above as is, it might be a little noisy for your initial exploration so let's break it down a little bit by looking at only the the different types of operations flying by:

steem.api.streamOperations((err, result) => {
        console.log(result[0]);
});

Here's a partial list of some of the stuff you might see:

comment
vote
custom_json
transfer
account_create_with_delegation
claim_reward_balance
comment_options

The beauty of the blockchain is that all of the operations and transactions are transparent. With access to this stream, we could begin to create a bot that acts in response to certain operations. Suppose we wanted to create a bot to notify us of any comments on our posts as they come in. First, we need to learn a little bit about the structure of comment so that we can filter comments that belong only to our posts.

I would start by modifying the code to display the object keys only for comment operations:

steem.api.streamOperations((err, result) => {
        if (result[0] == 'comment'){
            console.log(Object.keys(result[1]));
        }
});

The output should look something like this:

[ 'parent_author',
  'parent_permlink',
  'author',
  'permlink',
  'title',
  'body',
  'json_metadata' ]
[ 'parent_author',
  'parent_permlink',
  'author',
  'permlink',
  'title',
  'body',
  'json_metadata' ]

Piece of cake! We've learned that we have 'parent_author' available for each comment, so we could now setup our streamOperations() function to only filter out comments where we are the parent:

steem.api.streamOperations((err, result) => {
        if ((result[0] == 'comment') && (result[1].parent_author == 'sha256md5')){
            // do what we want to do
        }
});

From here we could go on to do some more robust bot development, but that's a topic for future tutorials. It is probably worth mentioning that this is not a good way to run a critical bot in production. Steem API endpoints can have some wonky connectivity issues, and so can NodeJS scripts. Even a small amount of downtime can mean that the script misses critical events.

This is certainly a good way to do some fun automation, but if you're building something that relies on transactions where STEEM $ is involved, then your bot should account for edge cases by reconciling any events that it might have missed during occasional downtime.

Happy hacking!

Screen Shot 2018-01-18 at 9.00.25 PM.png



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

You can contact us on Discord.
[utopian-moderator]

Thanks, I appreciate the feedback. I'm looking forward to sharing more detailed contributions in the future.

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.035
BTC 91157.44
ETH 3167.61
USDT 1.00
SBD 3.12