Exemplo n.º 1
0
        private void AddTransactionNotifications(DbCryptoContext db, DateTime startTime, DateTime endTime)
        {
            var groupedTransfers = db.Transfers.Where(t => startTime <= t.Date && endTime > t.Date).GroupBy(t => new { t.Token, t.OutgoingAddress }).Select(g => new {
                Token    = g.Key.Token,
                Wallet   = g.Key.OutgoingAddress,
                Value    = g.Sum(x => x.Value),
                UsdValue = g.Sum(x => x.UsdValue)
            });

            foreach (var gt in groupedTransfers)
            {
                // get token
                Token t = db.Tokens.First(tkn => tkn.Contract == gt.Token);

                if (GetTokenNotificationValue(t) <= gt.Value)
                {
                    if (db.Notifications.Where(n => gt.Wallet == n.LinkedWallet.Address && n.DateTime.AddHours(24) > ConvertTime(DateTime.Now) && gt.Value <= n.Value * 2).Count() != 0)
                    {
                        continue;
                    }

                    Wallet w = db.Wallets.SingleOrDefault(wal => wal.Address == gt.Wallet);
                    if (w == null)
                    {
                        w = new Wallet()
                        {
                            Address = gt.Wallet
                        };
                        w.Name = GetWalletName(w.Address);
                        db.Wallets.Add(w);
                        db.SaveChanges();
                    }

                    // Send SMS for big transaction
                    if ((gt.Value / t.TotalSupply) > 0.01)
                    {
                        SendSMS("Trascation for token " + t.Name + " - " + String.Format("{0:#,##0}", gt.UsdValue) + "$");
                    }

                    db.Notifications.Add(new Notification()
                    {
                        Action       = Actions.BigDailySum,
                        LinkedWallet = w,
                        Description  = "[" + t.Name + "]. Transfer summ per day: " + String.Format("{0:#,##0}", gt.Value) +
                                       " (" + String.Format("{0:#,##0}", gt.UsdValue) + "$)" + " on the wallet: " + gt.Wallet,
                        DateTime        = ConvertTime(DateTime.Now),
                        Value           = gt.Value,
                        PercentOfSupply = gt.Value / t.TotalSupply * 100,
                        USDValue        = gt.UsdValue
                    });
                    db.SaveChanges();
                }
            }
        }
Exemplo n.º 2
0
        private void AddNewTokenNotifications(DbCryptoContext db, List <WatchedToken> wt)
        {
            // Удаление нетребующих уведомлений обменников
            List <Exchange> exchangeToNotNotify  = db.Exchange.Where(ex => !ex.NotifyOnNextUpdate).ToList();
            List <Exchange> changeExchangeStatus = new List <Exchange>();

            exchangeToNotNotify.ForEach(etnn => {
                if (wt.Where(wte => wte.Exchange.ID == etnn.ID).Count() > 0)
                {
                    changeExchangeStatus.Add(etnn);
                }
            });

            changeExchangeStatus.ForEach(ces => wt.RemoveAll(t => t.Exchange.ID == ces.ID));

            db.Exchange.Where(ex => changeExchangeStatus.Where(ces => ces.ID == ex.ID).Count() > 0).ToList().ForEach(ex => ex.NotifyOnNextUpdate = true);
            db.SaveChanges();

            var groupedTokens = wt.GroupBy(t => new { t.Exchange }).Select(g => new
            {
                Exchange       = g.Key.Exchange,
                NumberOfTokens = g.Count()
            });

            /*
             * foreach (var gt in groupedTokens)
             * {
             *  if (gt.NumberOfTokens == 1)
             *      SendSMS(wt.First(watchedToken => watchedToken.Exchange.ID == gt.Exchange.ID).Name + " was added to " + gt.Exchange.Title);
             *  else
             *      SendSMS(gt.NumberOfTokens + " new tokens was added to " + gt.Exchange.Title);
             * }
             */

            foreach (WatchedToken token in wt)
            {
                Wallet w = db.Wallets.FirstOrDefault(wal => wal.Address == token.Comment);
                db.Notifications.Add(new Notification()
                {
                    Action       = Actions.NewToken,
                    LinkedWallet = w,
                    Description  = "[" + token.Name + "]. New token on excange " + token.Exchange.Title + " . Wallet address: " + token.Comment,
                    DateTime     = ConvertTime(DateTime.Now),
                });
                db.SaveChanges();
            }
        }
