Domain Steem with JavaScript: Lesson #3 - Hearing the blockchain
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:
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);
});
});
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("-----------------------------------");
}
});
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.
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
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 ofwithdraw_vesting
. Can the students use any other operation if you can change it.I can suggest the following:
0.00 SBD,
0.00 STEEM,
0.00 SP
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
0.00 SBD,
0.04 STEEM,
0.04 SP
Thank you. I was confuse in it and now I can proceed it.