Domain Steem with JavaScript: Lesson #4 - Broadcasting operations part 1: Transferring, and posting
Hello steemians,
Here is my homework for SLC22 Week 3, with the corresponding tasks assigned by @alejos7ven!
Create a bot that detects when you vote for someone else's publication, when detecting your vote creates an automatic comment on that publication you voted for (the app that created your comment should be called your username and version 1.0, example app:'alejos7ven/1.0')
In this code configures the account details and initializes the necessary Steem library, it first includes the steem
library via require("steem")
, then it defines two constants: username
, which contains the name of 'user on Steem (in this case, "kouba01"), and privatePostingKey
, which stores the private posting key needed to perform secure operations like commenting, this information allows interaction with the blockchain Steem while securing actions.
Also a function named listenForVotes
is defined which is designed to listen in real time for votes on the Steem blockchain using the steem.api.streamOperations
method, it monitors every operation performed on the blockchain and filters only those that are of type vote
, if a vote is detected, and the user originating the vote matches the configured username
(i.e. "kouba01"), the code automatically creates a comment on the voted publication.
In the body of the function, when the vote is identified, a log string displays the vote details (the author and permalink of the post), then a comment is prepared and sent, a unique permalink identifier (permlink
) is generated by combining a fixed string with a timestamp, the comment body is customized to include a message indicating that the comment was automatically generated with mention of the application used (in this case the name of 'user followed by the version, kouba01/1.0
).
The steem.broadcast.comment
method is used to post the comment, taking several parameters like post private key, parent post author, parent post permalink, username, unique permanent link, an empty title (because this is not a classic post), the body of the comment, and JSON metadata containing information about the application used.
As we have implemented an error handler to capture any error that may occur during the broadcast of the comment, if the comment is posted successfully, a success message is displayed in the console, the listenForVotes
function is called at the end to start the listening process.
Output
Use SteemWorld, Ecosynthesizer, or any other explorer to show the transactions you have created, leave link just like me.
I used Ecosynthesizer to view an overview of my recent activities on the Steem blockchain, where I could see that I had voted on several posts using 40% voting power, while leaving automated comments immediately after each vote, these activities are recorded in a clear list, where each line corresponds to a distinct action, and I noticed that the associated timestamps allow you to see precisely at what time each vote and each response was made, By examining this list , I was able to check that my bot-generated comments were correctly associated with the posts I had upvoted, showing a perfect match between actions.
I looked in detail at one of the specific transactions corresponding to a vote I made on the post of a user identified by the pseudonym paholags, and discovered that this transaction contained several technical elements like the transaction_id
, the reference blocks, and the cryptographic signatures, I noticed that the weight of my vote was indicated precisely, corresponding to 40% of my voting power (4000), and that this operation was recorded from transparent way in block 92,029,875 at a specific time, which confirms the reliability and integrity of the data recorded on the blockchain.
https://ecosynthesizer.com/steem/trx/d5197a4e8ca814d50e7e2788f3d8004debbe2148
I also analyzed another transaction where I left an auto-generated comment on the post I upvoted, and found that this transaction contained specific information about the comment, like the parent_author
and the parent_permlink
, which clearly identify the target post, looking at the associated metadata I saw that my app had indeed used the personalization information with app:"kouba01/1.0"
, confirming that I had configured my scripted correctly to include this information, the comment content showed an automatically generated thank you message, and I verified that the timestamp matched exactly the time the comment was posted, demonstrating that my bot was working as expected.
https://ecosynthesizer.com/steem/trx/12018c21834542a2381fb99c29590eb663b053d7
Transfer 0.001 STEEM to @eight888 using Steem JS, add in the memo the title of this lesson.
In this code, I used the Steem-JS library by importing it with require("steem")
, which allowed me to access the functionality needed to interact with the Steem blockchain, I then defined two main constants, username
and privateActiveKey
, where I respectively assigned my username and my active private key to authenticate operations on my Steem account, these two elements were essential so that the transfer can be authorized and validated by the network.
I created a function called transfer_Steem
to perform a transfer of 0.001 STEEM to the account eight888
, I defined the necessary constants like the recipient, the amount and a memo indicating that this was the lesson number 4. I used the steem.broadcast.transfer
method to execute the transaction by providing the required parameters including my private active key, then I called the function to initiate the transfer, which allowed me to complete the transaction successfully, after that I went to SteemWorld to verify the transaction and confirmed that it had indeed been recorded on the blockchain, it showed a transfer of 0.001 STEEM with the correct memo, thus validating the correct operation of the code.
Another image shows the result on the blockchain, where I verified that the transaction had been completed with the memo displayed correctly, which proves that the transfer was successful and that the memo was correctly included in the transaction.
Thank you very much for reading, it's time to invite my friends @pelon53, @steemdoctor1, @sahar78 to participate in this contest.
Best Regards,
@kouba01