Exemplo n.º 3
0
        public void Update(DbCryptoContext db, ILogger _logger)
        {
            logger = _logger;
            logger.LogDebug("Parser.Update starting...");

            List <Token> tokens = db.Tokens.ToList();

            foreach (Token t in tokens)
            {
                if (!t.IsObserved)
                {
                    continue;
                }

                // Update Total Supply
                double nts = UpdateTotalSupply(t.Contract, t.Decimals);
                if (nts != -1)
                {
                    t.TotalSupply = Convert.ToUInt64(nts);
                    db.SaveChanges();
                }

                double price = 0;
                try
                {
                    price = TokenParser.GetPriceByContract(t.Contract, _logger);
                }
                catch (Exception e)
                {
                    continue;
                }

                // Add new trascations
                Transfer lastTransfer = db.Transfers.Where(ft => ft.Token == t.Contract).OrderBy(ft => ft.Date).LastOrDefault();
                DateTime startTime    = lastTransfer == null?ConvertTime(DateTime.Now.AddHours(-24)) : lastTransfer.Date.AddMinutes(-20);

                List <Transfer> transfers = GetTransactionsForToken(t, t.Decimals, startTime, ConvertTime(DateTime.Now), price);

                AddTransactions(db, t, transfers);
            }

            // Add new notifications
            AddTransactionNotifications(db, ConvertTime(DateTime.Now.AddHours(-24)), ConvertTime(DateTime.Now));

            logger.LogDebug("Exchange Update start...");
            List <WatchedToken> wt = ExchangeUpdater.Update(db);

            logger.LogDebug("Exchange Update:" + wt.Count + " new tokens");

            logger.LogDebug("New Token Notifications start...");
            AddNewTokenNotifications(db, wt);
            logger.LogDebug("Add New Token Notifications finished");

            logger.LogDebug("New Token Tweet Notifications updating...");
            TwitterParser.Update(db);
            logger.LogDebug("Add New Token Tweet Notifications finished");

            logger.LogDebug("Parser.Update finished");
        }
Exemplo n.º 4
0
        public static void RemoveAllWatchedTokens(DbCryptoContext db, int excId)
        {
            var wt = db.WatchedTokens.Where(w => w.Exchange.ID == excId).ToList();

            db.WatchedTokens.RemoveRange(wt);
            Exchange ex = db.Exchange.First(e => e.ID == excId);

            ex.NotifyOnNextUpdate = false;
            db.SaveChanges();
        }
Exemplo n.º 5
0
        private void AddTransactions(DbCryptoContext db, Token t, List <Transfer> transfers)
        {
            int i = 0;

            foreach (Transfer tr in transfers)
            {
                if (db.Transfers.Where(trans => trans.Token == t.Contract && tr.TransactionHash == trans.TransactionHash).Count() == 0)
                {
                    db.Transfers.Add(tr);
                    i++;
                }
            }
            db.SaveChanges();

            logger.LogDebug("Token: " + t.Name + ". Added " + i + " transfers");
        }
