Advertisement
Guest User

antpool_block_alert

a guest
Feb 6th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Put your parameters here:
  2. // The Gmail address should be a mock one you don't use
  3. var yourGmailAddress = 'xxx@gmail.com'
  4. var yourGmailPassword = 'xxx'
  5. // This is your normal email, that will receive the email alerts when Antpool claims a block
  6. var yourEmail = 'xxx@xxx.xxx'
  7. // Open an account in AntPool and create an API key, follow their documentation: https://www.antpool.com/user/apiGuild.htm
  8. var yourApiKey = 'xxx'
  9. // You will have to encrypt the autentication message in SHA256, go to https://www.freeformatter.com/hmac-generator.html and introduce:
  10. // a) In "string" type (without spaces in between): your_userID + your_api_key + 2
  11. // b) In "secret" type the secret key the Antpool API registering page returned you when you were creating the API
  12. // Then wite down here as "yourHMAC" the resulting encrypted message
  13. var yourHMAC = 'xxx'
  14.  
  15.  
  16. var fs = require('fs');
  17. var request = require('C:/nodejs/node_modules/request');
  18. var nodemailer = require('C:/nodejs/node_modules/nodemailer');
  19. var cron = require('C:/nodejs/node_modules/cron');
  20.  
  21.  
  22. var transporter = nodemailer.createTransport({
  23.     service: 'gmail',
  24.     auth: {
  25.         user: yourGmailAddress,
  26.         pass: yourGmailPassword
  27.     }
  28. });
  29.  
  30. // Loading the log file here
  31. var data1 = '';
  32. var chunk1;
  33. var stream1 = fs.createReadStream("antpoolblocks.json")
  34. stream1.on('readable', function() { //Function just to read the whole file before proceeding
  35.     while ((chunk1=stream1.read()) != null) {
  36.         data1 += chunk1;}
  37. });
  38. stream1.on('end', function() {
  39.     if (data1 != "") {
  40.         var antpoolReports = JSON.parse(data1)
  41.     } else {
  42.         var antpoolReports = [] // Empty array
  43.     }
  44.  
  45.     if (antpoolReports.length > 0) {
  46.         knownBlocks = antpoolReports[antpoolReports.length-1].reportedblocks
  47.     } else {
  48.         knownBlocks = 0
  49.     }
  50.  
  51.     // Calling the API every 5 minutes
  52.     var cronJob = cron.job("00 0,5,10,15,20,25,30,35,40,45,50,55 * * * *", function(){
  53.  
  54.         var sign = yourHMAC.toUpperCase()
  55.  
  56.         var now = Date.now();
  57.         request.post({
  58.             url: 'https://antpool.com/api/poolStats.htm',
  59.             json: true,
  60.             form: {
  61.                 key: yourApiKey,
  62.                 nonce: 2,
  63.                 signature: sign,
  64.                 coin: 'SC'
  65.             }
  66.         }, function (err, httpResponse, body) {
  67.             var newReportedBlocks = body.data.totalBlockNumber
  68.             if (newReportedBlocks > knownBlocks) {
  69.                 console.log("Reported a block!!")
  70.                 entry = {"time": now, "reportedblocks": newReportedBlocks}
  71.                 console.log(entry)
  72.                 antpoolReports.push(entry)
  73.                 knownBlocks = newReportedBlocks
  74.                
  75.                 // Save file
  76.                 var stream2 = fs.createWriteStream("antpoolblocks.json")
  77.                 var string2 = JSON.stringify(antpoolReports)
  78.                 stream2.write(string2)
  79.  
  80.                 var emailText = 'Antpool found blocks at: ' + now + ". Total count: " + knownBlocks
  81.                 // Email
  82.                 var mailOptions = {
  83.                     from: yourGmailAddress,
  84.                     to: yourEmail,
  85.                     subject: 'Antpool found blocks!',
  86.                     text: emailText
  87.                 }
  88.                 transporter.sendMail(mailOptions, function(error, info){
  89.                     if (error) {
  90.                         console.log(error);
  91.                     } else {
  92.                         console.log('Email sent: ' + info.response);
  93.                     }
  94.                 });
  95.  
  96.             } else {
  97.                 console.log("Nothing new at: " + now + " --> " + newReportedBlocks)
  98.             }
  99.         })
  100.     })
  101.     cronJob.start();
  102. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement