Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const logger = require('./steemlog');
- const CronJob = require('cron').CronJob;
- const steem = require('steem');
- class UpvoteTasks {
- constructor() {
- this.difference_between_upvotes = 4 * 1000; // Steem Blockchain allows to upvote once per 3 seconds; vote within minimum difference of 4 seconds
- this.upvote_tasks = []; // Array with upvoting tasks
- }
- set_data(posting_key, account_name, connection) {
- this.posting_key = posting_key;
- this.account_name = account_name;
- this.connection = connection;
- }
- /**
- *
- * @param {number} timestamp_to_fire - timestamp to set up cron task (in order to upvote)
- * @param {string} account_name - Steem Blockchain account name
- * @param {string} permlink - Steem Blockchain article permlink
- * @param {number} upvote_strength - 1-100% upvote strength
- * @param {function()} - callback after setting up new task
- */
- set_task(timestamp_to_fire, account_name, permlink, upvote_strength, next) {
- this.check_cron_tasks(timestamp_to_fire, (timestamp_to_fire) => {
- var cron_job = new CronJob(new Date(timestamp_to_fire), function(){
- this.upvote(account_name, permlink, upvote_strength);
- }, null, true);
- cron_job.account_name = account_name;
- cron_job.permlink = permlink;
- this.upvote_tasks.push(
- cron_job
- );
- this.upvote_tasks.sort((a, b) => {
- var timestamp_a = new Date(a.cronTime.source).getTime();
- var timestamp_b = new Date(b.cronTime.source).getTime();
- return timestamp_a - timestamp_b;
- });
- this.connection.query('UPDATE `articles` SET `upvote_when` = ? WHERE `account_name` = ? AND `permlink` = ?',
- [
- new Date(timestamp_to_fire).toISOString().slice(0, 19).replace('T', ' '),
- account_name,
- permlink
- ], (error, results, fields) => {
- if(error){
- logger.log(error);
- }
- });
- if(next){
- next();
- }
- });
- }
- /**
- * Delete task from array cron_upvotes
- * @param {string} account_name - Steem Blockchain account name
- * @param {string} permlink - Steem Blockchain article permlink
- */
- delete_task(account_name, permlink) {
- this.upvote_tasks = this.upvote_tasks.filter((task) => {
- if(task.account_name != account_name || task.permlink != permlink){
- return task;
- }
- });
- }
- /**
- * 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)
- * @param {number} timestamp_to_fire - timestamp to check if it is suitable (4 seconds diff from others)
- * @param {function(number)} - callback with timestamp to set up new task
- */
- static check_cron_tasks(timestamp_to_fire, next) {
- if(this.upvote_tasks.length > 0){
- var closest = this.get_closest_timestamp(timestamp_to_fire);
- var diff_absolute = Math.abs(closest - timestamp_to_fire);
- if(diff_absolute < difference_between_upvotes){
- this.check_cron_tasks(timestamp_to_fire + (difference_between_upvotes - diff_absolute), next);
- }
- else {
- next(timestamp_to_fire);
- }
- return;
- }
- next(timestamp_to_fire);
- return;
- }
- /**
- * Function is looking for the closest Cron object's timestamp i cron_upvotes array
- * @param {number} timestamp
- * @returns {number} - closest timestamp
- */
- get_closest_timestamp (timestamp){
- var closest_cron = this.upvote_tasks.reduce((previous, current) => {
- var previous_timestamp = new Date(previous.cronTime.source).getTime();
- var current_timestamp = new Date(current.cronTime.source).getTime();
- var diff_previous = Math.abs(previous_timestamp - timestamp);
- var diff_current = Math.abs(current_timestamp - timestamp);
- if(diff_previous > diff_current){
- return current;
- }
- else {
- return previous;
- }
- });
- return new Date(closest_cron.cronTime.source).getTime();
- }
- /**
- *
- * @param {string} account_name - Steem Blockchain account name
- * @param {string} permlink - Steem Blockchain article permlink
- * @param {number} upvote_strength - 1-100% upvote strength
- */
- upvote(account_name, permlink, upvote_strength){
- steem.broadcast.vote(
- this.posting_key, // Voting account posting key
- this.account_name, // Voting account name
- account_name, // account whose permlink to be voted
- permlink, // permlink from account to be voted
- upvote_strength * 100, // Steem Blockchain use 1-10000, fe. 40(%) upvote * 100 = 4000
- function (error, result) {
- if(error){
- logger.log(error);
- }
- else {
- logger.log('Upvote done for: ' + account + ' / ' + permlink);
- }
- this.connection.query('UPDATE `articles` SET `done` = 1 `done_when` = ? WHERE `account_name` = ? AND `permlink` = ?',
- [
- new Date().toISOString().slice(0, 19).replace('T', ' '),
- account_name, permlink
- ],
- (error, results, fields) => {
- // Status edited
- }
- );
- this.deleteTask(account_name, permlink);
- }
- );
- };
- }
- module.exports = new UpvoteTasks();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement