Browse Source

Исправление ошибок для #EBT-21
Некоторые новые комманды

Veloe 4 years ago
parent
commit
25fa259a1e

+ 2 - 0
EthermineBotTelegram/EthermineBotTelegram.csproj

@@ -159,6 +159,8 @@
     <Compile Include="EFDatabase.cs" />
     <Compile Include="JsonCurrentStats.cs" />
     <Compile Include="JsonDownloader.cs" />
+    <Compile Include="JsonEtherscanGas.cs" />
+    <Compile Include="JsonPayouts.cs" />
     <Compile Include="JsonWorker.cs" />
     <Compile Include="NetworkStats.cs" />
     <Compile Include="Program.cs" />

+ 6 - 3
EthermineBotTelegram/JsonCurrentStats.cs

@@ -1,10 +1,10 @@
-namespace EthermineBotTelegram
+namespace JsonCurrentStatsData
 {
     public class Data
     {
         public int time { get; set; }
         public int lastSeen { get; set; }
-        public int reportedHashrate { get; set; }
+        public long reportedHashrate { get; set; }
         public double currentHashrate { get; set; }
         public int validShares { get; set; }
         public int invalidShares { get; set; }
@@ -17,10 +17,13 @@
         public double usdPerMin { get; set; }
         public double btcPerMin { get; set; }
     }
+}
 
+namespace EthermineBotTelegram
+{
     public class JsonCurrentStats
     {
         public string status { get; set; }
-        public Data data { get; set; }
+        public JsonCurrentStatsData.Data data { get; set; }
     }
 }

+ 22 - 0
EthermineBotTelegram/JsonPayouts.cs

@@ -0,0 +1,22 @@
+using System.Collections.Generic;
+
+namespace JsonPayoutsData
+{
+    public class Datа
+    {
+        public int start { get; set; }
+        public int end { get; set; }
+        public object amount { get; set; }
+        public string txHash { get; set; }
+        public int paidOn { get; set; }
+    }
+}
+
+namespace EthermineBotTelegram
+{
+    public class JsonPayouts
+        {
+            public string status { get; set; }
+            public List<JsonPayoutsData.Datа> data { get; set; }
+        }
+}

+ 134 - 67
EthermineBotTelegram/Program.cs

@@ -12,10 +12,13 @@ using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
 using Newtonsoft.Json;
 using System.Net;
+using System.Net.Http;
+using System.Threading;
 using EFDatabase;
 using MySql.Data.MySqlClient;
 using Quartz;
 using Quartz.Impl;
+using Quartz.Util;
 
 namespace EthermineBotTelegram
 {
@@ -47,9 +50,6 @@ namespace EthermineBotTelegram
                 Botdb.Database.Connection.Open();
                 Botdb.Database.Connection.Close();
                 
-                
-                //InitUpdater("server=" + AppSettings.dbIp +  ";port=3306;database=botdb;uid=" + user + "; pwd=" + pass);
-            
                 // Grab the Scheduler instance from the Factory
                 StdSchedulerFactory factory = new StdSchedulerFactory();
                 IScheduler scheduler = await factory.GetScheduler();
@@ -73,7 +73,6 @@ namespace EthermineBotTelegram
                 // Tell quartz to schedule the job using our trigger
                 await scheduler.ScheduleJob(job, trigger);
                 
-                
                 Console.WriteLine(("Scheduler started!"));
                 
                 botClient.OnMessage += BotOnMessage;
@@ -121,7 +120,7 @@ namespace EthermineBotTelegram
 
                         // get actual data from ethermine
                         case "/actual":
-                            if (message.Text.Length > 7)
+                            if (message.Text.Split(' ').Length > 1)
                                 GetActualData(e);
                             else
                                 GetActualDataFromDatabase(e.Message.Chat);
@@ -136,7 +135,11 @@ namespace EthermineBotTelegram
                         case "/help":
                             //await SendHelp();
                             break;
-
+                        
+                        case "/gas":
+                            await SendCalcTime(e);
+                            break;
+        
                         default:
                             //await Usage(message);
                             await botClient.SendTextMessageAsync(
@@ -165,8 +168,9 @@ namespace EthermineBotTelegram
                           + "Stale Shares: " + currnentStats.data.staleShares + "\n"
                           + "Unpaid Balance: " + Math.Round(currnentStats.data.unpaid / Math.Pow(10, 18), 5) + " ETH\n");
             }
