Steem.Js for dummies #8: Delegating Steem PowersteemCreated with Sketch.

in #utopian-io7 years ago

image.png

In my extension @steem-plus, I have used SteemConnect to proceed with delegations, for security purpose. Indeed, it can be scary to input a private Active Key in an extension.

Nonetheless, let's see how to use SteemJs to proceed with a SteemPower delegation.

Let's start with our usual SteemJs documentation check by following this link. So, let's search something matching the keyword delegate... Here you go!

steem.broadcast.delegateVestingShares(wif, delegator, delegatee, vesting_shares, function(err, result) {
  console.log(err, result);
});

As usual, let's break this into pieces:

  • wif , here we are talking abt money money $$, so as you can guess, it's the Private Active Wif that we need. You can find it under the Permission tab of the Wallet, after clicking on 'Show Private Key'. Be careful with that one, anyone knowing that key could easily empty your SBD/STEEM and Power Down your Steem Power..

  • delegator stands for the account that will delegate the Steem Power

  • delegatee well... guess?

  • vesting_shares , that's where it gets funny so I'll make a whole chapter about it ;p

Vesting Shares?



Yes, I know you want to delegate Steem Power and not these weird-named Vesting Shares, but well, we'll have to see how to go from one to the other.
Actually, don't you remember we've done that already for calculating a user's Steem Power?

 steem.api.getDynamicGlobalProperties( {
                }).then((result)=>
                {
                    const totalSteem = Number(result.total_vesting_fund_steem.split(' ')[0]);
                    const totalVests = Number(result.total_vesting_shares.split(' ')[0]);
                    const delegated_SP = $('input[name=amount]').val();
                    var delegated_vest = delegated_SP * totalVests / totalSteem;
                    delegated_vest=delegated_vest.toFixed(6);
                    delegated_vest=delegated_vest.toString()+' VESTS';
}

Let's see what I did:

  • Thanks to a call to getDynamicGlobalProperties, I get the total number of Steem and Vesting Shares in the blockchain.
  • I read the number of Steem Power I want to delegate from an input (change that to a constant if you want).
  • I translate the number of delegated Steem Power into Vesting Shares.
  • The function only accepts a number of Vesting Shares with 6 decimals, so I use toFixed for that.
  • Stringify the result and add the VESTS symbol after a whitespace.

Once that's done, we just need to broadcast the operation:

steem.broadcast.delegateVestingShares('XXXXXXX', 'steem-plus', 'stoodkev', delegated_vest, function(err, result) {
  console.log(err, result);
});

That works!
I forgot to take a screenshot and every SteemPower that is delegated and taken back takes 7 days to cooldown so I won't do it again, you ll have to trust my word ;)

To change the number of SP delegated, simply make a new delegateVestingShares call with the new total number of delegated SP (if you delegated 100 SP and want to delegate 50SP, you should make a delegation of 150SP).

To cancel a delegation, simply delegate 0SP.

Hope this helps!

@stoodkev



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

You are doing a good job buddy. I started Steem tutorial but could not continue due to lack of visibility.

But I will now continue with it. Steem is a very good platform for developers to show their skills with both front end and block chain.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

thank you for your advice.

Congratulations! Your post has been manually selected by @flauwy for the daily @utopian-1up curation trail. 15 Utopians are following my trail and have upvoted your post for $3.94 in total. You can follow the @utopian-1up trail on SteemAuto (tutorial).

Thank you for making quality contributions on Utopian.io!

Disclaimer: 1UP is in not affiliated or organized by the official Utopian.io project!

Hey @stoodkev I am @utopian-io. I have just upvoted you at 7% Power!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • I introduced a competition factor. My vote is based also on how competitive the category used is.

Human Curation

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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.20
TRX 0.13
JST 0.030
BTC 65248.25
ETH 3471.40
USDT 1.00
SBD 2.51