Domain Steem with JavaScript: Lesson #1 - Introduction to Steem Blockchain and RPC Nodes

shabbir86 -

Greeting steemians,

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

            editing by canva

What is your understanding about RPC Nodes?

My understanding RPC nodes for the blockchain involve a set of units that facilitate the communication process between the users and the applications running in the blockchain as well as in the blockchain network. They serve as middlemen through which developers and customers can easily engage in use without the necessity to keep the full copy of a blockchain. This not only makes it easy and secure to access and perform operations such as queries or transactions on the block chain.

Functionality of RPC Nodes

An RPC node offers the API through which requests are made to the blockchain network. Such requests are normally causing functions, obtaining information or announcing transactions. RPC enables a client to call concrete procedures of a blockchain from a distance as if the procedures existed in the same environment.

For example:

Querying Data: Clients can, for example, obtain information about brep, eth_getBlockByNumber.

Smart Contract Interaction: The functions that enable developers to call functions inside a smart contract without creating a transaction are called eth_call.

Transaction Handling: Transactions are signed offline and sent into the network using the eth_sendRawTransaction client command.

How RPC Nodes Work

An RPC node is used where when it receives a request, processes it and transmit it to the blockchain network and send back the right response. This enables applications to transact with the blockchain without hosting their own nodes for the blockchain.

Types of RPC Nodes

  1. Public RPC Nodes: These are available from third-party providers like Infura, Alchemy, or QuickNode to name but a few. They give affordable and for the most part solid admittance to blockchains such as Ethereum, Binance Smart Chain and more.
  1. Self-Hosted Nodes: developers/organizations can startup own full node – this generally provides the highest level of control over the blockchain data and no necessity to rely on third parties.

Advantages of RPC Nodes

Accessibility: A glimpse on how blockchain features can be used by users and developers without having to run a full node.

Scalability: The public RPC providers operate infrastructure and can support high-traffic cases.

Cost Efficiency: Hacker allow developers to free them from the need to host own nodes in order to save resources.

Challenges

Some public RPC nodes can add rates and may not give full privacy to users. Also, the use of third parties enhances bring in challenges of centralization in decentralized environments.

They are by far the most crucial component of the blockchain-client architecture since they provide means for the actual execution of transactions and data retrieval. Whether internally developed or accessed from third parties, they provide a convenient method to interface with the blockchain; and enable developers to create and run dApps efficiently.