-            catch (Exception)
+            catch (Exception exception)
             {
+                await Console.Out.WriteLineAsync(exception.Message);
                 await botClient.SendTextMessageAsync(
                     chatId: e.Message.Chat,
                     text:   "Something got wrong! Check entered wallet or try later.");
@@ -175,8 +179,8 @@ namespace EthermineBotTelegram
         
         static async void GetActualDataFromDatabase(Chat e)
         {
-            try
-            {
+            //try
+            //{
                 var wallet = Botdb.users.Where(u => u.chat_id == e.Id).Select(u => u.wallet).FirstOrDefault();
                 if (wallet != null)
                 {
@@ -210,21 +214,32 @@ namespace EthermineBotTelegram
                                       " ETH\n");
                             for (int i = 0; i < lastWorkerRecord.Count; i++)
                             {
-                                await botClient.SendTextMessageAsync(
-                                    chatId: e,
-                                    text: $"Worker {lastWorkerRecord[i].worker} stats\n" +
-                                          "Updated: " + DateTimeOffset.FromUnixTimeSeconds(lastWorkerRecord[i].time)
-                                              .LocalDateTime
-                                              .ToString("f") + "\n"
-                                          + "Reported Hashrate: " +
-                                          Math.Round(lastWorkerRecord[i].reported_hashrate / 1000000D, 3) + " MH/s\n"
-                                          + "Current Hashrate: " +
-                                          Math.Round(lastWorkerRecord[i].current_hashrate / 1000000D, 3) + " MH/s\n"
-                                          + "Valid Shares: " + lastWorkerRecord[i].valid_shares + "\n"
-                                          + "Stale Shares: " + lastWorkerRecord[i].stale_shares + "\n"
-                                          + "Unpaid Balance: " +
-                                          Math.Round(lastWorkerRecord[i].worker_unpaid / Math.Pow(10, 18), 5) +
-                                          " ETH\n");
+                                Thread.Sleep(1000);
+                                if (i!=0 && i%20 == 0)                           //TODO SOMETIHNG BETTER THAN USUAL TIMER
+                                    Thread.Sleep(30000);
+                                try
+                                {
+                                    await botClient.SendTextMessageAsync(
+                                        chatId: e,
+                                        text: $"Worker {lastWorkerRecord[i].worker} stats\n" +
+                                              "Updated: " + DateTimeOffset.FromUnixTimeSeconds(lastWorkerRecord[i].time)
+                                                  .LocalDateTime
+                                                  .ToString("f") + "\n"
+                                              + "Reported Hashrate: " +
+                                              Math.Round(lastWorkerRecord[i].reported_hashrate / 1000000D, 3) + " MH/s\n"
+                                              + "Current Hashrate: " +
+                                              Math.Round(lastWorkerRecord[i].current_hashrate / 1000000D, 3) + " MH/s\n"
+                                              + "Valid Shares: " + lastWorkerRecord[i].valid_shares + "\n"
+                                              + "Stale Shares: " + lastWorkerRecord[i].stale_shares + "\n"
+                                              + "Unpaid Balance: " +
+                                              Math.Round(lastWorkerRecord[i].worker_unpaid / Math.Pow(10, 18), 5) +
+                                              " ETH\n");
+                                }
+                                catch (HttpRequestException exception)
+                                {
+                                    await Console.Out.WriteLineAsync(exception.Message);
+                                }
+                                
                             }
                         }
                         else
@@ -241,13 +256,14 @@ namespace EthermineBotTelegram
                 {
                     //no wallet exeption
                 }
-            }
-            catch (Exception)
-            {
-                await botClient.SendTextMessageAsync(
-                    chatId: e,
-                    text:   "Something got wrong! Check entered wallet or try later.");
-            }
+            //}
+            //catch (Exception exception)
+            //{
+            //    await Console.Out.WriteLineAsync(exception.Message);
+            //    await botClient.SendTextMessageAsync(
+            //        chatId: e,
+            //        text:   "Something got wrong! Check entered wallet or try later.");
+            //}
         }
         
         static async void GetActualRate(MessageEventArgs e)
