Domain Steem with JavaScript: Lesson #4 - Broadcasting operations part 1: Transferring, and posting.

4.jpg

Created with canva

We have already learned the essentials to be able to read and interpret the information in the blockchain, now we go to the fun part of the matter, we are going to start writing transactions directly from our code, which will allow us to start creating applications capable of reacting without us being present.

To perform operations in Steem we will require a library such as Steem JS or DSteem, since POST queries do not support this type of transactions, so from here we will work only with libraries.

Broadcasting operations


To perform operations the Steem JS library has methods for the most common, you can see the guide here:

For this occasion I have created an account that I will dedicate exclusively to tests, and thus not fill my main account with spam since we are going to perform several actions, this account is @eight888 and will only have that purpose, once the course is over the account will be inactive and will only be used again in case of future activities for educational or practical purposes.

As you can see, it is a totally clean account, so the first thing we need to do is add SP to be able to perform operations. Today all the operations that we are going to carry out will be with code to practice to the maximum, we are going to create the script to make delegations.

To make a delegation we need to take into account a number of things:

1- The API must receive the amount to delegate in vesting_shares, but we are not going to enter quantities in this unit since we know it is working with SP, so we need to obtain the amount of STEEM PER MILLION VESTING to perform the conversion, for this we have a function that returns this data and calculates the desired value automatically:

Captura de pantalla 2025-01-05 a la(s) 1.53.10 p. m..png

2- The formula we saw earlier is used to convert vesting_shares to SP, so we must invert it to obtain the desired result, it would look like this:

SP * 1000000 / per_mvests

For this we create a function

Captura de pantalla 2025-01-05 a la(s) 1.55.59 p. m..png

Having this, we can use the method steem.broadcast.delegateVestingShares to make the delegation, we will need the account that you are going to delegate with your ACTIVE KEY, the account that you are going to receive and the amount in SP, we will ask the user for this data. After having obtained this information we transform the amount of SP to vesting shares, and finally we execute the operation.

We have installed the readline-sync library in node to be able to ask the user for data from the console.

Captura de pantalla 2025-01-05 a la(s) 2.09.43 p. m..png

Captura de pantalla 2025-01-05 a la(s) 2.10.14 p. m..png

It is important to mention that the amount in VESTING SHARES must have 6 decimals, for this you can use the .toFixed(6) method to correctly format the amount. It is also important to add the VESTS symbol.

Once the operation is executed, we check our account and we already have the delegated SP:

Captura de pantalla 2025-01-05 a la(s) 2.10.50 p. m..png

We have fulfilled our task, now we can use this new account to perform operations.


Creating a post


To create a post we will use the method steem.broadcast.comment, this method will require the following data:

  • parent_author: This value is left blank, it is only used if you are going to comment on a post from someone else, if you create a post of 0, you only have an empty string.
  • parent_permlink: Again if you are creating a post of 0, in parent_permlink you must place the tag of the community where you will publish or the first tag of your post if it will be on your blog.

Captura de pantalla 2025-01-05 a la(s) 2.47.25 p. m..png

  • author: the name of the account to publish (private posting key required)
  • permlink: it is the unique link to your publication, you must create an algorithm to create a unique and random permlink preferably, the permlink is what goes in the url right after your user.

Captura de pantalla 2025-01-05 a la(s) 2.50.55 p. m..png

  • title: the title of the post.
  • body: the content of the post.
  • json_metadata: some important metadata, this section includes tags, and information such as from which app the post was published.

This is the code:

let steem = require("steem");
const readlineSync = require('readline-sync');
(async ()=> {
    let tags = ["steem", "development", "steemjs"];
    let post = {
        author:"eight888",
        title:"Hello world! This is a post for the Steem JS Course",
        body:`
![Captura de pantalla 2025-01-05 a la(s) 2.34.02 p. m..png](

Hello!

This is a post created using Steem JS code for the Steem JS course. In this lesson you are learning how to broadcast operations. You can follow the course here:

|Thumb|Post|
|---|---|
||[Domain Steem with JavaScript: Lesson #1 - Introduction to Steem Blockchain and RPC Nodes](https://steemit.com/@alejos7ven/domain-steem-with-javascript-lesson-1-introduction-to-steem-blockchain-and-rpc-nodes)|
||[Domain Steem with JavaScript: Lesson #2 - Exploring the Steem API: Steem JS](https://steemit.com/@alejos7ven/domain-steem-with-javascript-lesson-2-exploring-the-steem-api-steem-js)|
||[Domain Steem with JavaScript: Lesson #3 - Hearing the blockchain](https://steemit.com/@alejos7ven/domain-steem-with-javascript-lesson-3-hearing-the-blockchain)|

I hope you are enjoying it,

Let's rock with Steem!`,
        parent_author:"",
        parent_permlink:tags[0],
        permlink: "steemjs-course-" + new Date().getDate() +"-id-" + Math.random().toFixed(4).substring(2),
        json_metadata:JSON.stringify({ tags:tags, app:'steemjscourse/1.0' })
    }

    let wif = readlineSync.question("Are you sure do you want to create this post? Enter your posting key ");

    steem.broadcast.comment(wif, 
                            post.parent_author, 
                            post.parent_permlink, 
                            post.author, 
                            post.permlink, 
                            post.title, 
                            post.body, 
                            post.json_metadata, function(err, result) {
        console.log(err, result);
    });
})()

Created an arrangement with all the necessary variables, create a unique permlink using specific words of the course, the date, and random numbers, in the body add the text of a simple post and in the metadata indicate that it was created with JS for the course.

Captura de pantalla 2025-01-05 a la(s) 2.56.14 p. m..png

After entering the posting key the post was created:

Captura de pantalla 2025-01-05 a la(s) 2.57.04 p. m..png

ThumbAuthorPost
@eight888Hello world! This is a post for the Steem JS Course

And finally, if we see the transaction we will see that it was created by our code, and not by Steemit.

  • 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') [5 PTS]
  • Use SteemWorld, Ecosynthesizer, or any other explorer to show the transactions you have created, leave link just like me. [2 PTS]
  • Transfer 0.001 STEEM to @eight888 using Steem JS, add in the memo the title of this lesson. [3 PTS]
PLEASE NOTE: Be careful while making your post, avoid showing your keys in the screenshots, always cut the parts where your keys could get exposed.

Rules


  • The content must be #steemexclusive.
  • The article must contain the tag #steemjs-s22w4.
  • 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 January 6. 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.
H2
H3
H4
3 columns
2 columns
1 column
25 Comments