Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const logger = require('./steemlog');
  2. const CronJob = require('cron').CronJob;
  3. const steem = require('steem');
  4.  
  5. class UpvoteTasks {
  6.     constructor() {
  7.         this.difference_between_upvotes = 4 * 1000; // Steem Blockchain allows to upvote once per 3 seconds; vote within minimum difference of 4 seconds
  8.         this.upvote_tasks = []; // Array with upvoting tasks
  9.     }
  10.  
  11.     set_data(posting_key, account_name, connection) {
  12.         this.posting_key = posting_key;
  13.         this.account_name = account_name;
  14.         this.connection = connection;
  15.     }
  16.  
  17.     /**
  18.      *
  19.      * @param {number} timestamp_to_fire - timestamp to set up cron task (in order to upvote)
  20.      * @param {string} account_name - Steem Blockchain account name
  21.      * @param {string} permlink - Steem Blockchain article permlink
  22.      * @param {number} upvote_strength - 1-100% upvote strength
  23.      * @param {function()} - callback after setting up new task
  24.      */
  25.     set_task(timestamp_to_fire, account_name, permlink, upvote_strength, next) {
  26.         this.check_cron_tasks(timestamp_to_fire, (timestamp_to_fire) => {
  27.        
  28.             var cron_job = new CronJob(new Date(timestamp_to_fire), function(){
  29.                 this.upvote(account_name, permlink, upvote_strength);
  30.             }, null, true);
  31.             cron_job.account_name = account_name;
  32.             cron_job.permlink = permlink;
  33.    
  34.             this.upvote_tasks.push(
  35.                 cron_job
  36.             );
  37.    
  38.             this.upvote_tasks.sort((a, b) => {
  39.                 var timestamp_a = new Date(a.cronTime.source).getTime();
  40.                 var timestamp_b = new Date(b.cronTime.source).getTime();
  41.    
  42.                 return timestamp_a - timestamp_b;
  43.             });
  44.  
  45.             this.connection.query('UPDATE `articles` SET `upvote_when` = ? WHERE `account_name` = ? AND `permlink` = ?',
  46.             [
  47.                 new Date(timestamp_to_fire).toISOString().slice(0, 19).replace('T', ' '),
  48.                 account_name,
  49.                 permlink
  50.             ], (error, results, fields) => {
  51.                 if(error){
  52.                     logger.log(error);
  53.                 }
  54.             });
  55.            
  56.             if(next){
  57.                 next();
  58.             }
  59.         });
  60.     }
  61.  
  62.     /**
  63.      * Delete task from array cron_upvotes
  64.      * @param {string} account_name - Steem Blockchain account name
  65.      * @param {string} permlink  - Steem Blockchain article permlink
  66.      */
  67.     delete_task(account_name, permlink) {
  68.         this.upvote_tasks = this.upvote_tasks.filter((task) => {
  69.             if(task.account_name != account_name || task.permlink != permlink){
  70.                 return task;
  71.             }
  72.         });
  73.     }
  74.  
  75.     /**
  76.      * Function is recursively looking for the optimal time for Cron Upvote task to be done in relation to other tasks placed in the cron_upvotes array (minimum difference is 4 seconds)
  77.      * @param {number} timestamp_to_fire - timestamp to check if it is suitable (4 seconds diff from others)
  78.      * @param {function(number)} - callback with timestamp to set up new task
  79.      */
  80.     static check_cron_tasks(timestamp_to_fire, next) {
  81.  
  82.         if(this.upvote_tasks.length > 0){
  83.             var closest = this.get_closest_timestamp(timestamp_to_fire);
  84.    
  85.             var diff_absolute = Math.abs(closest - timestamp_to_fire);
  86.    
  87.             if(diff_absolute < difference_between_upvotes){
  88.                 this.check_cron_tasks(timestamp_to_fire + (difference_between_upvotes - diff_absolute), next);
  89.             }
  90.             else {
  91.                 next(timestamp_to_fire);
  92.             }
  93.    
  94.             return;
  95.         }
  96.    
  97.         next(timestamp_to_fire);
  98.         return;
  99.     }
  100.  
  101.     /**
  102.      * Function is looking for the closest Cron object's timestamp i cron_upvotes array
  103.      * @param {number} timestamp
  104.      * @returns {number} - closest timestamp
  105.      */
  106.     get_closest_timestamp (timestamp){
  107.  
  108.         var closest_cron = this.upvote_tasks.reduce((previous, current) => {
  109.    
  110.             var previous_timestamp = new Date(previous.cronTime.source).getTime();
  111.             var current_timestamp = new Date(current.cronTime.source).getTime();
  112.    
  113.             var diff_previous = Math.abs(previous_timestamp - timestamp);
  114.             var diff_current = Math.abs(current_timestamp - timestamp);
  115.        
  116.             if(diff_previous > diff_current){
  117.                 return current;
  118.             }
  119.             else {
  120.                 return previous;
  121.             }
  122.         });
  123.    
  124.         return new Date(closest_cron.cronTime.source).getTime();
  125.     }
  126.  
  127.     /**
  128.      *
  129.      * @param {string} account_name - Steem Blockchain account name
  130.      * @param {string} permlink  - Steem Blockchain article permlink
  131.      * @param {number} upvote_strength - 1-100% upvote strength
  132.      */
  133.     upvote(account_name, permlink, upvote_strength){
  134.    
  135.         steem.broadcast.vote(
  136.             this.posting_key, // Voting account posting key
  137.             this.account_name, // Voting account name
  138.             account_name, // account whose permlink to be voted
  139.             permlink, // permlink from account to be voted
  140.             upvote_strength * 100, // Steem Blockchain use 1-10000, fe. 40(%) upvote * 100 = 4000
  141.             function (error, result) {
  142.                
  143.                 if(error){
  144.                     logger.log(error);
  145.                 }
  146.                 else {
  147.                     logger.log('Upvote done for: ' + account + ' / ' + permlink);
  148.                 }
  149.                
  150.                 this.connection.query('UPDATE `articles` SET `done` = 1 `done_when` = ? WHERE `account_name` = ? AND `permlink` = ?',
  151.                     [
  152.                         new Date().toISOString().slice(0, 19).replace('T', ' '),
  153.                         account_name, permlink
  154.                     ],
  155.                     (error, results, fields) => {
  156.                         // Status edited
  157.                     }
  158.                 );
  159.    
  160.                 this.deleteTask(account_name, permlink);
  161.             }
  162.         );
  163.     };
  164. }
  165.  
  166. module.exports = new UpvoteTasks();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement