-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrontrun.js
220 lines (180 loc) · 7.46 KB
/
frontrun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var axios = require('axios');
var FULL_CURATION_TIME = 30 * 60 * 1000;
var api_url = 'https://steembottracker.net';
var fs = require('fs');
var steem = require('steem');
var fs = require('fs');
var path = require('path');
//var jsonPath = path.join(__dirname, '..', 'frontrun', 'fconfig.json');
//var utilPath = path.join(__dirname, '..', 'frontrun', 'futils.js');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var utils = require('./utils');
var frontrun = config.frontrun;
var cPostInterval = frontrun.cPostInterval * 1000;
var VPlimit = frontrun.VPlimit * 100;
var numPosts = frontrun.numPosts;
var sVote = 0;
var fVote = 0;
var counter = 0;
var authors = [];
var permlinks = [];
var vp;
var account = null;
var min;
var posts;
start();
async function start() {
await getVP();
frontRun();
}
function getVP() {
steem.api.getAccounts([config.account], function (err, result) {
if (result) {
account = result[0];
vp = utils.getVotingPower(account);
}
});
}
function frontRun() {
axios
.get(api_url + '/posts')
.then(data => {
posts = data.data;
var num_loaded = 0;
posts.forEach(function (post) {
var permLink = post.permlink;
var author = post.author;
steem.api.getContent(author, permLink, function (err, result) {
if (!err && result && result.id > 0) {
post.created = new Date(result.created + 'Z');
post.payout = parseFloat(result.pending_payout_value);
post.title = result.title;
post.author = result.author;
post.permlink = result.permlink;
var pre_30_min_pct = Math.min(new Date() - new Date(result.created + 'Z'), FULL_CURATION_TIME) / FULL_CURATION_TIME;
post.curation_reward = (Math.sqrt((post.payout + 1) * 0.25) - Math.sqrt(post.payout * 0.25)) * Math.sqrt((post.payout + 1 + post.vote_value) * 0.25) * pre_30_min_pct;
}
num_loaded++;
if (num_loaded >= posts.length) {
posts.sort(function (a, b) { return parseFloat(b.curation_reward) - parseFloat(a.curation_reward) });
scurate(posts);
}
})
})
});
}
function scurate(posts) {
for (var i = 0; i < posts.length; i++) {
authors.push(posts[i].author);
permlinks.push(posts[i].permlink);
}
//Calculate the actual data size comparing with numofposts to be upvoted
min = Math.min(permlinks.length, numPosts);
var Table = require('cli-table2');
var table = new Table({
head: ["index", "author", "permlinks"],
wordWrap: true
});
for (var i = 0; i < permlinks.length; i++) {
table.push([i+1, authors[i], permlinks[i]]);
}
console.log(table.toString());
myLoop(j = 0);
sVote = 0;
fVote = 0;
}
//Keep looping until you satisfy Counter of successful votes = numposts
//If number of successful vote met, stop the loop
//If min=1, restart the loop with delay
async function myLoop(j = 0) {
await sleep(5000);
getVP();
console.log('============ Selected numbers of post: ' + min + ' | Total posts: ' + permlinks.length + '========');
if (min == 0) {
utils.log("No frontrun posts at the moment")
setTimeout(frontRun, 30000);
}
if (sVote < min) {
VoteFront(j);
console.log('j1 in mainloop '+ j)
//The condition to continue loop or not
if (++j < permlinks.length && vp >= VPlimit && sVote < min) {
myLoop(j);
console.log('j2 in myloop : ' + j);
}
}
}
function VoteFront(j) {
if (vp >= VPlimit && numPosts !== '') {
steem.broadcast.vote(config.posting_key, config.account, authors[j], permlinks[j], (frontrun.voteWeight * 100),
function (err, result) {
if (result && !err) {
sVote++;
utils.log("Frontrunned posts| Author: " + authors[j] + " | Permlink: " + permlinks[j] + " | Vote weight of: " + frontrun.voteWeight + '%');
utils.log('Num of failed vote: ' + fVote);
utils.log('Num of successful vote: ' + sVote);
} else if (err) {
fVote++;
utils.log(err.message + " POST INFO | Author: " + authors[j] + " | Permlink: " + permlinks[j] + ' | Voting Power: ' + utils.format(vp / 100) + '%');
utils.log('Num of failed vote: ' + fVote);
utils.log('Num of successful vote: ' + sVote);
}
//if successful votes match the number setted, refront run again
if (sVote === min) {
let x = async () => {
console.log('==========================================================================');
console.log(' Successfully upvoted all posts!!');
console.log('==========================================================================');
await sleep(10000);
authors.length = 0;
permlinks.length = 0;
start();
console.log('j3 in sucess upvote all: ' + j);
}
x();
//Reloop when max data size and if desired number of upvoted post not reached
} else if (sVote < min && (fVote + sVote === permlinks.length)) {
let x = async () => {
await sleep(5000);
console.log('==========================================================================');
console.log(' End of available posts, Refetching new posts to upvote');
console.log('==========================================================================');
authors.length = 0;
permlinks.length = 0;
start();
}
x();
}
})
} else if (vp < VPlimit) {
utils.log('You have upvoted to the maximum number of post set per day');
utils.log('Voting Power: ' + utils.format(vp / 100) + '%' + " is too low to vote!");
utils.log("Author: " + authors[j] + ' | ' + 'Permlinks: ' + permlinks[j]);
StartTimer();
}
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
function StartTimer() {
let x = async () => {
await getVP();
let nTimer = utils.mTimer(vp);
if (nTimer <= 0) {
utils.log('Time until recovery to configured VP: ' + VPlimit / 100 + ' % | ' + utils.toTimer(0) + ' | Voting Power: ' + vp)
} else {
utils.log('Time until recovery to configured VP: ' + VPlimit / 100 + ' % | ' + utils.toTimer(nTimer) + ' | Voting Power: ' + vp)
}
if (vp >= VPlimit) {
clearInterval(y);
utils.log('VP fully replenished to the configured amount of ' + VPlimit / 100 + ' %, Starting frontrun!');
sVote = 0;
authors.length = 0;
permlinks.length = 0;
frontRun();
}
};
var y = setInterval(x, 10000);
}