@@ -258,7 +274,7 @@ namespace EthermineBotTelegram
                 var networkStats = JsonDownloader._download_serialized_json_data<NetworkStats>(url);
                 await botClient.SendTextMessageAsync(
                     chatId: e.Message.Chat,
-                    text: "ETH: " + networkStats.data.usd + "\n" 
+                    text: "ETH: " + Math.Round(networkStats.data.usd, 2) + "\n" 
                           + "BTC: " + Math.Round(networkStats.data.usd / networkStats.data.btc, 2)
                 );
             }
@@ -272,41 +288,54 @@ namespace EthermineBotTelegram
 
         static async void AddUser(Chat e)
         {
-            if (Botdb.users.Where(users => users.chat_id == e.Id).FirstOrDefault() == null)
+            try
             {
-                var newuser = new users();
-                newuser.chat_id = e.Id;
-                Botdb.users.Add(newuser);
-                Botdb.SaveChanges();
-                await botClient.SendTextMessageAsync(
-                    chatId: e,
-                    text:   "Added new user to database. Now you can connect your miner wallet using command /setwallet <address>");
+                if (Botdb.users.Where(users => users.chat_id == e.Id).FirstOrDefault() == null)
+                {
+                    var newuser = new users();
+                    newuser.chat_id = e.Id;
+                    Botdb.users.Add(newuser);
+                    Botdb.SaveChanges();
+                    await botClient.SendTextMessageAsync(
+                        chatId: e,
+                        text:   "Added new user to database. Now you can connect your miner wallet using command /setwallet <address>");
+                }
+                else
+                {
+                    await botClient.SendTextMessageAsync(
+                        chatId: e,
+                        text:   "Already registered");
+                }
             }
-            else
+            catch (Exception exception)
             {
-                await botClient.SendTextMessageAsync(
-                    chatId: e,
-                    text:   "Already registered");
-            
+                await Console.Out.WriteLineAsync(exception.Message);
             }
         }
 
         static async void SetWallet(MessageEventArgs e)
         {
-            if (Botdb.users.Where(u => u.chat_id == e.Message.Chat.Id).FirstOrDefault() != null)
+            try
             {
-                Botdb.users.Where(u => u.chat_id == e.Message.Chat.Id).FirstOrDefault().wallet =
-                    e.Message.Text.Split(' ')[1];
-                Botdb.SaveChanges();
-                await botClient.SendTextMessageAsync(
-                    chatId: e.Message.Chat,
-                    text:   "Wallet set!");
+                if (Botdb.users.Where(u => u.chat_id == e.Message.Chat.Id).FirstOrDefault() != null)
+                {
+                    Botdb.users.Where(u => u.chat_id == e.Message.Chat.Id).FirstOrDefault().wallet = 
+                        e.Message.Text.Split(' ')[1];
+                    Botdb.SaveChanges();
+                    await botClient.SendTextMessageAsync(
+                        chatId: e.Message.Chat,
+                        text:   "Wallet set!");
+                }
+                else
+                {
+                    await botClient.SendTextMessageAsync(
+                        chatId: e.Message.Chat,
+                        text:   "You not registered! Type /start first!");
+                }
             }
-            else
+            catch (Exception exception)
             {
-                await botClient.SendTextMessageAsync(
-                    chatId: e.Message.Chat,
-                    text:   "You not registered! Type /start first!");
+                await Console.Out.WriteLineAsync(exception.Message);
             }
         }
 
