Making Voting Weight and Threshold Configurable

in #utopian-io6 years ago (edited)

Changes

Bug Fixes

Fix for Revoting Revotes were occurring. Posts that had already been voted on, would be voted on again. If the voting weight changed, it would vote and increase the weight (feature?). It was undesirable outcome. This fixes it so revotes aren't occurring and we're able to specify weight at startup.

New Features

Configurable Voting Weight Now voting weight can be configured at runtime using:

docker run --rm -e STEEM_NAME=$STEEM_NAME -e STEEM_WIF=$STEEM_WIF -e DEFAULT_UPVOTE_WEIGHT=500 r351574nc3/upvote-queue-steem-bot:latest

Vote Rescheduling Voting is rescheduled now instead of sleeping.

Details

diff --git a/upvote-queue-steem-bot/app/config/index.js b/upvote-queue-steem-bot/app/config/index.js

index 723e9b9..d103e31 100644
--- a/upvote-queue-steem-bot/app/config/index.js
+++ b/upvote-queue-steem-bot/app/config/index.js
@@ -1,6 +1,8 @@
 config = {
     user: process.env.STEEM_NAME || "YOU NEED TO FILL THIS IN ICEHOLE",
     wif: process.env.STEEM_WIF || "YOU NEED TO FILL THIS IN ICEHOLE",
+    weight: process.env.DEFAULT_UPVOTE_WEIGHT || 500,
+    threshold: process.env.VOTING_THRESHOLD || 9250,
     steemit_url: "https://www.steemit.com"
 }

The above is part of the feature to make weight and threshold more configurable at runtime. These are defaults added to configuration.

diff --git a/upvote-queue-steem-bot/app/helpers/bot/index.js b/upvote-queue-steem-bot/app/helpers/bot/index.js

index 9061ce8..b014f55 100644
--- a/upvote-queue-steem-bot/app/helpers/bot/index.js
+++ b/upvote-queue-steem-bot/app/helpers/bot/index.js
@@ -1,7 +1,7 @@
 'use strict'
 
 const scheduler = require('node-schedule')
