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

in #utopian-io6 years ago (edited)

steemsnippetsv1.2.2.png

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



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

I appreciate sharing these snippets, but doesn't the GPL license imply, that all projects using these snippets must be GPL licensed as well?
Why didn't you use a more permissive license such as the MIT license?

Uuh I'm pretty bad with licenses. I'll switch to MIT soon then :)

https://choosealicense.com/ gives a rough overview.

Oh excellent! I keep this close to me! You wouldn't have a function to calculate the SBD value of an upvote. For example for my Discord bot right now it gets the votes from the curation account I chose to track down. I would also like to display the value in SBD but everything I've found so far is a bit too long for my taste :-) ! It would be nice to create a small function that could be used a bit like the reputation formatting function.

I will come regularly to watch the evolution of all this I see that you have made our lives easier ;-) ! Thank a lot @howo!

Not yet, but I'll work on it :)

Thanks for the kind words !

I didn't understood, how do i use snippets? Maybe question is a bit stupid, but I'm new around here :') . Ty for answer.

No question is stupid :)

The idea is that each snippet is a ready to use function to be added to your project. So you simply copy paste them and use them :)

Of course if you're not a programmer they won't be much use to you though. This is very oriented towards programmers and not really for "normal" users.

Great information, really a good choice
You are a wonderful and successful person

Thanks :)

Thank you for your contribution.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

Thanks :)

Hey @howo I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Utopian Witness!

Participate on Discord. Lets GROW TOGETHER!

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 63135.01
ETH 2546.56
USDT 1.00
SBD 2.64