SteemSnippets 1.2.4 : how to get steem power, delegated steem power, followers, following and check if a post exists

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@howo·
0.000 HBD
SteemSnippets 1.2.4 : how to get steem power, delegated steem power, followers, following and check if a post exists
<center> ![steemsnippetsv1.2.2.png](https://cdn.utopian.io/posts/5b29824991fdd8d0f23c412eb84df0084e94steemsnippetsv1.2.2(1).png)
</center>

Hello ! 

I've been working on a lot of things on the side, and I figured I would add some of my findings as snippets.

if you're wondering what this project is about you can find the long version here : https://steemit.com/programming/@howo/introducting-steemsnippets

In short :

Steemsnippets is a repository with lots of small snippets examples with a very specific functionnality in mind so that programmers who are beginners ( or not) with the steem ecosystem can go in there and be like "ok how I do that ?" and find a corresponding snippet with complete documentation associated with it.

## New snippets :

### Get steem power and delegated steem power

This one is a bit more tricky than what you would expect, because internally steem doesn't deal in steem power, it deals in vests, and the ratio vests/steem varies all the time. so you have to get the current ratio, and then calculate the actual values.

Another thing that I started doing is checking for rpc errors directly within the function. It's implemented by simply doing a recursive call if I detect that there has been an error with the rpc node. Note that if the node actually goes down for a long period of time, your application will go down as well fairly fast as well because the call stack will get filled. On my more mature projets I avoid doing recursive calls and just loop around the same function.  

the examples shows pretty well how the whole snippet works :

```
async function example()
{
    const data =  await get_steem_power("howo");
    console.log("howo has "+ data.steem_power + " steem power and has "+ data.delegated_steem_power + " delegated steem power, so his current steem power is " + (data.steem_power+data.delegated_steem_power))
}
```

related commit :

https://github.com/drov0/steemsnippets/commit/bd7f6698e6e063a047e106570e404d3b6fd8ccf8

### Get followers/following

This was quick snippet but when I looked at the steem-js functions I saw "getFollowCount" and nothing said that this function also showed the followers. So I figured this snippet was worth it. 

This function also implements a failsafe in case of rpc errors.

Once again the example easily shows how it works 

```
async function example()
{
    const data =  await get_followers_following("howo");
    console.log("howo has "+ data.followers + " followers and he's following "+ data.following + " users. ")
}
```

related commit : https://github.com/drov0/steemsnippets/commit/b0fb069c0dc0632c5d49f347dbb90be4d13aad5f


### Post exist 

Sometimes you want to know if a post exist before trying to do some more heavy functions on top of it (commenting/voting) as signing will be more heavy than just checking.

 This was important for me because we store the posts made with steempress on a local database for easier access. But then when we wanted to vote on some with the steempress-io account, we realized that sometimes the post was deleted. So I needed to check before voting if the post actually still exists. 

In the blockchain, a deleted/nonexistant post can be seen easily as the author field will be empty. 

The example :

```
// example
// if you get error, make sure that you have the latest node version.
async function main() {
    // this exists : https://steemit.com/utopian-io/@howo/steemsnippets-1-2-3-power-up-get-active-votes-and-get-lastest-posts-from-tags
    var exists = await post_exists("howo", "steemsnippets-1-2-3-power-up-get-active-votes-and-get-lastest-posts-from-tags");
    console.log(exists); // true
    // this post does not exists
    var not_existing = await post_exists("howo", "nonexistant");
    console.log(not_existing); // false
}
```



related commit :

https://github.com/drov0/steemsnippets/commit/7e57efd5594f69b5df16b3f5368269820044496d




The project is open source so feel free to submit pull requests with your own snippets :)

@howo


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@howo/steemsnippets-1-2-4-how-to-get-steem-power-delegated-steem-power-followers-following-and-check-if-a-post-exists">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,