Explore the Steem Developers Portal (https://developers.steem.io) and fetch data using JavaScript or Python from at least 3 methods of condenser_api plugin (Don't use get_block method)

Thus, in order to interact with the Steem blockchain, the condenser_api plugin contains methods to get the data. Below, we will explore three key methods: get_accounts, get_discussions_by_created and get_dynamic_global_properties. :Those methods will be introduced and used with JavaScript with the help of dsteem API and Python with beem API.


  1. Using JavaScript

Setup: There is a dsteem library you need to install using the command:

npm install dsteem

Code:

require({ const Client = require(‘dsteem’); });
const client = new Client('https://api.steemit.com');

// Fetch account details
function getAccounts() {
say
async function getAccounts() {
const accounts = await client.call(‘condenser_api’, ‘get_accounts’, [['shabbir86’]]);
console.log("Account Details:", accounts);
}

There exists another way to fetch discussions: by their creation time.
async function getDiscussionsByCreated () {
const discussions = await client.call('condenser_api', 'get_discussions_by_created', [{ limit: 5 }]);
console.log("Recent Discussions:", discussions);
}

// Fetch global properties
async function getGlobalProperties() {
let the props that change frequently = await client.call(‘condenser_api’, ‘get_dynamic_global_properties’, []);
console.log("Global Properties:", properties);
}

// Execute all functions
getAccounts();
getDiscussionsByCreated();
getGlobalProperties();

Explanation:

Function get_accounts will pull information on the account shabbir86.

Users get_discussions_by_created retrieves the last five discussions with the new discussions at the top.

Head block number, participation rate and other blockchain statistics are obtained from the function get_dynamic_global_properties.


  1. Using Python

Setup: To install the beem library, please type the following command lines:

pip install beem

Code:

from beem import Steem
The other is from beem.account which is mainly dealing with the account name.
from beem import helpers as beem_helpers from beem.discussions import Query, Discussions

steem = Steem(nodes=["https://api.steemit.com"])

Fetch account details

def get_accounts():
account = Account(“shabbir86” , steem_instance=steem)
print("Account Details:", account)

Get discussions by creation

def get_discussions_by_created():
query = Query(limit=5)
Discussions = Discussions(by="created”, query=query,steem_instance= steem)
for discussion in discussions:
print("Discussion:", discussion)

Fetch global properties

def get_global_properties():
properties = steem.get_dynamic_global_properties()
print("Global Properties:", properties)

Execute all functions

get_accounts()
get_discussions_by_created()
get_global_properties()

Explanation:

get_accounts retrieves accounts of shabbir86 using Account.

get_discussions_by_created gets the latest 5 discussions using a Query.

get_dynamic_global_properties gives the global blockchain properties.


Output

In both implementations, you will see the following:

  1. Account Details: Information regarding the username that entails.
  1. Recent Discussions: A reverse chronological list of posts.
  1. Global Properties: Blockchain metrics such as head block number, and inflation rate.

These examples show, how data can be efficiently pulled from Steem with the help of dsteem and beem. It is versatile so that you can modify the queries in any form that suits your needs. Please don’t hesitate to drop me a message, if you need further help.

Get the information of a random block between 1,000,000 and 1,500,000 and write a program that shows who created that block and the date it was created.


  1. Prerequisites

  2. Install Python: First make sure you have Python on your operating system.

  1. Install steem-python:

pip install steem


  1. Python Script

Here’s the full script to retrieve information about a random block between 1,000,000 and 1,500,000:

from steem import Steem
from random import randint
from datetime import datetime

First we need to initiate the Steem instance

steem = Steem(nodes=["https://api.steemit.com"])

choose a randomly block number with in the range of 1,000,000 to 1,500,000

block_number = random(integers(1000000),integers(1500000))
print(f"Fetching data for block number: {block_number}")

Fetch the block information

try:
block = steem.get_block(block_number)

# Extract relevant details
block_id = block["block_id"]
witness = block["witness"]
In this case, the time that the block was created was passed into the function and named timestamp = block[“timestamp”].

># Convert timestamp to a readable format
readable_date = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')

# Print block details
print("\n================== Block Information ======================")
print(f"Block Number   : {block_number}")
print(f"Block ID       : {block_id}")
print(f"Created By     : {witness}")
print(f"Date Created   : {readable_date}")
print("\(==================================================\)”)

except Exception as e:
print(f"Error fetching block data: {e}")


  1. Explanation

  2. Random Block Number:

Some random number between one thousand and one and five hundred thousand is generated by randint function of python.

  1. Steem API:

The Steem library connects to the Steem blockchain node (https://api.steemit.com).

  1. Retrieve Block Data:

The get_block function fetches the block's details, such as:

Block ID: A Running identifier for the block.

Witness: Creator of the block.

Timestamp: Time of creation.

  1. Readable Timestamp:

Thus, it is converted to a human readable date using datetime.strptime.


  1. Example Output

When running the script, you might see the following:

Fetching data for block number: 1234567

=== Block Information ===
Block Number : 1234567
Block ID : cd78fab10b75ae733906ccf1c273fd059e21bae
Created By : witness-name
Date Created : 2016-10-25 12:47:47
=========================


  1. How to Run the Script

  2. Store the code in a file for instance fetch_steem_block Save the code in a file, e.g., fetch_steem_block.py.

  1. Run the script:

python fetch_steem_block.py


i would like to invite @wilmer1988 @josepha @stef1 to take part in this contest

Regard For me @shabbir86