An important part of HIVE blockchain is HivePower. It is the influence users has and which gives them different voting power. Your stake will determine your vote power. There are many users that do power up and then delegate to their curation account. This tutorial will explain how to power up liquid Hive and delegate to another account or undelegated.
I believe this tool will be useful for people that want to know more on the backend of HIVE blockchain. I will be interacting with the HiveJS API as usual in this tutorial.
<ul>
<li>Level of Difficulty : Intermediate
<li>IDE used : Sublime Text
<li>Languages : JavaScript, HTML, CSS
<li><br />
I will guide you through the tutorial and explain every part in a way that a newbie will understand and want to try it out. We will be interacting with the <a href="https://gitlab.syncad.com/hive/hive-js/tree/master/doc" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">HIVE chain JavaScript library to delegate HIVE Power and undelegated Hive power.<span>HIVE JavaScript API docs : <a href="https://gitlab.syncad.com/hive/hive-js/tree/master/doc" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://gitlab.syncad.com/hive/hive-js/tree/master/doc
<h2>Step 1 : HTML structure
<p dir="auto">The first step is to create the foundation of what we will build on. We will create the HTML structure of our HIVE delegation tool. We need 4 input fields which will accept inputs for the delegator’s name, delegatee’s username, private active key, and the hive power amount. Then there will be a button to delegate when all necessary details have been put in. The first thing is to set your HTML specifications in the opened sublime text by typing in the below code
<pre><code><!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
<p dir="auto">Then you can put the below code inside the body tag.
<pre><code> <div class="mypage">
<h1><center>HivePower Delegator</center></h1>
<input id='delegatee' type='text' placeholder="Delegatee Username"></input>
<input id='delegator' type='text' placeholder="Delegator Username"></input>
<input id='wif' type='password' placeholder="Private Active Key"></input>
<input id='hivepower' type='number' placeholder="Hive Power"></input>
<center><input type="button" value="DELEGATE" id='button'/></button></center>
</div>
<p dir="auto">The code above shows that I used inputs and used the type attributes to distinguish from the text and password field. The password type attribute is needed in order to turn whatever details there into asterisks. All the inputs were assigned id’s which will be used in the CSS part and also in the JavaScript part. The inputs were also given placeholders to let users see information about each field before inputting any details. The button input type was also added which will serve as the button to click which will in-turn trigger the delegation. The button input holds the value DELEGATE.<br />
All the inputs were wrapped in a div called mypage.<br />
<img src="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244040/zvnotqijyy1ejgpkdghn.png" alt="zvnotqijyy1ejgpkdghn" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244040/zvnotqijyy1ejgpkdghn.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244040/zvnotqijyy1ejgpkdghn.png 2x" /><br />
The above image shows the way our HTML page displays in the browser. It shows we need to style it further. This will be done in the CSS section to make it more appealing to the end-user.
<h2>Step 2 : CSS styling
<p dir="auto">Our CSS code will also be in the HTML file. We only need to specify the style type in the head section of our code by using <code><style type="text/css"></style>. This will ensure that any code that goes in there have effect in our HTML code.<br />
There is not much difference in the CSS code compared to the HIVE voting tool in my <a href="https://hive.blog/hive-196387/@zoneboy/-hive-javascript-api-tutorial-post-upvote-tutorial-1611613623">last tutorial .<br />
I used the same selectors which is the input types to apply similar settings to the HTML structure. Please refer to the <a href="https://hive.blog/hive-196387/@zoneboy/-hive-javascript-api-tutorial-post-upvote-tutorial-1611613623">last tutorial CSS section here to avoid duplicate tutorials.<br />
The only difference is the
<pre><code>#delegatee:hover, #delegator:hover, #wif:hover, #hivepower:hover {
border: 2px solid #9D4242;
}
<p dir="auto"><br />
That mean we are done with the CSS and ready to move to JavaScript part.<br />
<img src="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244113/nrxubajsfv5zsnrmkc1b.png" alt="nrxubajsfv5zsnrmkc1b" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244113/nrxubajsfv5zsnrmkc1b.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244113/nrxubajsfv5zsnrmkc1b.png 2x" /><br />
You can see how it looks like now in the browser.<span>The input type id’s are delegate, delegator, wif, and hivepower. The code above only shows what to do when the end-user hovers on them. It translates to a solid border of 2px with color <a href="/trending/9d4242"> #9D4242 should display when any of the input is hovered on.
<h2>Step 3 : JavaScript
<p dir="auto">We have now designed the delegation tool and it can’t function because we haven’t added any functionality to it. This is where we will implement the HIVE JavaScript Library to interact with the HIVE blockchain.
<p dir="auto">We will set our JavaScript in the head section before our CSS script and just after the last html tag in our body tag using <code><script></script> in both. This is where our JavaScript code will go into.<br />
We will not be installing any npm package on the system and that’s why we will use a Content Delivery Network (CDN) to connect to the HIVE JavaScript Library by typing in <code><script src="https://cdn.jsdelivr.net/npm/@hiveio/hive-js/dist/hive.min.js"></script> just above the CSS properties. The above means we have connected to the HIVE JavaScript minified library. Then we need to set some parameters before using the JavaScript APIs available to us.
<pre><code><script>
hive.api.setOptions({ url: 'https://anyx.io' });
hive.config.set('address_prefix','STM');
hive.config.set('chain_id','beeab0de00000000000000000000000000000000000000000000000000000000');
hive.config.set('alternative_api_endpoints', ['https://api.hive.blog', 'https://anyx.io']);
</script>
<p dir="auto">The above code sets the default RPC servers (nodes) to connect to and also include a fallback option in-case one of the nodes fail to work or respond. It also sets the chain id and address prefix.
<p dir="auto">This mean we can now start working with the HIVE APIs.<br /><span>
Let’s test if the connection is working by looking up all the accounts on HIVE. We use the get account API code in <a href="https://gitlab.syncad.com/hive/hive-js/tree/master/doc#get-account-count" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://gitlab.syncad.com/hive/hive-js/tree/master/doc#get-account-count which is
<pre><code>hive.api.getAccountCount(function(err, result) {
console.log(err, result);
});
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244269/ydtp3cm62k5lemw4ddhp.png" alt="ydtp3cm62k5lemw4ddhp" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244269/ydtp3cm62k5lemw4ddhp.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244269/ydtp3cm62k5lemw4ddhp.png 2x" /><br />
If you connect to the internet and reload the browser. Then, open console by right clicking anywhere on the page and click inspect then console. You will see the total number of registered accounts on hive which is currently <strong>1433716 at the time of publishing this post. This shows our setup is ready.
<p dir="auto">Delete the getAccountCount code to get started with the delegation code.
<p dir="auto">The API code to delegate or undelegated is
<pre><code>hive.broadcast.delegateVestingShares(wif, delegator, delegatee, vesting_shares, function(err, result) {
console.log(err, result);
});
<ul>
<li>and wif stands for the private posting key
<li>delegator stands for the person that have the HivePower to delegate or undelegate
<li>delegate is the person receiving the delegation or undelegation
<li>vesting_shares is the HIVE POWER denominated in VESTS
<p dir="auto">These are the parameters we will link with our previously created page html and CSS styled page.<br />
A function will be created titled delegateHP and all our JavaScript code will be placed inside the function scope. This is the function that will run once the DELEGATE button is clicked.
<p dir="auto">The next thing is to access the DOM by
<pre><code>var wif = document.getElementById('wif').value;
var delegator = document.getElementById('delegator').value;
var delegatee = document.getElementById('delegatee').value;
var hp = parseInt(document.getElementById('hivepower').value);
<p dir="auto">The above code is used to assign whatever value that goes into the password, delegator, delegate, and hivepower field to be assigned to var wif, delegator, delegate, and hp respectively.
<p dir="auto">The parseInt parses a string and returns an integer. It basically converts the string to an integer. Normally, the number inputted in the hive power field will come out as a string and need to be converted to an integer by using parseInt.<br />
The next part is where the bulk of the work is. It requires converting VESTS to HIVE Power.<br />
According to <a href="https://developers.hive.io/tutorials-recipes/vest-to-steem.html" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Hive Developers here
<blockquote>
<p dir="auto">Hive’s has base unit is VESTS and usually user doesn’t know about this unit because everything is dynamically calculated and presented in HIVE form for convenience of the user. In this recipe we will talk about how converting is working behind the scenes on all Hive apps. Dynamic Global Properties are used in this recipe to fetch the current values of global blockchain properties.
<p dir="auto">The above shows we can convert VESTS to Hive using<br />
<code>vestHive = ( result.total_vesting_fund_hive x availableVESTS ) / result.total_vesting_shares<br />
The <code>availableVESTS is the variable assigned to our hivepower input value. result.total_vesting_shares and result.total_vesting_fund_hive can be gotten from the Dynamic Global Properties.<br />
This means we need to access the Dynamic Global Properties API since we need some information there. The Dynamic Global Properties API is
<pre><code>hive.api.getDynamicGlobalProperties(function(err, result) {
console.log(err, result);
});
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244374/vo2sqv6rfwiff2yyktum.png" alt="vo2sqv6rfwiff2yyktum" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244374/vo2sqv6rfwiff2yyktum.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244374/vo2sqv6rfwiff2yyktum.png 2x" /><br />
If you run it and view the result in the console, you will see things like content_reward_percent, current_aslot, current_hbd_supply, current_supply, current_witness, and many others. You will also see the total_vesting_fund_hive and total_vesting_shares which is required.<br />
Now, we need to work on our Dynamic Global Properties API code to make it functional in the delegation tool.
<pre><code> var delegatedHivePower = hp;
var allHive = Number(result.total_vesting_fund_hive.split(' ')[0]);
var allVests = Number(result.total_vesting_shares.split(' ')[0]);
//Vests to Hive Calculator
var hiveCalc = (allVests * delegatedHivePower) / allHive;
hiveCalc = hiveCalc.toFixed(6);
hiveCalc = hiveCalc.toString();
var hpfinal = hiveCalc;
<p dir="auto">We split the <code>result.total_vesting_fund_hive.split gotten from the dynamic global properties API to make it form arrays then selected the first data in the array by using [0]. Everything was then wrapped with the Number() function to converts the object argument to a number that represents the object's value. This is then assigned to variable allHive.
<p dir="auto">The same process was used for the result.total_vesting_shares and assigned to the allVests variable.
<p dir="auto">They will now be used for the conversion calculator which is <code>(allVests * delegatedHivePower) / allHive; This means whatever the hivepower value is multiplied by the allVests and the answer divided by allHive. The result is assigned to a new variable called hiveCalc.
<p dir="auto">The hiveCalc is then attached to the .toFixed(6) method toFixed() which converts the number into a string and round it off the specified decimals which is 6. The .toString() method is also applied before finally assigning it to a new variable called hpfinal. This mean we are done with our Vests to Hive calculation and ready to run the Delegation API.
<pre><code>hive.broadcast.delegateVestingShares(
wif,
delegator,
delegatee,
hpfinal + ' VESTS',
function(err, result) {
if(err)
alert('Delegation unsuccessful ' + err);
else
alert('The transaction is successfully carried out');
console.log(err, result);
});
<p dir="auto">The above code is a modified version of the Delegation API and it runs once the Delegate button is clicked. This means we need to apply an onclick event in our delegation button html code. It will now look like<br />
<code><center><input type="button" value="DELEGATE" id='button' onclick="delegateHP()" /></button>
<p dir="auto">We now have a working delegation too that will report the error in case of unsuccessful delegation and a successful message if it goes through.
<p dir="auto">Our code is now fully done and functional.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244516/gk5mgun8rmhyzcvs6bmt.png" alt="gk5mgun8rmhyzcvs6bmt" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244516/gk5mgun8rmhyzcvs6bmt.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244516/gk5mgun8rmhyzcvs6bmt.png 2x" /><br />
<img src="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244535/qqlasspgvhrafppegdp8.png" alt="qqlasspgvhrafppegdp8" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244535/qqlasspgvhrafppegdp8.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/drrz8xekm/image/upload/v1612244535/qqlasspgvhrafppegdp8.png 2x" /><br />
You can see my block explorer data of the code working. I undelegated 50 HivePower from bryheem.
<p dir="auto"><br /><span>The code can be viewed on <a href="https://github.com/zoneboy/delegate/blob/main/index.html" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://github.com/zoneboy/delegate/blob/main/index.html<span>
The code is also live on <a href="https://zoneboy.github.io/delegate/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://zoneboy.github.io/delegate/
<h3>Check out my previous tutorial
<h6><a href="https://hive.blog/hive-196387/@zoneboy/-hive-javascript-api-tutorial-post-upvote-tutorial-1611613623">HiveJs post upvote tutorial
I've always wondered how that was done. Thank you for sharing.
You are welcome. Good to see someone got the backend view behind the code.