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

in #steemjs-s22w215 hours ago

Exploring the Steem API Steem JS2.png

Edited by Canva

Hello steemians,

Here is my homework for SLC22 Week 2, with the corresponding tasks assigned by @alejos7ven!

Use Steem JS to execute at least 3 methods from Steem API

To run Node.js scripts like those using Steem JS with Sublime Text, I first install Node.js, download and install Node.js from the official site, then verify the installation by typing the commands node -v and npm -v in the terminal to confirm that the versions of Node.js and npm are correctly displayed, then I install Sublime Text by downloading it from its official website and opening it once installed complete.

Then I set up the project in Sublime Text, create a folder for the project, named steem-js-project, and open it in Sublime Text via the File > Open Foldermenu, then, to install Steem JS, I 'open a terminal and I go to the project folder using the command cd path/to/steem-js-project, I start the initialization of a Node.js project with npm init -y and I install Steem JS library with npm install steem --save

1. Method get_accounts

The get_accounts method is used to retrieve detailed information about one or more Steem accounts, in this example the kouba01 and steemcurator01 accounts are queried, the method retrieves data such as balances in STEEM and SBD, accounts raw reputation scores, acquisition shares used to calculate Steem Power, and additional details such as key permissions and number of posts, implementation involves calling the function getAccounts with an array of account names, and the API responds with the corresponding account data, this method is particularly useful for user profile displays, reputation calculations, and determining Steem Power.

4.PNG

The expected result is a JSON object containing account-specific data, such as:

get_accounts.gif

2. Method get_dynamic_global_properties

The get_dynamic_global_properties method retrieves global metadata about the current state of the Steem blockchain, this includes the number of the last generated block (head_block_number), the current blockchain time (time), the total STEEM assigned to the vesting fund (total_vesting_fund_steem) and the total vesting shares in the system (total_vesting_shares), the method provides a snapshot of the overall state of the blockchain, which is essential for monitoring the health of the blockchain, performing calculations related to Steem Power and creating tools that depend on real-time data, implementation involves calling the function getDynamicGlobalProperties, which returns a JSON object structured with these details.

3.PNG

The expected result is:

get_dynamic_global_properties.gif

3. get_discussions_by_created method

The get_discussions_by_created method retrieves the most recently created discussions on the Steem blockchain, it takes a parameter, such as limit, which specifies the number of messages to retrieve, the API responds with an array of discussions, each containing details such as author username, post title, creation timestamp, number of upvotes and associated tags, this method is commonly used to create feeds or dynamically display the latest posts of a specific category, the implementation is to call the getDiscussionsByCreated function with the desired parameters.

2.PNG

The expected result is:

get_discussions_by_created.gif

Calculate the effective SP (vesting_shares+received_vesting_shares-delegated_vesting_shares-vesting_withdraw_rate) for all Steemcurator accounts (sc01-08)

5.PNG

We began with the import of the Steem JS library and the configuration of the connection point to the Steem API by the use of require("steem") command which allows the library to be included in the project, then steem.api. setOptions({ url: 'https://api.steemit.com' }) configures the RPC node URL, which will be used to interact with the Steem blockchain, this establishes the connection with the blockchain to execute necessary queries.

6.PNG

The steemcurators table contains the names of the Steemcurator accounts, ranging from steemcurator01 to steemcurator08, these accounts represent the users targeted by the script to calculate their effective Steem Power, each account is included as a character string in the table , this list serves as input to the script that extracts relevant information for each account.

7.PNG

This section presents the extraction of data from each account and the calculation of the effective Steem Power, the steem.api.getAccounts function retrieves the details of the accounts specified in the steemcurators table, important data like vesting_shares, received_vesting_shares, delegated_vesting_shares, and vesting_withdraw_rate are extracted for each account and converted to numbers using parseFloat, The effective Steem Power is calculated using the formula:

Effective SP = vesting_shares + received_vesting_shares - delegated_vesting_shares - vesting_withdraw_rate

The result is then displayed in the console with the corresponding account name.

8.PNG

The results displayed in the console show the effective Steem Power calculated for each Steemcurator account, each row shows the account name followed by its effective Steem Power value, for example, account steemcurator01 has an effective Steem Power of 22609773717.538094 , while the steemcurator02 account has a value of 11138386421.945415, the results are accurate to six decimal places, which ensures detailed and reliable calculations, the script displays the information in a clear and readable format for each account.

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

The get_dynamic_global_properties method allows real-time metadata to be retrieved from the Steem blockchain, offering valuable information about its current state which is essential for various calculations and monitoring operations by providing a solid basis for understanding how the blockchain works, here is a detailed explanation of five important values that can be extracted from this method, along with their meaning and usefulness.

The following code shows how to retrieve and save these values ​​using Steem JS:

image.png

Output

image.png

1. head_block_number

The head_block_number value corresponds to the number of the most recently generated block on the Steem blockchain, this number is incremented every three seconds, because Steem produces a new block at this interval, this value is essential to track the current state of the blockchain and guarantee synchronization between the different nodes of the network, developers often rely on this data to verify that their applications interact with up-to-date information coming from the blockchain, thus ensuring the precision and reliability of operations carried out on the network .


2. time

The time field indicates the current blockchain time in UTC format, which allows operations such as votes, posts or transactions to be accurately timestamped, this value is of particular importance for factor-sensitive operations time, such as the validation of voting windows, the distribution of rewards or even the management of expirations of specific transactions, in addition, it helps applications to align with the blockchain calendar in order to maintain temporal consistency and prevent any desynchronization, which ensures smooth operation and meets network requirements.


3. total_vesting_fund_steem

The total_vesting_fund_steem value represents the total quantity of STEEM allocated to the vesting fund, essential for the calculation of the Steem Power (SP), which measures the influence on the blockchain, this value makes it possible to determine the conversion rate between the STEEM and vesting shares using the following formula:

SP = acquisition shares x total STEEM acquisition fund / total acquisition shares

This parameter is essential for developers who create tools to estimate users' Steem Power based on their vesting actions, offering an accurate representation of the influence and assets staked on the blockchain.


4. total_vesting_shares

The value total_vesting_shares reflects the total number of vesting shares on the blockchain, which correspond to staked STEEM used to generate Steem Power, associated with total_vesting_fund_steem, this value determines the conversion rate of vesting shares in SP, it plays an essential role in calculating the SP of a user and in understanding the total power staked on the Steem network, highlighting the distribution of the influence amongst its participants.


5. virtual_supply

The virtual_supply value represents the estimated total STEEM supply, including the value of all Steem-backed dollars (SBD) converted to STEEM at the current exchange rate, this data reflects the theoretical maximum STEEM supply while taking into account counts SBDs in circulation, it is particularly useful for understanding blockchain economic dynamics, such as the impact of inflation or conversions between STEEM and SBD, providing valuable information on global monetary policies of the system, this value plays a crucial role for developers, analysts and users who wish to evaluate the blockchain economics and anticipate its evolution.


Thank you very much for reading, it's time to invite my friends @pelon53, @crismenia, @bossj23 to participate in this contest.

Best Regards,
@kouba01