Domain Steem with JavaScript: Lesson #3 - Hearing the blockchain

in #devwithseven5 days ago

3.jpg

Created with canva

We have already managed to consult specific information about the blockchain, perform calculations and interpret values, however, we still do not know how to react or detect when something happens. For example, let's imagine that we want to vote for the publications that are made within the hashtag #steemexclusive automatically, how could we achieve it?

Well, there are several paths but the Steem JS library comes equipped with a method that allows us to listen to the Blockchain, the logic is simple but you have to understand how Steem works returning to our first lesson:

WhatsApp GIF 2024-12-15 at 14.14.12.gif

Animated with After Effects.

Every ~3 seconds a new block, full of transactions is added to Steem, what this method allows us is to give us the information every time a new block is created. This method is called streamOperations, here is an example:

const steem = require("steem");
steem.api.setOptions({ url: 'https://api.steemit.com' }); 

steem.api.streamOperations(function (err, operations) {
    operations.forEach(function (operation) {
      console.log(operation);
    });
});

Grabacindepantalla2024-12-28alas10.25.52p.m.-ezgif.com-video-to-gif-converter.gif

Each time a block is created we receive all its operations in the form of an array [transaction_type, {transaction_data}], in this way we can detect when a type of information happens. For example, we will show the operation only when they are votes:

const steem = require("steem");
steem.api.setOptions({ url: 'https://api.steemit.com' }); 

steem.api.streamOperations(function (err, operations) { 
    let type = operations[0];
    let data = operations[1]; 
    if (type == "vote") {
        console.log(data.voter, "voted in", data.author+"/"+data.permlink, "at", data.weight/100,"%"); 
        console.log("-----------------------------------");
    }
});

Grabacindepantalla2024-12-28alas10.49.15p.m.-ezgif.com-video-to-gif-converter.gif

In this way we manage to obtain real-time information of everything that happens in Steem, and with the help of transmission operations perform actions to react. For example, this way we could create a bot to vote for our favorite authors every time they publish, for example.

We will see the transmission operations later in this course, for now, let's continue simply manipulating and interpreting the information.


Homework


  • What uses would you give to the streamOperations method? (Not to mention those explained the lesson) [2 PTS]
  • Use the streamOperations method to detect the following types of transactions: comment, transfer, transfer_to_vesting, withdraw_vesting. Analyze the information contained in these transactions and show a message for each case. [5 PTS]
  • Hear the blockchain and detect when you give a vote with your own account (start the bot and then give votes, and show how you detect it) [3 PTS]

Rules


  • The content must be #steemexclusive.
  • The article must contain the tag #steemjs-s22w3.
  • Plagiarism is not allowed.
  • The link of your task must be added in the comments of this publication.
  • The course will be open for 7 days from 00:00 UTC on December 30. After the deadline, users will be able to continue participating without applying for prizes with the aim of allowing more people in time to take advantage of this content.
Sort:  
Loading...

This post has been upvoted by @italygame witness curation trail


If you like our work and want to support us, please consider to approve our witness




CLICK HERE 👇

Come and visit Italy Community



Hi @alejos7ven I hope you will be good. I have tried many times but I am unable to detect withdraw_vesting looks like no one is converting sp to steem. SO if I do not get any transaction in the blockchain for withdraw_vesting then can I just explain the properties and theoretical working of withdraw_vesting. Can the students use any other operation if you can change it.

I can suggest the following:

  • Create a new account or use another Steem account already created for testings.
  • Add 1-2 SP in that account.
  • Then start a power down and try to detect this operation

It means we just need to detect the power down or power up or power down stopped transactions because conversion of Steem power to liquid takes 7 days.

Yes just detect the power down start operation

Thank you. I was confuse in it and now I can proceed it.

Coin Marketplace

STEEM 0.25
TRX 0.27
JST 0.040
BTC 96876.24
ETH 3451.68
SBD 1.58