create account

Write a Steemit Web App: Part 3 - Current Voting Power by jfollas

View this thread on: hive.blogpeakd.comecency.com
· @jfollas · (edited)
$0.04
Write a Steemit Web App: Part 3 - Current Voting Power
_(Previous Post: [Part 2](/@jfollas/write-a-steemit-web-app-part-2-followers-and-following))_
# What is Voting Power?
Voting Power is a rate-limiting feature of Steem to help prevent abuse. Each vote cast reduces the user's voting power by 2% of their remaining pool. So, if someone (or a bot) goes wild and upvotes hundreds of posts, then each upvote is worth less than the previous one (eventually being worth nothing).

Voting power regenerates linearly over 5 days, or 20% per day. So, with HF19, most people have set a goal of only depleting 20% of their voting power per day so that it will regenerate to 100% within 24 hours. This means that they can vote 11 times (at 100% per vote - only worth noting for those with enough steem power to unlock the ability to choose how much each vote is worth):

#|Voting Power
----|----
(Start)|100.00%
Vote 1|98.0%
Vote 2|96.04%
Vote 3|94.12%
Vote 4|92.24%
Vote 5|90.40%
Vote 6|88.58%
Vote 7|86.81%
Vote 8|85.08%
Vote 9|83.37%
Vote 10|81.71%
**Vote 11**|**80.07%**

## Representing Voting Power in a Web App
When you fetch account information with Steem.js, such as with the `getAccounts()` function, you get a `voting_power` number in the account data object: `{ ..., voting_power: 9668, ... }`. However, if you keep refreshing, you will not see this number ever change on its own - even if it's been five days since the last vote. This value is only updated when a user casts a vote. 

_Note: A LOT of the Steem applications out there are guilty of just showing this value... so even if you are really at 100% voting power, the apps won't reflect that._

But, there is another value in the account data object that is also updated when a vote is cast: `{..., last_vote_time: "2017-07-03T01:12:15", ...}`. So, your web app must do the task of calculating the current voting power value given these two inputs.

The basic formula is to take the reported `voting_power` and add in how much should have been regenerated since the `last_vote_time`. The maximum result would be 100%.

## JavaScript Code
In the [first post](/@jfollas/write-a-steemit-web-app-part-1-hello-user) of this series, we used the following code in the naive way to represent the user's voting power:

```javascript
vm.$set(vm.userData, 'power', result[0].voting_power / 100)
```

<br/>To correct this, we must first figure out how many seconds have passed:

```javascript
let secondsago = (new Date - new Date(result[0].last_vote_time + "Z")) / 1000
```

<br/>Next, divide the `secondsago` by the number of seconds in 5 days (`432000`) in order to get a percentage of how much of the 5 days has elapsed. Multiply this by `10000` (100%) to calculate how much voting power has regenerated so far, and add to the `voting_power` value from the account object:

```javascript
let vpow = result[0].voting_power + (10000 * secondsago / 432000)
```

<br/>Now we can set the value in our viewmodel (taking care of formatting and ensuring that the max is 100%):

```javascript
vm.$set(vm.userData, 'power', Math.min(vpow / 100, 100).toFixed(2))
```

<br/>The whole thing together would look like:

```javascript
steem.api.getAccountsAsync([accountName])
  .then(function (result) {
    ...
    let secondsago = (new Date - new Date(result[0].last_vote_time + "Z")) / 1000
    let vpow = result[0].voting_power + (10000 * secondsago / 432000)
    vm.$set(vm.userData, 'power', Math.min(vpow / 100, 100).toFixed(2))  
    ...
  })
```

<br/>Improvements to this would be to periodically recalculate the value using a timer so that the user always sees their latest voting power (especially if they are waiting for it to tick over to 100%).