-const HOURLY = '0 1 * * * *'
+const EVERY_20_MINUTES = '*/20 * * * *'
 
 
 module.exports = {
@@ -9,6 +9,6 @@ module.exports = {
 }
 
 function run() {
-    scheduler.scheduleJob(HOURLY, require('./upvote'))
+    scheduler.scheduleJob(EVERY_20_MINUTES, require('./upvote'))
     // require('./upvote.js').execute()
 }

The above change is for adjusting from hourly execution to every 20 minutes. This is not yet configurable. Probably should be.

diff --git a/upvote-queue-steem-bot/app/helpers/bot/upvote.js b/upvote-queue-steem-bot/app/helpers/bot/upvote.js

index 32d2934..21f61ac 100644
--- a/upvote-queue-steem-bot/app/helpers/bot/upvote.js
+++ b/upvote-queue-steem-bot/app/helpers/bot/upvote.js
@@ -2,9 +2,9 @@
 
 const Promise = require('bluebird')
 const steem = Promise.promisifyAll(require('steem'))
-const {user, wif} = require('../../config')
-const sleep = require('sleep')
+const {user, wif, weight, threshold} = require('../../config')
 const moment = require('moment')
+const schedule = require('node-schedule')

The above removes the sleep dependency and replaces with node-schedule so that votes are not slept, but rescheduled. This is a rescheduling feature that is more efficient than sleeping.
It also adds weight and threshold configuration settings.

 module.exports = {
@@ -12,7 +12,6 @@ module.exports = {
 }
 
 const FLAG = 'https://steemitimages.com/0x0/https://memegenerator.net/img/instances/500x/71701676/my-ultimate-is-still-charging.jpg'
-const TWO_PERCENT = 200
 
 const SECONDS_PER_HOUR = 3600
 const PERCENT_PER_DAY = 20
@@ -34,23 +33,29 @@ function current_voting_power(vp_last, last_vote) {
 }
 
 function time_needed_to_recover(voting_power) {
-    return RECOVERY_RATE * (9250 - voting_power)
+    return RECOVERY_RATE * (threshold - voting_power)
 }

The above makes recovery rate calculations more legible and removes hard-coded voting weight since it's now configurable.

 function upvote(post) {
+    var recovery_wait = 0
     return steem.api.getAccountsAsync([ user ]).then((account) => {
         var voting_power = current_voting_power(account.voting_power, account.last_vote_time)
-        var recovery_wait = time_needed_to_recover(voting_power)
-        while (recovery_wait > 0) {
-            console.log("Waiting ", recovery_wait, " seconds to recover")
-            sleep.sleep(recovery_wait)
-            voting_power = current_voting_power(account.voting_power, account.last_vote_time)
-            recovery_wait = time_needed_to_recover(voting_power)
-        }
+        recovery_wait = time_needed_to_recover(voting_power) / 60
         return post
     })
     .then((post) => {
-        return steem.broadcast.voteAsync(wif, user, post.parent_author, post.parent_permlink, TWO_PERCENT)
+
+        // Reschedule vote
+        if (recovery_wait > 0) {
+            var later = moment().add(recovery_wait, 'minutes').toDate()
+            console.log("Rescheduling ", recovery_wait, " minutes to recover")
+            schedule.scheduleJob(later, function() {
+                upvote(post)
+            })
+            return post
+        }
+
+        return steem.broadcast.voteAsync(wif, user, post.parent_author, post.parent_permlink, weight)
         .then((results) =>  {
             console.log(results)
         })
@@ -60,7 +65,14 @@ function upvote(post) {
     })
 }

The above changes are for using the new weight configuration and replacing the old sleep functionality with scheduling instead.

+function not_already_voted_on(post) {
+    return steem.api.getActiveVotesAsync(post.parent_author, post.parent_permlink)
+    .filter((vote) => vote.voter == user)
+    .then((votes) => { return votes < 1 })
+}
+
 function execute() {
+    console.log("Upvotes running on schedule")
     return steem.api.getDiscussionsByCommentsAsync({start_author: user, limit: 100})
         .filter((post) => moment(post.created).diff(moment(), 'days') <= 7)
         .filter((post) => post.body.indexOf(FLAG) > -1)
@@ -71,5 +83,7 @@ function execute() {
                 parent_author: post.parent_author,
                 parent_permlink: post.parent_permlink
             }
-        }).each((post) => upvote(post))
+        })
+        .filter((post) => not_already_voted_on(post))
+        .each((post) => upvote(post))
 }

The above changes are part of a new feature to prevent re-voting on already voted posts. Checks and filters are added to verify the post has not already been voted on yet.

diff --git a/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-bot/templates/deployment.yaml b/upvote-queue-steem-bot/orchestration/charts/upvot

index 1529065..503cf41 100644
--- a/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-bot/templates/deployment.yaml
+++ b/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-bot/templates/deployment.yaml
@@ -24,6 +24,8 @@ spec:
               value: "{{ .Values.steem.name }}"
             - name: STEEM_WIF
               value: "{{ .Values.steem.wif }}"
+            -name: DEFAULT_UPVOTE_WEIGHT
+              value: {{ .Values.steem.default_upvote_weight }}
           ports:
             - containerPort: {{ .Values.service.internalPort }}
           resources:

The above changes are to add voting weight as an environment variable for deployment with kubernetes

diff --git a/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-bot/values.yaml b/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-

index f37239f..f3af4cb 100644
--- a/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-bot/values.yaml
+++ b/upvote-queue-steem-bot/orchestration/charts/upvote-queue-steem-bot/values.yaml
@@ -15,6 +15,7 @@ service:
 steem:
   name: please fill this in
   wif: please fill this in
+  default_upvote_weight: 500
 
 ingress:
   enabled: false

The above is adding a upvote weight as a default setting for kubernetes.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Really hope this one gets approved.

Not reviewed yet?

@r351574nc3, Approve is not my ability, but I can upvote you.

Thanks for helping. It's greatly appreciated!

Thank you for the contribution. It has been approved.

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

What do you think? Am I getting better at submitting these?

Yes you are getting better, just try to add few more features whenever you add any new contribution, because in near future we will be having the quality slider and based on that your contribution will be evaluated.

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

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

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 63793.25
ETH 3410.80
USDT 1.00
SBD 2.59