Domain Steem with JavaScript: Lesson #2 - Exploring the Steem API: Steem JS

in #devwithseven2 days ago

2.jpg

Created with canva

We have learned how to connect with Steem through HTTP queries using RPC nodes as an access door, however, when it comes to programming there are methods that facilitate work and they are the libraries that have been created specifically to work on the blockchain. Steem JS and DSteem are known so far for JavaScript, but this time we will first get to know Steem JS.

To learn to walk it is necessary to crawl first, for that reason I taught you how to connect with Steem without help, however, things can be much easier, and this is thanks to the Steem JS library that will allow us to perform operations more intuitively. You can see the library here:

To use it, as I am using node you must install it with the following command:

npm install steem --save

After this I will be able to import it into my project and start using it. Let's connect the API with the library, and let's try to do the same as in the previous example.

Captura de pantalla 2024-12-15 a la(s) 3.55.51 p. m..png

Result

Captura de pantalla 2024-12-15 a la(s) 3.56.09 p. m..png

Code

const steem = require("steem");
steem.api.setOptions({ url: 'https://api.steemit.com' });
let num = 1;
steem.api.getBlock(num, function(err, block) {
    console.log(err, block);
    console.log("Block ", num, "Created at",block.timestamp, "By", block.witness);
});

As with the developer portal, the Steem JS library is loaded with the most important Steem methods, you can consult the list here:

If any method is not available you can run a normal HTTP query to get what you want to do.

Getting information and using it


Steem applications show us the information of our accounts and other elements already transformed into the values we already know, however, when we obtain the data directly from the blockchain some of them may be in a rawer version that we have to work on later.

For example, when we use the get_accounts method, we get the reputation of the desired users in a value transformed to millions, where 1,000,000,000 is the base reputation of 25. The Steem JS library has a function included that allows us to make the conversion without worrying too much, let's see how to do it:

Captura de pantalla 2024-12-22 a la(s) 3.20.37 p. m..png

In this example we get the information of 2 accounts, mine, and that of steemcurator01. Then we use the function formatter.reputation() and pass the raw value to make the conversion. Let's see the result:

Captura de pantalla 2024-12-22 a la(s) 3.20.55 p. m..png

Another clear example are the values related to the Steem Power that are represented in Vesting Shares, in this case we will transform these vests to SP manually.

To transform VESTS to SP we have to take into account the following:

  • We need to get the Steem per Million Vestings. This is achieved with the following formula:
total_vesting_fund_steem*1000000/total_vesting_shares
  • To convert to SP we will use this formula:
steem_per_mvests * amount_to_convert / 1000000

Captura de pantalla 2024-12-22 a la(s) 3.40.25 p. m..png

To get the mvests we will need to obtain the global information of the blockchain with the get_dynamic_global_properties method, there will be everything we will need to calculate it and then use it in our conversion to SP.

Once we have it, we got the information from my account and steemcurator01, and again we made the conversion for both. In the results you can see the value already converted, and the value in vesting_shares.

Captura de pantalla 2024-12-22 a la(s) 3.40.43 p. m..png

Now, this value is not the effective SP, to this we have to discount the delegated SP, add the SP received and subtract the amount of SP necessary if the user is in power down, but I will leave that as a task.


Homework


  • Use Steem JS to execute at least 3 methods from Steem API [2 PTS]
  • Calculate the effective SP (vesting_shares+received_vesting_shares-delegated_vesting_shares-vesting_withdraw_rate) for all Steemcurator accounts (sc01-08) [5 PTS]
  • Study the get_dynamic_global_properties method and explain at least 5 values we can get here. Use this site: https://developers.steem.io/tutorials-recipes/understanding-dynamic-global-properties [3 PTS]

Rules


  • The content must be #steemexclusive.
  • The article must contain the tag #steemjs-s22w2.
  • 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 23. 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 participated in this contest after being invited on your post.
please check my post.
Lesson 1:Mastering Steem.js for Blockchain Development