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

alejos7ven -

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.

Result

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:

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:

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:

total_vesting_fund_steem*1000000/total_vesting_shares
steem_per_mvests * amount_to_convert / 1000000

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.

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



Rules