This tutorial will show how to fetch comments made by a specific account (in this case @steemitblog) by demonstrating how to use the get_state api function call. We will also demonstrate the most commonly used fields from the response object as well as how to parse the body of each comment.
Intro
We are using the get_state function with dsteem that returns the current state of the network as well as additional content. Each content body is written in markdown and could be submitted to the blockchain by many different applications built on top of Steem. For that reason we are using the remarkable npm package to parse markdown in a readable format.
Steps
App setup Configuration of dsteem to use the proper connection and network.
Query Query the path which we want to extract from Steem blockchain state.
Formatting Formatting the JSON object to be viewed in a simple user interface.
1. App setup
Below we have dsteem pointing to the main network with the proper chainId, addressPrefix and connection server.
There is a public/app.js file which holds the Javascript segment of this tutorial. In the first few lines we define and configure library and packages.
constdsteem=require('dsteem');letopts={};//connect to production serveropts.addressPrefix='STM';opts.chainId='0000000000000000000000000000000000000000000000000000000000000000';//connect to server which is connected to the network/productionconstclient=newdsteem.Client('https://api.steemit.com');constRemarkable=require('remarkable');constmd=newRemarkable({html:true,linkify:true});
remarkable is assigned to the variable md with linkify and html options, allowing us to parse markdown links and html properly.
2. Query
Next, we have the main function which runs when the page is loaded.
// query string, fetching comments made by @steemitblog accountconstquery='/@steemitblog/comments';client.database.call('get_state',[query]).then(result=>{// work with state object});
query is the path from where want to extract Steem blockchain state. In our example we are querying comments from the @steemitblog account. The result will be the current state object with various information as well as the content property holding the content of the query.
The following is an example of the returned object:
Next we will format the above object properly to view in a simple user interface. From the above object, we are only interested in the content object which holds the data we require.
JS: Get Account Comments
By the end of this tutorial you should know how to retrieve account comments from the steem blockchain
Full, runnable src of Get Account Comments can be downloaded as part of the JS tutorials repository.
This tutorial will show how to fetch comments made by a specific account (in this case
@steemitblog
) by demonstrating how to use theget_state
api function call. We will also demonstrate the most commonly used fields from the response object as well as how to parse the body of each comment.Intro
We are using the
get_state
function withdsteem
that returns the current state of the network as well as additional content. Each content body is written in markdown and could be submitted to the blockchain by many different applications built on top of Steem. For that reason we are using theremarkable
npm package to parse markdown in a readable format.Steps
dsteem
to use the proper connection and network.1. App setup
Below we have
dsteem
pointing to the main network with the proper chainId, addressPrefix and connection server. There is apublic/app.js
file which holds the Javascript segment of this tutorial. In the first few lines we define and configure library and packages.remarkable
is assigned to the variablemd
with linkify and html options, allowing us to parse markdown links and html properly.2. Query
Next, we have the
main
function which runs when the page is loaded.query
is the path from where want to extract Steem blockchain state. In our example we are queryingcomments
from the@steemitblog
account. The result will be the current state object with various information as well as thecontent
property holding the content of the query.The following is an example of the returned object:
3. Formatting
Next we will format the above object properly to view in a simple user interface. From the above object, we are only interested in the
content
object which holds the data we require.We first check if
content
is not an empty object. We then iterate through each object incontent
and extract:parent_author
parent_permlink
@steemitblog
account is replying toWe format
created
date and time, parsebody
markdown and getnet_votes
on that comment. Each line is then pushed and displayed separately.To Run the tutorial
cd tutorials/09_get_account_comments
npm i
npm run dev-server
ornpm run start