Steem.Js for dummies #8: Delegating Steem Power
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 Powerdelegatee
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!
Posted on Utopian.io - Rewarding Open Source Contributors
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
Suggestions
Human Curation
Community-Driven Witness!
I am the first and only Steem Community-Driven 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