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

alejos7ven -

Created with canva

Today we are going to start this new course which will aim to teach you how to create Steem applications, connecting directly to the blockchain and interacting with it. For this course it will be necessary to have previous knowledge in JavaScript, as well as a computer and internet access.

For practicality, we are going to use Node JS to show the examples through console, however, the libraries that we are going to show are perfectly compatible with the web version of the language.


Understanding Steem Blockchain


Steem is a decentralized blockchain platform that powers social media and blogging platforms like Steemit. Every 3 seconds a block is added to the blockchain, and this block contains the transactions that are made such as transferring, creating a post, voting, etc. Basically anything we do here is a transaction that will be recorded immutably in a block.

Animated with After Effects.

The objective as programmers will be to connect to Steem to read the information of the blocks or write new operations on them, allowing us, for example, to create a bot to vote for all the posts that are created within a specific tag.


The RPC Nodes


To be able to connect to Steem we will require a service made available generally by the Witnesses of the platform, they are the RPC nodes. Of its acronyms Remote Procedure Call, they provide a way for developers to interact with the Steem blockchain remotely.

Functions: RPC nodes expose a set of functions (APIs) that allow developers to:

The most used RPC node is the one provided by Steemit Inc:

If we enter this URL we will only see a response in JSON format, this URL itself is not a website, it is an API that is designed to be used with POST queries with HTTP.

If you have experience in programming you will know that creating HTTP queries is a fairly common task, however, these queries must be prepared precisely, otherwise they will fail. For this, a complete manual is available on the web on how you should prepare your HTTP queries to connect to Steem. That site is the Steem developer portal:

On this site you will find tutorials in different languages, Glossary of terms, and many useful resources when developing in Steem, but the most important part is the one that says APPBASE API, there you will see the list of Plugins available in the Steem API.

Each plugin allows you to obtain specific information from some section and each RPC node can have more or less plugins enabled. For example, if we enter the Condenser Api plugin which is the largest and most complete, we will see the list of methods that that plugin allows us to use. For example, we are going to review the method get_block, which as its name suggests will allow us to obtain the information of a specific block.

When entering the method we will see useful information such as an example of an expected result, the returned information and the necessary parameters to request information with this method.

But what is really important will be the examples with cURL queries, which we can test directly in our console and from here migrate the query to practically any programming language.

For example, in the examples we see how they use the Steemit RPC, they connect to the condenser_api plugin, and call the get_block method to obtain information from the specific block. In this case we will see the information from block number 1.

Now let's move this to JavaScript, I will create a file called connecting.js and in it I will create a function that allows me to call any plugin and method of the Steem API using fetch which makes an HTTP Post call, this is very practical because with a method like this you can read almost any information of the blockchain without external libraries.

Result

As you can see, we already managed to do the same as in the cURL example, already with the information in JSON format you can play with it in your code, I will improve a little the previous example to show the miner that produced the block and the date.

Result

Code

const fetch = require("node-fetch");

function customApi (server, method, params) {
    return new Promise((resolve, reject) => {
        fetch(server, {
        method: "POST",
        headers: { 'Content-Type': 'application/json'},
        body: JSON.stringify({"jsonrpc":"2.0","method":method,"params":params,"id":1})
        }).then(response => { resolve(response.text());})
          .catch(error => { reject(error); });
    });
} 

(async () => {
    let num = 1;
    let block = await customApi("https://api.steemit.com", "condenser_api.get_block", [num]);
    block = JSON.parse(block).result;
    console.log("Block ", num, "Created at",block.timestamp, "By", block.witness);
    
})();

Homework



Rules