Guest User

Untitled

a guest
Feb 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. const fs = require("fs");
  2. const steem = require('steem'); // Include Steem Library
  3. const validUrl = require('valid-url'); // Include Valid-URL Library
  4.  
  5. var account = null;
  6. var trans_number = 0;
  7. var config = null;
  8. var outstanding_votes = [];
  9. var isVoting = false;
  10.  
  11. // Load the settings from the config file
  12. config = JSON.parse(fs.readFileSync("config.json"));
  13.  
  14. steem.api.setOptions({ url: config.steem_node }); // Set Steem Node
  15.  
  16. if (fs.existsSync('state.json')) {
  17. const state = JSON.parse(fs.readFileSync("state.json"));
  18.  
  19. if (state.trans_number) {
  20. trans_number = state.trans_number;
  21. }
  22.  
  23. console.log("Loaded state.json and set vars");
  24. }
  25.  
  26. loop();
  27. setInterval(loop, 10000); // Create Loop
  28.  
  29. function loop() {
  30. console.log('looping every 10 Seconds'); // Output
  31.  
  32. steem.api.getAccounts([config.account_name], function (err, result) { // Get Account Data
  33. if (err || !result) { // Check for Errors
  34. console.log('Error loading account: ' + err); // Output Error
  35. return;
  36. }
  37.  
  38. account = result[0]; // set account
  39. });
  40.  
  41. if (account) {
  42. steem.api.getAccountHistory(account.name, -1, 50, function (err, result) { // Get last 50 transactions of account
  43. if (err || !result) { // Check for errors
  44. console.log('Error loading account history: ' + err); // Output error
  45. return;
  46. }
  47.  
  48. result.forEach(function(trans) { // Loop through transactions
  49. var op = trans[1].op; // Get transaction data
  50.  
  51. if (trans[0] > trans_number) {
  52. if (op[0] == 'transfer' && op[1].to == account.name) { // Check if transaction is transfer TO user account
  53. if (validUrl.isUri(op[1].memo)) { // Check if memo contains valid URL
  54. var amount = op[1].amount;
  55. var currency = amount.substr(amount.indexOf(' ') + 1);
  56. amount = parseFloat(amount);
  57.  
  58. if(config.accepted_currencies && config.accepted_currencies.indexOf(currency) < 0) {
  59. console.log("INVALID CURRENCY SENT - SHOULD REFUND");
  60. refund(op[1].from, op[1].amount, 'invalid_currency');
  61. } else if(amount < config.min_bid) {
  62. console.log("BID TO LOW - SHOULD REFUND");
  63. refund(op[1].from, op[1].amount, 'invalid_bid');
  64. } else if (amount > config.max_bid) {
  65. console.log("BID TO HIGH - SHOULD REFUND");
  66. refund(op[1].from, op[1].amount, 'invalid_bid');
  67. } else {
  68. checkValidMemo(op, op[1].from, op[1].amount);
  69. }
  70. } else {
  71. console.log("MEMO NOT A URI - SHOULD REFUND");
  72. refund(op[1].from, op[1].amount, 'invalid_memo');
  73. }
  74. }
  75.  
  76. trans_number = trans[0];
  77. saveState();
  78. }
  79. });
  80. });
  81.  
  82. if (outstanding_votes.length > 0 && !isVoting) {
  83. sendVotes();
  84. }
  85. }
  86. }
  87.  
  88. function saveState() {
  89. var state = {
  90. trans_number: trans_number
  91. };
  92.  
  93. fs.writeFile('state.json', JSON.stringify(state), function (err) {
  94. if (err) {
  95. console.log(err);
  96. }
  97. });
  98. }
  99.  
  100. function sendVotes() {
  101. isVoting = true;
  102. vote(outstanding_votes.pop(), function() {
  103. if (outstanding_votes.length > 0) {
  104. setTimeout(function () { sendVotes(); }, 5000);
  105. } else {
  106. isVoting = false;
  107. }
  108. })
  109. }
  110.  
  111. function vote(vote, callback) {
  112. console.log('Voting: ' + vote);
  113.  
  114. steem.broadcast.vote(config.private_posting_key, account.name, vote.author, vote.permlink, 10000, function (err, result) {
  115. if (err && !result) {
  116. console.log('Voting failed: ' + err);
  117. return;
  118. }
  119.  
  120. sendComment(vote);
  121.  
  122. if (callback) {
  123. callback();
  124. }
  125. });
  126. }
  127.  
  128. function sendComment(vote) {
  129. const permlink = 're-' + vote.author.replace(/\./g, '') + '-' + vote.permlink + '-' + new Date().toISOString().replace(/-|:|\./g, '').toLowerCase();
  130. const comments = config.comments;
  131. const comment = comments[Math.floor(Math.random() * comments.length)];
  132.  
  133. // Broadcast the comment
  134. steem.broadcast.comment(config.private_posting_key, vote.author, vote.permlink, account.name, permlink, permlink, comment, '{"app":"upvoter/"}', function (err, result) {
  135. if (!err && result) {
  136. console.log('Posted comment: ' + permlink);
  137. } else {
  138. console.log('Error posting comment: ' + permlink + ', Error: ' + err);
  139. }
  140. });
  141. }
  142.  
  143. function checkValidMemo(transData, sender, amount) {
  144. if (isVoting) { // Exit early if bot is already voting
  145. return;
  146. }
  147.  
  148. const memo = transData[1].memo; // Get Memo
  149.  
  150. var permLink = memo.substr(memo.lastIndexOf('/') + 1); // Get permLink from memo
  151. var author = memo.substring(memo.lastIndexOf('@') + 1, memo.lastIndexOf('/')); // get Author from memo
  152.  
  153. steem.api.getContent(author, permLink, function (err, result) { // Get Post Data
  154. if (err || !result) {
  155. console.log('Not a valid url / author: ' + err); // Post does not exist
  156. refund(sender, amount, 'invalid_memo');
  157. return;
  158. }
  159.  
  160. // Disable comment upvoting
  161. if(result.parent_author != null && result.parent_author != '') {
  162. refund(sender, amount, 'no_comments');
  163. return;
  164. }
  165.  
  166. var created = new Date(result.created + 'Z');
  167. if ((new Date() - created) >= (config.max_post_age * 60 * 60 * 1000)) {
  168. console.log('The post is too old for upvoting it!');
  169. refund(sender, amount, 'post_to_old');
  170. return;
  171. }
  172.  
  173. var votes = result.active_votes.filter(function(vote) { return vote.voter == account.name; }); // Check if already voted
  174.  
  175. if (votes.length > 0 && votes[0].percent > 0) {
  176. console.log('Already voted on post');
  177. refund(sender, amount, 'already_voted');
  178. return;
  179. }
  180.  
  181. outstanding_votes.push({author: result.author, permlink: result.permlink}); // Add vote to outstanding vote list
  182. });
  183. }
  184.  
  185. function refund(sender, amount, memoType) {
  186. var message = config.memo_messages[memoType];
  187.  
  188. message = message.replace(/{sender}/g, sender);
  189. message = message.replace(/{bid}/g, amount);
  190.  
  191. steem.broadcast.transfer(config.private_active_key, config.account_name, sender, amount, message, function (err, result) {
  192. if (err || !result) {
  193. console.log("Refund failed ! For: " + sender);
  194. }
  195. });
  196. }
Add Comment
Please, Sign In to add comment