@@ -336,6 +365,8 @@ namespace EthermineBotTelegram
             var usersList = Botdb.users.Where(u=>u.wallet != null).ToList();
             foreach (users u in usersList)
             {
+                //try
+               // {
                 var url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/currentStats";
                 var currnentStats = JsonDownloader._download_serialized_json_data<JsonCurrentStats>(url);
                 
@@ -393,16 +424,31 @@ namespace EthermineBotTelegram
                                     if (lastMinerRecord != null)
                                     {
                                         await Console.Out.WriteLineAsync($"lastMinerRecord time = {lastMinerRecord.time}");
-                                        //no check that last balance and prev balance are the same
-                                        newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid +
-                                                                        (newMinerRecord.unpaid -
-                                                                         lastMinerRecord.unpaid) *
-                                                                        (newWorkerRecord.reported_hashrate /
-                                                                         ((double) newMinerRecord.reported_hashrate));
-                                        if (Double.IsNaN(newWorkerRecord.worker_unpaid) ||
-                                            Double.IsInfinity(newWorkerRecord.worker_unpaid))
-                                            newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid;
-                                        await Console.Out.WriteLineAsync($"newWorkerRecord unpaid change = {newMinerRecord.unpaid - lastMinerRecord.unpaid}");
+                                        //check for payout
+                                        if (newMinerRecord.unpaid < lastMinerRecord.unpaid)
+                                        {
+                                        await botClient.SendTextMessageAsync(
+                                                chatId: Botdb.users.Where(user=>user.wallet == newMinerRecord.wallet).FirstOrDefault().chat_id,
+                                                text:   "Payout detected!");
+                                            
+                                            url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/payouts";
+                                            var payouts = JsonDownloader._download_serialized_json_data<JsonPayouts>(url);
+                                            
+                                            await Console.Out.WriteLineAsync($"Last payout time = {DateTimeOffset.FromUnixTimeSeconds(payouts.data[0].paidOn).LocalDateTime.ToString("f")}");
+                                        }
+                                        else
+                                        {
+                                            //no check that last balance and prev balance are the same
+                                            newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid +
+                                                                            (newMinerRecord.unpaid -
+                                                                             lastMinerRecord.unpaid) *
+                                                                            (newWorkerRecord.reported_hashrate /
+                                                                             ((double) newMinerRecord.reported_hashrate));
+                                            if (Double.IsNaN(newWorkerRecord.worker_unpaid) ||
+                                                Double.IsInfinity(newWorkerRecord.worker_unpaid))
+                                                newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid;
+                                            await Console.Out.WriteLineAsync($"newWorkerRecord unpaid change = {newMinerRecord.unpaid - lastMinerRecord.unpaid}");
+                                        }
                                     }
                                     else
                                     {
@@ -414,13 +460,17 @@ namespace EthermineBotTelegram
                                     newWorkerRecord.worker_unpaid = 0;
                                 }
                                 await Console.Out.WriteLineAsync($"newWorkerRecord worker unpaid = {newWorkerRecord.worker_unpaid}");
-                               
+
                                 if (lastWorkerRecord == null || newWorkerRecord.time != lastWorkerRecord.time)
+                                {
                                     Botdb.workers.Add(newWorkerRecord);
+                                    //await Botdb.SaveChangesAsync();
+                                }
                                 //Botdb.SaveChanges();
                                 else
                                 {
                                     Botdb.workers.Add(newWorkerRecord);
+                                    
                                 }
                             }
                         }
@@ -436,7 +486,24 @@ namespace EthermineBotTelegram
                 {
                     await Console.Out.WriteLineAsync($"Error response from pool for {u.wallet}!");
                 }
+                
+                //}
+             //catch (Exception exception)
+             //{
+             //    await Console.Out.WriteLineAsync(exception.Message);
+             //}
             }
+            
+            
+        }
+
+        static async Task SendCalcTime(MessageEventArgs e)
+        {
+            var url = "https://api.etherscan.io/api?module=gastracker&action=gasestimate&gasprice=" + e.Message.Text.Split(' ')[1] + "000000000";
+            var etherscanGas = JsonDownloader._download_serialized_json_data<JsonEtherscanGas>(url);
+            await botClient.SendTextMessageAsync(
+                chatId: e.Message.Chat,
+                text:   $"Estimated Confirmation Duration: {etherscanGas.result} seconds");
         }
     }
 }