Exemplo n.º 6
0
        public static void Update(DbCryptoContext db)
        {
            List <TwitterAccount> cachAccs = db.TwitterAccount.ToList();

            foreach (TwitterAccount ta in cachAccs)
            {
                DateTime currentTime = DateTime.Now;

                string resResponse;
                System.Threading.Thread.Sleep(3000);

                using (WebClient wc = new WebClient())
                {
                    resResponse = wc.DownloadString("https://twitter.com/" + ta.Name);
                }

                // get content
                List <Twitt> tweets = new List <Twitt>();
                string       rs     = resResponse;

                while (true)
                {
                    string tag = GetStringByTag(rs, "<div class=\"js-tweet-text-container\">", "</p>");
                    if (tag != "")
                    {
                        int ind = rs.IndexOf("<div class=\"js-tweet-text-container\">");
                        // cut inner tag : <p class="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text" lang="tl" data-aria-label-part="0">
                        rs = rs.Substring(ind + 20);
                        HtmlDocument htmlDoc = new HtmlDocument();
                        htmlDoc.LoadHtml(tag);
                        string result = htmlDoc.DocumentNode.InnerText;

                        Twitt t = new Twitt()
                        {
                            content = result.ToLower()
                        };
                        tweets.Add(t);
                    }
                    else
                    {
                        break;
                    }
                }

                rs = resResponse;
                int index = 0;
                while (true)
                {
                    string tag = GetStringByTag(rs, "_timestamp js-short-timestamp", "</span>", false);
                    if (tag != "")
                    {
                        int ind      = rs.IndexOf("_timestamp js-short-timestamp");
                        int localind = tag.IndexOf("data-time=\"") + "data-time=\"".Length;

                        rs = rs.Substring(ind + 50);

                        tweets[index].dateTime = epoch.AddSeconds(long.Parse(GetUntilOrEmpty(tag.Substring(localind), "\"")));
                        index++;
                    }
                    else
                    {
                        break;
                    }
                }

                List <Twitt> newTwitts = tweets.Where(t => t.dateTime > ta.LastUpdated).ToList();

                Wallet w = db.Wallets.FirstOrDefault();

                //Get filter words
                List <string>       filter = ta.Template.Split(new string[] { "^^" }, StringSplitOptions.None).ToList();
                List <Notification> nots   = new List <Notification>();
                newTwitts.ForEach(t => {
                    if (filter.Any(t.content.Contains))
                    {
                        nots.Add(new Notification()
                        {
                            Action       = Actions.NewTweet,
                            DateTime     = t.dateTime,
                            Description  = "New tweet from " + ta.Name + ". " + t.content,
                            LinkedWallet = w
                        });
                        //       Call(nots.Last().Description, "79997135966"); 79250813700
                        Call(nots.Last().Description, "79250813700");
                    }
                });

                nots.ForEach(d => db.Notifications.Add(d));
                db.SaveChanges();

                ta.LastUpdated = currentTime;
                db.SaveChanges();
            }
        }
Exemplo n.º 7
0
 private void SetUsdPriceToZero(DbCryptoContext db)
 {
     db.Transfers.Where(t => t.UsdValue != 0).ToList().ForEach(t => t.UsdValue = 0);
     db.SaveChanges();
 }
Exemplo n.º 8
0
        public static List <WatchedToken> Update(DbCryptoContext db)
        {
            List <WatchedToken> res           = new List <WatchedToken>();
            List <WatchedToken> resTwoEntries = new List <WatchedToken>();

            List <WatchedToken> counterTokens = new List <WatchedToken>();

            foreach (Exchange ex in db.Exchange)
            {
                List <WatchedToken> wt = db.WatchedTokens.Where(w => w.Exchange.ID == ex.ID).ToList();

                List <Wallet> wallets = db.Wallets.Where(w => w.Exchange.ID == ex.ID).ToList();
                if (wallets.Count == 0)
                {
                    continue;
                }

                foreach (Wallet w in wallets)
                {
                    BuildURLForWalletBalance(w.Address);
                    List <WatchedToken> responsedTokens = GetWalletTokens(w.Address);

                    foreach (WatchedToken token in responsedTokens)
                    {
                        if (wt.FirstOrDefault(t => t.Name == token.Name) == null)
                        {
                            WatchedToken watchToken = new WatchedToken()
                            {
                                DateTime  = token.DateTime,
                                Exchange  = ex,
                                Name      = token.Name,
                                ShortName = token.ShortName,
                                Comment   = w.Address
                            };

                            res.Add(watchToken);
                        }
                        else
                        {
                            if (wt.FirstOrDefault(t => t.Name == token.Name) != null && counterTokens.Where(t => t.Name == token.Name).Count() == 0 &&
                                res.Where(t => t.Name == token.Name).Count() == 0)
                            {
                                counterTokens.Add(wt.FirstOrDefault(t => t.Name == token.Name));
                            }
                        }
                    }

                    wt = db.WatchedTokens.Where(wtc => wtc.Exchange.ID == ex.ID).ToList();
                }

                foreach (WatchedToken token in counterTokens)
                {
                    WatchedToken tfu = db.WatchedTokens.Where(wtc => wtc.Exchange.ID == ex.ID).FirstOrDefault(t => t.Name == token.Name);

                    if (tfu != null)
                    {
                        tfu.Counter++;
                        db.SaveChanges();
                        if (tfu.Counter == 2)
                        {
                            resTwoEntries.Add(tfu);
                        }
                    }
                }
            }

            db.WatchedTokens.RemoveRange(db.WatchedTokens.Where(t => t.Counter == 0));
            db.SaveChanges();
            res.ForEach(rt => db.WatchedTokens.Add(rt));
            db.SaveChanges();

            //тут фильтровать, если дохуя возвращается новых токенов
            return(resTwoEntries);
        }