Mastering Steem Blockchain Operations with JavaScriptsteemCreated with Sketch.

in steemjs-s22w4 •  2 days ago 

🌟 Mastering Steem Blockchain Operations 🌟
==============================================

Hey friends. I’ve come across an exciting challenge that I’m eager to join this week. It’s called Domain Steem with JavaScript: Lesson #4 - Broadcasting operations part 1: Transferring and Posting, hosted by @alejos7ven. You can find more details and participate through this link: Domain Steem with JavaScript Lesson #4.

Lesson Banner
Image Source: Canva


Today, we will explore an advanced JavaScript application that interacts on the Steem blockchain. This tutorial will show each component of our code, to explaining how it works and why each decision was made. Let's dive in!


Setting Up the Environment


Capture d’écran 2025-01-08 164140.png

These first lines set up our development environment:

  • We import the steem library to commuincate with the Steem blockchain .
  • readlineSync allows us to get user enter from the command line .
  • We define empty constants for account credentials that should be filled with your Steem account details ( you should complete this )

Delegation System


Capture d’écran 2025-01-08 172745.png

The Global Properties Helper:

  • Creates a Promise wrapper around getDynamicGlobalProperties
  • Calculates per_mvest value by:
    • Converting total vesting fund to float .
    • Multiplying by 10^6 (for MVESTS calculation) .
    • Dividing with total vesting shares
  • Returns global properties data

Capture d’écran 2025-01-08 172715.png

SP to VESTS Conversion:

  • Converts (SP) to VESTS
  • Takes SP ( steep power ) amount and per_mvests rate as parameters
  • Uses this formula : (SP * 1000000 / per_mvests)
  • Returns the result ( 6 decimal places )

Capture d’écran 2025-01-08 172633.png

The Delegation Process:

  • gets current global properties
  • ask user for:
    • Delegatee account name
    • Amount of SP to delegate
  • Converts SP to VESTS using helper function
  • Uses delegateVestingShares to perform the delegation
  • Requires ACTIVE_KEY for security (you soulkd enter this)
  • Includes comprehensive error handling

Witness Voting Implementation


Capture d’écran 2025-01-08 164325.png

This function demonstrates witness voting mechanics:

  • It's defined as async to handle asynchronous blockchain operations
  • We prompt the user to input a witness account name for testing

`
The witness voting process:

  • Uses Promise to handle asynchronous operations properly
  • Calls steem.broadcast.accountWitnessVote with parameters:
    • ACTIVE_KEY: Required for witness voting operations
    • ACCOUNT_NAME: Your account name
    • witness: The witness account name
    • false: Indicates vote removal
    • Callback function to handle results

Comment Posting System


Capture d’écran 2025-01-08 164416.png

The comment posting function:

  • Takes author and permlink parameters to identify the post
  • Creates a standard comment body
  • Generates metadata including the app name and version

Comment broadcasting details:

  • Uses POSTING_KEY for authentication
  • Creates a unique permlink by combining 're-', original permlink, and timestamp
  • Empty title string as it's a comment
  • Includes the comment body and metadata

Vote Detection System


Capture d’écran 2025-01-08 164448.png

The vote detection system:

  • Uses streamOperations to monitor blockchain operations in real-time
  • Destructures the operation result into type and details
  • Checks if the operation is a vote from our account
  • Automatically posts a comment when a vote is detected

Interactive Menu System


Capture d’écran 2025-01-08 175316.png

User interface implementation:

  • Displays a clear menu of available options
  • Uses questionInt to ensure numeric input
  • Returns the user's selection

Main Program Execution


Capture d’écran 2025-01-08 175331.png

The main execution loop:

  • Wrapped in an IIFE (Immediately Invoked Function Expression)
  • Uses async/await for proper asynchronous operation handling
  • Implements an infinite loop with menu selection
  • Includes error handling for robustness
  • Provides clean program termination

Security Considerations


When using this code:

  1. Never share your private keys
  2. Store keys in environment variables

Testing the Application


To test this application:

  1. enter your account credentials
  2. Run the program using Node.js
  3. Test witness voting with a known witness account
  4. Try the vote detection system
  5. Verify comments are posted correctly

Steps for Testing the Bot and Transaction Tracking:

1. Run the Bot and Start Detection:

  • Run the app according to the command provided in the image.

Capture d’écran 2025-01-08 165129.png

  • After starting, the menu will appear, and you will choose Option 2 to start vote detection.

Capture d’écran 2025-01-08 182428.png

2. Vote Detection and Automatic Comment Posting:

  • Vote on the publication SCT 2025-01-09.
  • When the bot detects your vote, it will automatically post a comment.

Here's what happens:

  • Before voting, there are no comments.

Capture d’écran 2025-01-08 165914.png

  • After voting, the bot detects the vote and posts a comment automatically.

Capture d’écran 2025-01-08 170033.png

3. Verifying the Comment:

  • The comment is posted

Capture d’écran 2025-01-08 170154.png

  • You can also view the post and comment properties using Ecosynthesizer:

Post link with app info

  • The app is identified as: "app": "kafio/1.0"

Capture d’écran 2025-01-08 170642.png

4. Transaction Tracking with SteemWorld or Ecosynthesizer:

  • Perform a delegation, upvote, and reply, then use SteemWorld or Ecosynthesizer to verify the transactions.

Here are the links to verify the transactions in Ecosynthesizer:

Capture d’écran 2025-01-08 173156.png

5. Witness Vote (AccountWitnessVote Method):

documentation of AccountWitnessVote Method : https://github.com/steemit/steem-js/tree/master/doc#account-witness-vote

  • Since you don't have STEEM to transfer, you replaced the task with an AccountWitnessVote method. You can approve or unapprove a witness.

To choose a witness, visit Steemit Wallet Witnesses.

  • Select the witness (e.g., "steemchiller").

Capture d’écran 2025-01-08 174839.png

Capture d’écran 2025-01-08 175213.png


Conclusion

on this tutorial we show to interact with the Steem blockchain using JavaScript , We explored key features such as delegating Steem Power, voting for witnesses, posting comments automatically when voting, and handling blockchain transactions securely sol lucky. showcasing the blockchain's transparency , Overall, this guide offers developers the tools to automate and enhance their interactions with the Steem blockchain.


I want to invite @naja, @maira27and @ransylove-1 join the contest.


My introduction post on Steemit: link


About the Author

👋 Hi, I’m Kafio!
Software Engineer | Data Science Enthusiast | Trading Buff | Development Geek | Computer Science Lover 💻📊📈

I’m passionate about exploring the intersection of technology and innovation, with a special interest in data science, trading, and software development. Whether it’s diving into the latest in computer science or developing new projects, I’m always excited to learn and share insights. 🚀

I also love to travel

Got questions or just want to connect? Feel free to reach out to me at: [email protected] 🌟

image00001.jpeg


Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  2 days ago 

Upvoted! Thank you for supporting witness @jswit.

  ·  2 days ago 

the post is shared on Twitter: Twitter Link.

Capture d’écran 2025-01-08 223321.png

Hello @kafio, thank you for sharing your quality content here. Because of your contribution in the platform I just voted your post! Keep up the good work.