-
Notifications
You must be signed in to change notification settings - Fork 89
/
postpromoter.js
1303 lines (1058 loc) · 50.4 KB
/
postpromoter.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var fs = require("fs");
var request = require("request");
var steem = require('steem');
var dsteem = require('dsteem');
var utils = require('./utils');
var account = null;
var transactions = [];
var outstanding_bids = [];
var delegators = [];
var last_round = [];
var next_round = [];
var blacklist = [];
var whitelist = [];
var config = null;
var first_load = true;
var isVoting = false;
var last_withdrawal = null;
var use_delegators = false;
var round_end_timeout = -1;
var steem_price = 1; // This will get overridden with actual prices if a price_feed_url is specified in settings
var sbd_price = 1; // This will get overridden with actual prices if a price_feed_url is specified in settings
var version = '2.1.1';
var client = null;
var rpc_node = null;
const AUTHOR_PCT = 0.5;
startup();
function startup() {
// Load the settings from the config file
loadConfig();
// Connect to the specified RPC node
rpc_node = config.rpc_nodes ? config.rpc_nodes[0] : (config.rpc_node ? config.rpc_node : 'https://api.steemit.com');
client = new dsteem.Client(rpc_node);
utils.log("* START - Version: " + version + " *");
utils.log("Connected to: " + rpc_node);
if(config.backup_mode)
utils.log('*** RUNNING IN BACKUP MODE ***');
// Load Steem global variables
utils.updateSteemVariables(client);
// If the API is enabled, start the web server
if(config.api && config.api.enabled) {
var express = require('express');
var app = express();
var port = process.env.PORT || config.api.port
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/api/bids', (req, res) => res.json({ current_round: outstanding_bids, last_round: last_round }));
app.listen(port, () => utils.log('API running on port ' + port))
}
// Check if bot state has been saved to disk, in which case load it
if (fs.existsSync('state.json')) {
var state = JSON.parse(fs.readFileSync("state.json"));
if (state.transactions)
transactions = state.transactions;
if (state.outstanding_bids)
outstanding_bids = state.outstanding_bids;
if (state.last_round)
last_round = state.last_round;
if (state.next_round)
next_round = state.next_round;
if(state.last_withdrawal)
last_withdrawal = state.last_withdrawal;
// Removed this for now since api.steemit.com is not returning more than 30 days of account history!
//if(state.version != version)
// updateVersion(state.version, version);
utils.log('Restored saved bot state: ' + JSON.stringify({ last_trx_id: (transactions.length > 0 ? transactions[transactions.length - 1] : ''), bids: outstanding_bids.length, last_withdrawal: last_withdrawal }));
}
// Check whether or not auto-withdrawals are set to be paid to delegators.
use_delegators = config.auto_withdrawal && config.auto_withdrawal.active && config.auto_withdrawal.accounts.find(a => a.name == '$delegators');
// If so then we need to load the list of delegators to the account
if(use_delegators) {
if(fs.existsSync('delegators.json')) {
delegators = JSON.parse(fs.readFileSync("delegators.json"));
var vests = delegators.reduce(function (total, v) { return total + parseFloat(v.vesting_shares); }, 0);
utils.log('Delegators Loaded (from disk) - ' + delegators.length + ' delegators and ' + vests + ' VESTS in total!');
}
else
{
var del = require('./delegators');
utils.log('Started loading delegators from account history...');
del.loadDelegations(client, config.account, function(d) {
delegators = d;
var vests = delegators.reduce(function (total, v) { return total + parseFloat(v.vesting_shares); }, 0);
utils.log('Delegators Loaded (from account history) - ' + delegators.length + ' delegators and ' + vests + ' VESTS in total!');
// Save the list of delegators to disk
saveDelegators();
});
}
}
// Schedule to run every 10 seconds
setInterval(startProcess, 10000);
// Load updated STEEM and SBD prices every 30 minutes
loadPrices();
setInterval(loadPrices, 30 * 60 * 1000);
}
function startProcess() {
// Load the settings from the config file each time so we can pick up any changes
loadConfig();
// Load the bot account info
client.database.getAccounts([config.account]).then(function (result) {
account = result[0];
if (account && !isVoting) {
var vp = utils.getVotingPower(account);
if(config.detailed_logging) {
var bids_steem = utils.format(outstanding_bids.reduce(function(t, b) { return t + ((b.currency == 'STEEM') ? b.amount : 0); }, 0), 3);
var bids_sbd = utils.format(outstanding_bids.reduce(function(t, b) { return t + ((b.currency == 'SBD') ? b.amount : 0); }, 0), 3);
utils.log((config.backup_mode ? '* BACKUP MODE *' : '') + 'Voting Power: ' + utils.format(vp / 100) + '% | Time until next round: ' + utils.toTimer(utils.timeTilFullPower(vp)) + ' | Bids: ' + outstanding_bids.length + ' | ' + bids_sbd + ' SBD | ' + bids_steem + ' STEEM');
}
// We are at 100% voting power - time to vote!
if (vp >= 10000 && outstanding_bids.length > 0 && round_end_timeout < 0) {
round_end_timeout = setTimeout(function() {
round_end_timeout = -1;
// Don't process any bids while we are voting due to race condition (they will be processed when voting is done).
isVoting = first_load = true;
// Make a copy of the list of outstanding bids and vote on them
startVoting(outstanding_bids.slice().reverse());
// Save the last round of bids for use in API call
last_round = outstanding_bids.slice();
// Some bids might have been pushed to the next round, so now move them to the current round
outstanding_bids = next_round.slice();
// Reset the next round
next_round = [];
// Send out earnings if frequency is set to every round
if (config.auto_withdrawal.frequency == 'round_end')
processWithdrawals();
// Save the state of the bot to disk
saveState();
}, 30 * 1000);
}
// Load transactions to the bot account
getTransactions(saveState);
// Check if there are any rewards to claim.
claimRewards();
// Check if it is time to withdraw funds.
if (config.auto_withdrawal.frequency == 'daily')
checkAutoWithdraw();
}
}, function(err) {
logError('Error loading bot account: ' + err);
});
}
function startVoting(bids) {
if(config.backup_mode) {
utils.log('*** Bidding Round End - Backup Mode, no voting ***');
setTimeout(function () { isVoting = false; first_load = true; }, 5 * 60 * 1000);
return;
}
// Sum the amounts of all of the bids
var total = bids.reduce(function(total, bid) {
return total + getUsdValue(bid);
}, 0);
var bids_steem = utils.format(outstanding_bids.reduce(function(t, b) { return t + ((b.currency == 'STEEM') ? b.amount : 0); }, 0), 3);
var bids_sbd = utils.format(outstanding_bids.reduce(function(t, b) { return t + ((b.currency == 'SBD') ? b.amount : 0); }, 0), 3);
utils.log('=======================================================');
utils.log('Bidding Round End! Starting to vote! Total bids: ' + bids.length + ' - $' + total + ' | ' + bids_sbd + ' SBD | ' + bids_steem + ' STEEM');
var adjusted_weight = 1;
if(config.max_roi != null && config.max_roi != undefined && !isNaN(config.max_roi)) {
var vote_value = utils.getVoteValue(100, account, 10000, steem_price);
var vote_value_usd = utils.getVoteValueUSD(vote_value, sbd_price)
//min_total_bids_value_usd: calculates the minimum value in USD that the total bids must have to represent a maximum ROI defined in config.json
//'max_roi' in config.json = 10 represents a maximum ROI of 10%
var min_total_bids_value_usd = vote_value_usd * AUTHOR_PCT * ((100 - config.max_roi) / 100 );
// calculates the value of the weight of the vote needed to give the maximum ROI defined
adjusted_weight = (total < min_total_bids_value_usd) ? (total / min_total_bids_value_usd) : 1;
utils.log('Total vote weight: ' + (config.batch_vote_weight * adjusted_weight));
}
utils.log('=======================================================');
for(var i = 0; i < bids.length; i++) {
// Calculate the vote weight to be used for each bid based on the amount bid as a percentage of the total bids
bids[i].weight = Math.round(config.batch_vote_weight * adjusted_weight * 100 * (getUsdValue(bids[i]) / total));
}
comment(bids.slice());
vote(bids);
}
function vote(bids) {
// Get the first bid in the list
sendVote(bids.pop(), 0, function () {
// If there are more bids, vote on the next one after 10 seconds
if (bids.length > 0) {
setTimeout(function () { vote(bids); }, 5000);
} else {
setTimeout(function () {
utils.log('=======================================================');
utils.log('Voting Complete!');
utils.log('=======================================================');
isVoting = false;
first_load = true;
}, 5000);
}
});
}
function comment(bids) {
sendComment(bids.pop());
if(bids.length > 0)
setTimeout(function () { comment(bids); }, 30000);
}
function sendVote(bid, retries, callback) {
utils.log('Casting: ' + utils.format(bid.weight / 100) + '% vote cast for: @' + bid.author + '/' + bid.permlink);
validatePost(bid.author, bid.permlink, true, function(e) {
if(e) {
utils.log('Post @' + bid.author + '/' + bid.permlink + ' is invalid for reason: ' + e);
if(callback)
callback();
} else {
client.broadcast.vote({ voter: account.name, author: bid.author, permlink: bid.permlink, weight: bid.weight }, dsteem.PrivateKey.fromString(config.posting_key)).then(function(result) {
if (result) {
utils.log(utils.format(bid.weight / 100) + '% vote cast for: @' + bid.author + '/' + bid.permlink);
if (callback)
callback();
}
}, function(err) {
logError('Error sending vote for: @' + bid.author + '/' + bid.permlink + ', Error: ' + err);
// Try again on error
if(retries < 2)
setTimeout(function() { sendVote(bid, retries + 1, callback); }, 10000);
else {
utils.log('============= Vote transaction failed three times for: @' + bid.author + '/' + bid.permlink + ' Bid Amount: ' + bid.amount + ' ' + bid.currency + ' ===============');
logFailedBid(bid, err);
if (callback)
callback();
}
});
}
});
}
function sendComment(bid) {
var content = null;
if(config.comment_location && config.comment_location != '') {
content = fs.readFileSync(config.comment_location, "utf8");
} else if (config.promotion_content && config.promotion_content != '') {
content = config.promotion_content;
}
// If promotion content is specified in the config then use it to comment on the upvoted post
if (content && content != '') {
// Generate the comment permlink via steemit standard convention
var permlink = 're-' + bid.author.replace(/\./g, '') + '-' + bid.permlink + '-' + new Date().toISOString().replace(/-|:|\./g, '').toLowerCase();
// Replace variables in the promotion content
content = content.replace(/\{weight\}/g, utils.format(bid.weight / 100)).replace(/\{botname\}/g, config.account).replace(/\{sender\}/g, bid.sender);
var comment = {
author: account.name,
permlink: permlink,
parent_author: bid.author,
parent_permlink: bid.permlink,
title: permlink,
body: content,
json_metadata: '{"app":"postpromoter/' + version + '"}'
};
// Broadcast the comment
client.broadcast.comment(comment, dsteem.PrivateKey.fromString(config.posting_key)).then(function (result) {
if (result)
utils.log('Posted comment: ' + permlink);
}, function(err) { logError('Error posting comment: ' + permlink); });
}
// Check if the bot should resteem this post
if (config.min_resteem && bid.amount >= config.min_resteem)
resteem(bid);
}
function resteem(bid) {
var json = JSON.stringify(['reblog', {
account: config.account,
author: bid.author,
permlink: bid.permlink
}]);
client.broadcast.json({ id: 'follow', json: json, required_auths: [], required_posting_auths: [config.account] }, dsteem.PrivateKey.fromString(config.posting_key)).then(function(result) {
if (result)
utils.log('Resteemed Post: @' + bid.sender + '/' + bid.permlink);
}, function(err) {
utils.log('Error resteeming post: @' + bid.sender + '/' + bid.permlink);
});
}
function getTransactions(callback) {
var last_trx_id = null;
var num_trans = 50;
// If this is the first time the bot is ever being run, start with just the most recent transaction
if (first_load && transactions.length == 0) {
utils.log('First run - starting with last transaction on account.');
}
// If this is the first time the bot is run after a restart get a larger list of transactions to make sure none are missed
if (first_load && transactions.length > 0) {
utils.log('First run - loading all transactions since last transaction processed: ' + transactions[transactions.length - 1]);
last_trx_id = transactions[transactions.length - 1];
num_trans = 1000;
}
client.database.call('get_account_history', [account.name, -1, num_trans]).then(function (result) {
// On first load, just record the list of the past 50 transactions so we don't double-process them.
if (first_load && transactions.length == 0) {
transactions = result.map(r => r[1].trx_id).filter(t => t != '0000000000000000000000000000000000000000');
first_load = false;
utils.log(transactions.length + ' previous trx_ids recorded.');
if(callback)
callback();
return;
}
first_load = false;
var reached_starting_trx = false;
for (var i = 0; i < result.length; i++) {
var trans = result[i];
var op = trans[1].op;
// Don't need to process virtual ops
if(trans[1].trx_id == '0000000000000000000000000000000000000000')
continue;
// Check that this is a new transaction that we haven't processed already
if(transactions.indexOf(trans[1].trx_id) < 0) {
// If the bot was restarted after being stopped for a while, don't process transactions until we're past the last trx_id that was processed
if(last_trx_id && !reached_starting_trx) {
if(trans[1].trx_id == last_trx_id)
reached_starting_trx = true;
continue;
}
if(config.debug_logging)
utils.log('Processing Transaction: ' + JSON.stringify(trans));
// We only care about transfers to the bot
if (op[0] == 'transfer' && op[1].to == config.account) {
var amount = parseFloat(op[1].amount);
var currency = utils.getCurrency(op[1].amount);
utils.log("Incoming Bid! From: " + op[1].from + ", Amount: " + op[1].amount + ", memo: " + op[1].memo);
// Check for min and max bid values in configuration settings
var min_bid = config.min_bid ? parseFloat(config.min_bid) : 0;
var max_bid = config.max_bid ? parseFloat(config.max_bid) : 9999;
var max_bid_whitelist = config.max_bid_whitelist ? parseFloat(config.max_bid_whitelist) : 9999;
if(config.disabled_mode) {
// Bot is disabled, refund all Bids
refund(op[1].from, amount, currency, 'bot_disabled');
} else if(amount < min_bid) {
// Bid amount is too low (make sure it's above the min_refund_amount setting)
if(!config.min_refund_amount || amount >= config.min_refund_amount)
refund(op[1].from, amount, currency, 'below_min_bid');
else {
utils.log('Invalid bid - below min bid amount and too small to refund.');
}
} else if (amount > max_bid && whitelist.indexOf(op[1].from) < 0) {
// Bid amount is too high
refund(op[1].from, amount, currency, 'above_max_bid');
} else if (amount > max_bid_whitelist) {
// Bid amount is too high even for whitelisted users!
refund(op[1].from, amount, currency, 'above_max_bid_whitelist');
} else if(config.currencies_accepted && config.currencies_accepted.indexOf(currency) < 0) {
// Sent an unsupported currency
refund(op[1].from, amount, currency, 'invalid_currency');
} else {
// Bid amount is just right!
checkPost(op[1].memo, amount, currency, op[1].from, 0);
}
} else if(use_delegators && op[0] == 'delegate_vesting_shares' && op[1].delegatee == account.name) {
// If we are paying out to delegators, then update the list of delegators when new delegation transactions come in
var delegator = delegators.find(d => d.delegator == op[1].delegator);
if(delegator)
delegator.new_vesting_shares = op[1].vesting_shares;
else {
delegator = { delegator: op[1].delegator, vesting_shares: 0, new_vesting_shares: op[1].vesting_shares };
delegators.push(delegator);
}
// Save the updated list of delegators to disk
saveDelegators();
// Check if we should send a delegation message
if(parseFloat(delegator.new_vesting_shares) > parseFloat(delegator.vesting_shares) && config.transfer_memos['delegation'] && config.transfer_memos['delegation'] != '')
refund(op[1].delegator, 0.001, 'SBD', 'delegation', 0, utils.vestsToSP(parseFloat(delegator.new_vesting_shares)).toFixed());
utils.log('*** Delegation Update - ' + op[1].delegator + ' has delegated ' + op[1].vesting_shares);
}
// Save the ID of the last transaction that was processed.
transactions.push(trans[1].trx_id);
// Don't save more than the last 60 transaction IDs in the state
if(transactions.length > 60)
transactions.shift();
}
}
if (callback)
callback();
}, function(err) {
logError('Error loading account history: ' + err);
if (callback)
callback();
});
}
function checkRoundFillLimit(round, amount, currency) {
if(config.round_fill_limit == null || config.round_fill_limit == undefined || isNaN(config.round_fill_limit))
return false;
var vote_value = utils.getVoteValue(100, account, 10000, steem_price);
var vote_value_usd = utils.getVoteValueUSD(vote_value, sbd_price)
var bid_value = round.reduce(function(t, b) { return t + b.amount * ((b.currency == 'SBD') ? sbd_price : steem_price) }, 0);
var new_bid_value = amount * ((currency == 'SBD') ? sbd_price : steem_price);
// Check if the value of the bids is over the round fill limit
return (vote_value_usd * AUTHOR_PCT * config.round_fill_limit < bid_value + new_bid_value);
}
function validatePost(author, permlink, isVoting, callback, retries) {
client.database.call('get_content', [author, permlink]).then(function (result) {
if (result && result.id > 0) {
// If comments are not allowed then we need to first check if the post is a comment
if(!config.allow_comments && (result.parent_author != null && result.parent_author != '')) {
if(callback)
callback('no_comments');
return;
}
// Check if any tags on this post are blacklisted in the settings
if (config.blacklist_settings.blacklisted_tags && config.blacklist_settings.blacklisted_tags.length > 0 && result.json_metadata && result.json_metadata != '') {
var tags = JSON.parse(result.json_metadata).tags;
if (tags && tags.length > 0) {
var tag = tags.find(t => config.blacklist_settings.blacklisted_tags.indexOf(t) >= 0);
if(tag) {
if(callback)
callback('blacklist_tag');
return;
}
}
}
var created = new Date(result.created + 'Z');
var time_until_vote = isVoting ? 0 : utils.timeTilFullPower(utils.getVotingPower(account));
// Get the list of votes on this post to make sure the bot didn't already vote on it (you'd be surprised how often people double-submit!)
var votes = result.active_votes.filter(function(vote) { return vote.voter == account.name; });
if (votes.length > 0 || ((new Date() - created) >= (config.max_post_age * 60 * 60 * 1000) && !isVoting)) {
// This post is already voted on by this bot or the post is too old to be voted on
if(callback)
callback(((votes.length > 0) ? 'already_voted' : 'max_age'));
return;
}
// Check if this post has been flagged by any flag signal accounts
if(config.blacklist_settings.flag_signal_accounts) {
var flags = result.active_votes.filter(function(v) { return v.percent < 0 && config.blacklist_settings.flag_signal_accounts.indexOf(v.voter) >= 0; });
if(flags.length > 0) {
if(callback)
callback('flag_signal_account');
return;
}
}
// Check if this post is below the minimum post age
if(config.min_post_age && config.min_post_age > 0 && (new Date() - created + (time_until_vote * 1000)) < (config.min_post_age * 60 * 1000)) {
if(callback)
callback('min_age');
return;
}
// Post is good!
if(callback)
callback();
} else {
// Invalid memo
if(callback)
callback('invalid_post_url');
return;
}
}, function(err) {
logError('Error loading post: ' + permlink + ', Error: ' + err);
// Try again on error
if(retries < 2)
setTimeout(function() { validatePost(author, permlink, isVoting, callback, retries + 1); }, 3000);
else {
utils.log('============= Validate post failed three times for: ' + permlink + ' ===============');
if(callback)
callback('invalid_post_url');
return;
}
});
}
function checkPost(memo, amount, currency, sender, retries) {
var affiliate = null;
// Check if this bid is through an affiliate service
if(config.affiliates && config.affiliates.length > 0) {
for(var i = 0; i < config.affiliates.length; i++) {
var cur = config.affiliates[i];
if(memo.startsWith(cur.name)) {
memo = memo.split(' ')[1];
affiliate = cur;
break;
}
}
}
// Parse the author and permlink from the memo URL
var permLink = memo.substr(memo.lastIndexOf('/') + 1);
var site = memo.substring(memo.indexOf('://')+3,memo.indexOf('/', memo.indexOf('://')+3));
switch(site) {
case 'd.tube':
var author = memo.substring(memo.indexOf("/v/")+3,memo.lastIndexOf('/'));
break;
case 'dmania.lol':
var author = memo.substring(memo.indexOf("/post/")+6,memo.lastIndexOf('/'));
break;
default:
var author = memo.substring(memo.lastIndexOf('@') + 1, memo.lastIndexOf('/'));
}
if (author == '' || permLink == '') {
refund(sender, amount, currency, 'invalid_post_url');
return;
}
// Make sure the author isn't on the blacklist!
if(whitelist.indexOf(author) < 0 && (blacklist.indexOf(author) >= 0 || blacklist.indexOf(sender) >= 0))
{
handleBlacklist(author, sender, amount, currency);
return;
}
// If this bot is whitelist-only then make sure the sender is on the whitelist
if(config.blacklist_settings.whitelist_only && whitelist.indexOf(sender) < 0) {
refund(sender, amount, currency, 'whitelist_only');
return;
}
// Check if this author has gone over the max bids per author per round
if(config.max_per_author_per_round && config.max_per_author_per_round > 0) {
if(outstanding_bids.filter(b => b.author == author).length >= config.max_per_author_per_round)
{
refund(sender, amount, currency, 'bids_per_round');
return;
}
}
var push_to_next_round = false;
checkGlobalBlacklist(author, sender, function(onBlacklist) {
if(onBlacklist) {
handleBlacklist(author, sender, amount, currency);
return;
}
validatePost(author, permLink, false, function(error) {
if(error && error != 'min_age') {
refund(sender, amount, currency, error);
return;
}
// Check if the round is full
if(checkRoundFillLimit(outstanding_bids, amount, currency)) {
if(checkRoundFillLimit(next_round, amount, currency)) {
refund(sender, amount, currency, 'next_round_full');
return;
} else {
push_to_next_round = true;
refund(sender, 0.001, currency, 'round_full');
}
}
// Add the bid to the current round or the next round if the current one is full or the post is too new
var round = (push_to_next_round || error == 'min_age') ? next_round : outstanding_bids;
// Check if there is already a bid for this post in the current round
var existing_bid = round.find(bid => bid.url == memo);
if(existing_bid) {
// There is already a bid for this post in the current round
utils.log('Existing Bid Found - New Amount: ' + amount + ', Total Amount: ' + (existing_bid.amount + amount));
var new_amount = 0;
if(existing_bid.currency == currency) {
new_amount = existing_bid.amount + amount;
} else if(existing_bid.currency == 'STEEM') {
new_amount = existing_bid.amount + amount * sbd_price / steem_price;
} else if(existing_bid.currency == 'SBD') {
new_amount = existing_bid.amount + amount * steem_price / sbd_price;
}
var max_bid = config.max_bid ? parseFloat(config.max_bid) : 9999;
// Check that the new total doesn't exceed the max bid amount per post
if (new_amount > max_bid)
refund(sender, amount, currency, 'above_max_bid');
else
existing_bid.amount = new_amount;
} else {
// All good - push to the array of valid bids for this round
utils.log('Valid Bid - Amount: ' + amount + ' ' + currency + ', Url: ' + memo);
round.push({ amount: amount, currency: currency, sender: sender, author: author, permlink: permLink, url: memo });
// If this bid is through an affiliate service, send the fee payout
if(affiliate) {
refund(affiliate.beneficiary, amount * (affiliate.fee_pct / 10000), currency, 'affiliate', 0, 'Sender: @' + sender + ', Post: ' + memo);
}
}
// If a witness_vote transfer memo is set, check if the sender votes for the bot owner as witness and send them a message if not
if (config.transfer_memos['witness_vote'] && config.transfer_memos['witness_vote'] != '') {
checkWitnessVote(sender, sender, currency);
} else if(!push_to_next_round && config.transfer_memos['bid_confirmation'] && config.transfer_memos['bid_confirmation'] != '') {
// Send bid confirmation transfer memo if one is specified
refund(sender, 0.001, currency, 'bid_confirmation', 0);
}
});
});
}
function checkGlobalBlacklist(author, sender, callback) {
if(!config.blacklist_settings || !config.blacklist_settings.global_api_blacklists || !Array.isArray(config.blacklist_settings.global_api_blacklists)) {
callback(null);
return;
}
request.get('http://blacklist.usesteem.com/user/' + author, function(e, r, data) {
try {
var result = JSON.parse(data);
if(!result.blacklisted || !Array.isArray(result.blacklisted)) {
callback(null);
return;
}
if(author != sender) {
checkGlobalBlacklist(sender, sender, callback);
} else
callback(config.blacklist_settings.global_api_blacklists.find(b => result.blacklisted.indexOf(b) >= 0));
} catch(err) {
utils.log('Error loading global blacklist info for user @' + author + ', Error: ' + err);
callback(null);
}
});
}
function handleBlacklist(author, sender, amount, currency) {
utils.log('Invalid Bid - @' + author + ((author != sender) ? ' or @' + sender : '') + ' is on the blacklist!');
// Refund the bid only if blacklist_refunds are enabled in config
if (config.blacklist_settings.refund_blacklist)
refund(sender, amount, currency, 'blacklist_refund', 0);
else {
// Otherwise just send a 0.001 transaction with blacklist memo
if (config.transfer_memos['blacklist_no_refund'] && config.transfer_memos['blacklist_no_refund'] != '')
refund(sender, 0.001, currency, 'blacklist_no_refund', 0);
// If a blacklist donation account is specified then send funds from blacklisted users there
if (config.blacklist_settings.blacklist_donation_account)
refund(config.blacklist_settings.blacklist_donation_account, amount - 0.001, currency, 'blacklist_donation', 0);
}
}
function handleFlag(sender, amount, currency) {
utils.log('Invalid Bid - This post has been flagged by one or more spam / abuse indicator accounts.');
// Refund the bid only if blacklist_refunds are enabled in config
if (config.blacklist_settings.refund_blacklist)
refund(sender, amount, currency, 'flag_refund', 0);
else {
// Otherwise just send a 0.001 transaction with blacklist memo
if (config.transfer_memos['flag_no_refund'] && config.transfer_memos['flag_no_refund'] != '')
refund(sender, 0.001, currency, 'flag_no_refund', 0);
// If a blacklist donation account is specified then send funds from blacklisted users there
if (config.blacklist_settings.blacklist_donation_account)
refund(config.blacklist_settings.blacklist_donation_account, amount - 0.001, currency, 'blacklist_donation', 0);
}
}
function checkWitnessVote(sender, voter, currency) {
if(!config.owner_account || config.owner_account == '')
return;
client.database.getAccounts([voter]).then(function (result) {
if (result) {
if (result[0].proxy && result[0].proxy != '') {
checkWitnessVote(sender, result[0].proxy, currency);
return;
}
if(result[0].witness_votes.indexOf(config.owner_account) < 0)
refund(sender, 0.001, currency, 'witness_vote', 0);
else if(config.transfer_memos['bid_confirmation'] && config.transfer_memos['bid_confirmation'] != '') {
// Send bid confirmation transfer memo if one is specified
refund(sender, 0.001, currency, 'bid_confirmation', 0);
}
}
}, function(err) {
logError('Error loading sender account to check witness vote: ' + err);
});
}
function saveState() {
var state = {
outstanding_bids: outstanding_bids,
last_round: last_round,
next_round: next_round,
transactions: transactions,
last_withdrawal: last_withdrawal,
version: version
};
// Save the state of the bot to disk
fs.writeFile('state.json', JSON.stringify(state, null, 2), function (err) {
if (err)
utils.log(err);
});
}
function updateVersion(old_version, new_version) {
utils.log('**** Performing Update Steps from version: ' + old_version + ' to version: ' + new_version);
if(!old_version) {
if(fs.existsSync('delegators.json')) {
fs.rename('delegators.json', 'old-delegators.json', (err) => {
if (err)
utils.log('Error renaming delegators file: ' + err);
else
utils.log('Renamed delegators.json file so it will be reloaded from account history.');
});
}
}
}
function saveDelegators() {
// Save the list of delegators to disk
fs.writeFile('delegators.json', JSON.stringify(delegators), function (err) {
if (err)
utils.log('Error saving delegators to disk: ' + err);
});
}
function refund(sender, amount, currency, reason, retries, data) {
if(config.backup_mode) {
utils.log('Backup Mode - not sending refund of ' + amount + ' ' + currency + ' to @' + sender + ' for reason: ' + reason);
return;
}
if(!retries)
retries = 0;
// Make sure refunds are enabled and the sender isn't on the no-refund list (for exchanges and things like that).
if (reason != 'forward_payment' && (!config.refunds_enabled || sender == config.account || (config.no_refund && config.no_refund.indexOf(sender) >= 0))) {
utils.log("Invalid bid - " + reason + ' NO REFUND');
// If this is a payment from an account on the no_refund list, forward the payment to the post_rewards_withdrawal_account
if(config.no_refund && config.no_refund.indexOf(sender) >= 0 && config.post_rewards_withdrawal_account && config.post_rewards_withdrawal_account != '' && sender != config.post_rewards_withdrawal_account)
refund(config.post_rewards_withdrawal_account, amount, currency, 'forward_payment', 0, sender);
return;
}
// Replace variables in the memo text
var memo = config.transfer_memos[reason];
if(!memo)
memo = reason;
memo = memo.replace(/{amount}/g, utils.format(amount, 3) + ' ' + currency);
memo = memo.replace(/{currency}/g, currency);
memo = memo.replace(/{min_bid}/g, config.min_bid);
memo = memo.replace(/{max_bid}/g, config.max_bid);
memo = memo.replace(/{max_bid_whitelist}/g, config.max_bid_whitelist);
memo = memo.replace(/{account}/g, config.account);
memo = memo.replace(/{owner}/g, config.owner_account);
memo = memo.replace(/{min_age}/g, config.min_post_age);
memo = memo.replace(/{sender}/g, sender);
memo = memo.replace(/{tag}/g, data);
var days = Math.floor(config.max_post_age / 24);
var hours = (config.max_post_age % 24);
memo = memo.replace(/{max_age}/g, days + ' Day(s)' + ((hours > 0) ? ' ' + hours + ' Hour(s)' : ''));
// Issue the refund.
client.broadcast.transfer({ amount: utils.format(amount, 3) + ' ' + currency, from: config.account, to: sender, memo: memo }, dsteem.PrivateKey.fromString(config.active_key)).then(function(response) {
utils.log('Refund of ' + amount + ' ' + currency + ' sent to @' + sender + ' for reason: ' + reason);
}, function(err) {
logError('Error sending refund to @' + sender + ' for: ' + amount + ' ' + currency + ', Error: ' + err);
// Try again on error
if(retries < 2)
setTimeout(function() { refund(sender, amount, currency, reason, retries + 1, data) }, (Math.floor(Math.random() * 10) + 3) * 1000);
else
utils.log('============= Refund failed three times for: @' + sender + ' ===============');
});
}
function claimRewards() {
if (!config.auto_claim_rewards || config.backup_mode)
return;
// Make api call only if you have actual reward
if (parseFloat(account.reward_steem_balance) > 0 || parseFloat(account.reward_sbd_balance) > 0 || parseFloat(account.reward_vesting_balance) > 0) {
var op = ['claim_reward_balance', { account: config.account, reward_sbd: account.reward_sbd_balance, reward_steem: account.reward_steem_balance, reward_vests: account.reward_vesting_balance }];
client.broadcast.sendOperations([op], dsteem.PrivateKey.fromString(config.posting_key)).then(function(result) {
if (result) {
if(config.detailed_logging) {
var rewards_message = "$$$ ==> Rewards Claim";
if (parseFloat(account.reward_sbd_balance) > 0) { rewards_message = rewards_message + ' SBD: ' + parseFloat(account.reward_sbd_balance); }
if (parseFloat(account.reward_steem_balance) > 0) { rewards_message = rewards_message + ' STEEM: ' + parseFloat(account.reward_steem_balance); }
if (parseFloat(account.reward_vesting_balance) > 0) { rewards_message = rewards_message + ' VESTS: ' + parseFloat(account.reward_vesting_balance); }
utils.log(rewards_message);
}
// If there are liquid SBD rewards, withdraw them to the specified account
if(parseFloat(account.reward_sbd_balance) > 0 && config.post_rewards_withdrawal_account && config.post_rewards_withdrawal_account != '') {
// Send liquid post rewards to the specified account
client.broadcast.transfer({ amount: account.reward_sbd_balance, from: config.account, to: config.post_rewards_withdrawal_account, memo: 'Liquid Post Rewards Withdrawal' }, dsteem.PrivateKey.fromString(config.active_key)).then(function(response) {
utils.log('$$$ Auto withdrawal - liquid post rewards: ' + account.reward_sbd_balance + ' sent to @' + config.post_rewards_withdrawal_account);
}, function(err) { utils.log('Error transfering liquid SBD post rewards: ' + err); });
}
// If there are liquid STEEM rewards, withdraw them to the specified account
if(parseFloat(account.reward_steem_balance) > 0 && config.post_rewards_withdrawal_account && config.post_rewards_withdrawal_account != '') {
// Send liquid post rewards to the specified account
client.broadcast.transfer({ amount: account.reward_steem_balance, from: config.account, to: config.post_rewards_withdrawal_account, memo: 'Liquid Post Rewards Withdrawal' }, dsteem.PrivateKey.fromString(config.active_key)).then(function(response) {
utils.log('$$$ Auto withdrawal - liquid post rewards: ' + account.reward_steem_balance + ' sent to @' + config.post_rewards_withdrawal_account);
}, function(err) { utils.log('Error transfering liquid STEEM post rewards: ' + err); });
}
}
}, function(err) { utils.log('Error claiming rewards...will try again next time.'); });
}
}
function checkAutoWithdraw() {
// Check if auto-withdraw is active
if (!config.auto_withdrawal.active)
return;
// If it's past the withdrawal time and we haven't made a withdrawal today, then process the withdrawal
if (new Date(new Date().toDateString()) > new Date(last_withdrawal) && new Date().getHours() >= config.auto_withdrawal.execute_time) {
processWithdrawals();
}
}
function processWithdrawals() {
if(config.backup_mode)
return;
var has_sbd = config.currencies_accepted.indexOf('SBD') >= 0 && parseFloat(account.sbd_balance) > 0;
var has_steem = config.currencies_accepted.indexOf('STEEM') >= 0 && parseFloat(account.balance) > 0;
if (has_sbd || has_steem) {
// Save the date of the last withdrawal
last_withdrawal = new Date().toDateString();
var total_stake = config.auto_withdrawal.accounts.reduce(function (total, info) { return total + info.stake; }, 0);
var withdrawals = [];
for(var i = 0; i < config.auto_withdrawal.accounts.length; i++) {
var withdrawal_account = config.auto_withdrawal.accounts[i];
// If this is the special $delegators account, split it between all delegators to the bot
if(withdrawal_account.name == '$delegators') {
// Check if/where we should send payout for SP in the bot account directly
if(withdrawal_account.overrides) {
var bot_override = withdrawal_account.overrides.find(o => o.name == config.account);
if(bot_override && bot_override.beneficiary) {
var bot_delegator = delegators.find(d => d.delegator == config.account);
// Calculate the amount of SP in the bot account and add/update it in the list of delegators
var bot_vesting_shares = (parseFloat(account.vesting_shares) - parseFloat(account.delegated_vesting_shares)).toFixed(6) + ' VESTS';
if(bot_delegator)
bot_delegator.vesting_shares = bot_vesting_shares;
else
delegators.push({ delegator: config.account, vesting_shares: bot_vesting_shares });
}
}
// Get the total amount delegated by all delegators
var total_vests = delegators.reduce(function (total, v) { return total + parseFloat(v.vesting_shares); }, 0);
// Send the withdrawal to each delegator based on their delegation amount
for(var j = 0; j < delegators.length; j++) {
var delegator = delegators[j];
var to_account = delegator.delegator;
// Check if this delegator has an override and if so send the payment to the beneficiary instead
if(withdrawal_account.overrides) {
var override = withdrawal_account.overrides.find(o => o.name == to_account);
if(override && override.beneficiary)
to_account = override.beneficiary;
}
if(has_sbd) {
// Check if there is already an SBD withdrawal to this account
var withdrawal = withdrawals.find(w => w.to == to_account && w.currency == 'SBD');
if(withdrawal) {
withdrawal.amount += parseFloat(account.sbd_balance) * (withdrawal_account.stake / total_stake) * (parseFloat(delegator.vesting_shares) / total_vests) - 0.001;
} else {
withdrawals.push({
to: to_account,
currency: 'SBD',
amount: parseFloat(account.sbd_balance) * (withdrawal_account.stake / total_stake) * (parseFloat(delegator.vesting_shares) / total_vests) - 0.001
});
}
}
if(has_steem) {