![javascriptlogo.png](https://steemitimages.com/DQmZBbDGvKqM8V46NHaXoT7nZvrXhHLtTysrhixRu8eS87g/javascriptlogo.png)

_(Next Post: [Part 4](/@jfollas/write-a-steemit-web-app-part-4-calculating-steem-power))_
👍  , , , , , , ,
properties (23)
authorjfollas
permlinkwrite-a-steemit-web-app-part-3-current-voting-power
categorysteem-dev
json_metadata{"tags":["steem-dev","steem","developer","javascript","steemjs"],"image":["https://steemitimages.com/DQmZBbDGvKqM8V46NHaXoT7nZvrXhHLtTysrhixRu8eS87g/javascriptlogo.png"],"links":["/@jfollas/write-a-steemit-web-app-part-2-followers-and-following","/@jfollas/write-a-steemit-web-app-part-1-hello-user","/@jfollas/write-a-steemit-web-app-part-4-calculating-steem-power"],"app":"steemit/0.1","format":"markdown"}
created2017-07-03 03:37:09
last_update2017-07-08 21:21:18
depth0
children5
last_payout2017-07-10 03:37:09
cashout_time1969-12-31 23:59:59
total_payout_value0.035 HBD
curator_payout_value0.001 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,852
author_reputation4,351,701,088,490
root_title"Write a Steemit Web App: Part 3 - Current Voting Power"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id7,094,279
net_rshares5,247,600,076
author_curate_reward""
vote details (8)
@matthewdavid ·
Thanks so much for posting these. I look forward to trying out the code.
properties (22)
authormatthewdavid
permlinkre-jfollas-write-a-steemit-web-app-part-3-current-voting-power-20170703t042141736z
categorysteem-dev
json_metadata{"tags":["steem-dev"],"app":"steemit/0.1"}
created2017-07-03 04:21:42
last_update2017-07-03 04:21:42
depth1
children0
last_payout2017-07-10 04:21:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length72
author_reputation2,155,255,892,877
root_title"Write a Steemit Web App: Part 3 - Current Voting Power"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,097,718
net_rshares0
@money-dreamer ·
To make the refresh add this function to the end where refreshAccountData function was called:

```
        refreshAccountData(vm.user)
        function timeout() {
          refreshAccountData(vm.user,true)
          setTimeout(function () {
            timeout();
          }, 1000);
        }
        timeout()
```

No need to refresh follow(ers/ing) each time:

```
        function refreshAccountData(accountName,refresh=false) {
           ...
              if( ! refresh ) {
                getFollowersList()
                getFollowingList()
                getIgnoredList()
           ...
              }
```
properties (22)
authormoney-dreamer
permlinkre-jfollas-write-a-steemit-web-app-part-3-current-voting-power-20171220t035221799z
categorysteem-dev
json_metadata{"tags":["steem-dev"],"app":"steemit/0.1"}
created2017-12-20 03:52:21
last_update2017-12-20 03:52:21
depth1
children0
last_payout2017-12-27 03:52:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length619
author_reputation4,495,208,779,741
root_title"Write a Steemit Web App: Part 3 - Current Voting Power"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id24,288,913
net_rshares0
@musicfever ·
Sir you are a great inspiration for people out there.
👍  
properties (23)
authormusicfever
permlinkre-jfollas-write-a-steemit-web-app-part-3-current-voting-power-20170703t035456007z
categorysteem-dev
json_metadata{"tags":["steem-dev"],"app":"steemit/0.1"}
created2017-07-03 03:54:57
last_update2017-07-03 03:54:57
depth1
children0
last_payout2017-07-10 03:54:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length53
author_reputation1,937,741,063
root_title"Write a Steemit Web App: Part 3 - Current Voting Power"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,095,664
net_rshares371,424,445
author_curate_reward""
vote details (1)
@shaunmza ·
3 months later, I find your post. Helped me tremendously, wish I had found it sooner so I could have voted on it.

Thanks for putting this up here for people like me to find.
properties (22)
authorshaunmza
permlinkre-jfollas-write-a-steemit-web-app-part-3-current-voting-power-20171017t161033146z
categorysteem-dev
json_metadata{"tags":["steem-dev"],"app":"steemit/0.1"}
created2017-10-17 16:10:33
last_update2017-10-17 16:10:33
depth1
children0
last_payout2017-10-24 16:10:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length174
author_reputation17,139,522,306,343
root_title"Write a Steemit Web App: Part 3 - Current Voting Power"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id17,906,595
net_rshares0
@steemitboard ·
Congratulations @jfollas! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/posts.png)](http://steemitboard.com/@jfollas) Award for the number of posts published

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-jfollas-20170703t055141000z
categorysteem-dev
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-07-03 05:51:39
last_update2017-07-03 05:51:39
depth1
children0
last_payout2017-07-10 05:51:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length691
author_reputation38,975,615,169,260
root_title"Write a Steemit Web App: Part 3 - Current Voting Power"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,104,647
net_rshares0