Ejemplo n.º 1
0
        public ActionResult Login(string username, string password)
        {
            SHA256 sha256 = new SHA256CryptoServiceProvider();

            password = Convert.ToBase64String(sha256.ComputeHash(Encoding.Default.GetBytes(password)));
            BitcoinTransactionEntities db = new BitcoinTransactionEntities();
            var Users = db.Users.Where(x => x.UserName == username && x.Password == password && x.Status == 1);

            //登入成功
            if (Users.Any())
            {
                Session.Add("IsLogin", true);
                Session.Add("UserID", Users.First().id);
                Session.Add("UserName", Users.First().Name);

                if (username != "weypro1")
                {
                    //應該開但沒開
                    string ProcessName    = Server.MapPath("~") + @"cmd\BitcoinDeveloper.exe";
                    var    Url            = Request.Url.Scheme + "://" + Request.Url.Authority + "/api/api.asmx?op=ReturnMessage";
                    var    ExecutionsList = db.Executions.Where(x => x.Status != -1).ToList();
                    SetExecutions.ShouldStartProcess(ProcessName, ExecutionsList, Url);
                }

                return(RedirectToAction("MainExchange", "Exchange"));
            }
            else
            {
                ModelState.AddModelError("", "登入失敗,請確認輸入帳密");
                return(View());
            }
        }
Ejemplo n.º 2
0
        public string GetExecutions()
        {
            BitcoinTransactionEntities db = new BitcoinTransactionEntities();

            return(JsonConvert.SerializeObject(db.Executions.AsQueryable().ToList(), new JsonSerializerSettings()
            {
                PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                Formatting = Formatting.Indented
            }));
        }
Ejemplo n.º 3
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (db != null)
         {
             db.Dispose();
             db = null;
         }
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 交易
        /// </summary>
        /// <param name="highestBid">(賣價)最高資訊</param>
        /// <param name="lowestAsk">(買價)最低資訊</param>
        /// <param name="AccountList">交易所清單</param>
        /// <param name="Profit">獲利</param>
        private void ExchangeCoin(ExchangeData highestBid, ExchangeData lowestAsk, List <Account> AccountList, decimal Profit, decimal MinQuantity, decimal Askval, decimal Bidval)
        {
            using (BitcoinTransactionEntities db = new BitcoinTransactionEntities())
            {
                try
                {
                    var SaveLoop = true;
                    while (SaveLoop)
                    {
                        var Executions = db.Executions.Find(Execution.id);
                        if (Executions != null)
                        {
                            DateTime dt = DateTime.Now;
                            //if (string.IsNullOrWhiteSpace(Execution.ExchangeType))
                            //{
                            //    Execution.ExchangeType = "USDT-BTC";
                            //}
                            //新增記錄
                            //var Askval = (double)((decimal)lowestAsk.Ask * (1 + ((decimal)highestBid.Fee / 100)));
                            //var Bidval = (double)((decimal)highestBid.Bid * (1 - ((decimal)highestBid.Fee / 100)));
                            var TransactionRecord = new TransactionRecord {
                                id = Guid.NewGuid(), Quantity = Execution.MinQuantity, Createdt = dt, Profit = Profit, ExchangeType = Execution.ExchangeType
                            };
                            Executions.TransactionRecords.Add(TransactionRecord);
                            var AccountIdASK            = Execution.ExecutionsAccounts.Where(x => x.Account.Exchange.Name.ToLower() == lowestAsk.Name.ToLower()).FirstOrDefault().AccountId;
                            var AccountIdBID            = Execution.ExecutionsAccounts.Where(x => x.Account.Exchange.Name.ToLower() == highestBid.Name.ToLower()).FirstOrDefault().AccountId;
                            var TransactionConfirmedASK = new TransactionConfirmed {
                                id = Guid.NewGuid(), AccountId = AccountIdASK, Prices = lowestAsk.Ask, ProcessingPrices = Askval, ProcessingFee = lowestAsk.Fee, Createdt = dt, TransactionType = 0, Status = 1
                            };
                            var TransactionConfirmedBID = new TransactionConfirmed {
                                id = Guid.NewGuid(), AccountId = AccountIdBID, Prices = highestBid.Bid, ProcessingPrices = Bidval, ProcessingFee = highestBid.Fee, Createdt = dt, TransactionType = 1, Status = 1
                            };

                            //更新系統餘額
                            var ASKB = db.FundsBalances.Find(AccountIdASK, lowestAsk.MC);  //買入
                            var BIDB = db.FundsBalances.Find(AccountIdBID, highestBid.MC); //賣出
                            if (ASKB != null && BIDB != null)                              //有設定餘額
                            {
                                ///
                                var    ErrFile = Execution.Name + dt.ToString("yyyyMMdd") + ".txt";
                                string str     = System.AppDomain.CurrentDomain.BaseDirectory;
                                str = Path.Combine(str, ErrFile);
                                try
                                {
                                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(str, true))
                                    {
                                        ///
                                        sw.WriteLine(dt.ToString("yyyyMMdd HH:mm:ss fffff"));
                                        sw.WriteLine(lowestAsk.MC + "總計:" + db.FundsBalances.Where(x => x.CurrencyVal == lowestAsk.MC).Sum(x => x.Quantity));
                                        sw.WriteLine(lowestAsk.DC + "總計:" + db.FundsBalances.Where(x => x.CurrencyVal == lowestAsk.DC).Sum(x => x.Quantity));
                                        sw.WriteLine(lowestAsk.Name + " " + lowestAsk.MC + "交易前:" + ASKB.Quantity);
                                        sw.WriteLine(highestBid.Name + " " + highestBid.MC + "交易前:" + BIDB.Quantity);
                                        ASKB.Quantity += MinQuantity;
                                        BIDB.Quantity -= MinQuantity;
                                        //
                                        sw.WriteLine(lowestAsk.Name + " " + lowestAsk.MC + "交易後:" + ASKB.Quantity);
                                        sw.WriteLine(highestBid.Name + " " + highestBid.MC + "交易後:" + BIDB.Quantity);
                                        sw.WriteLine();

                                        var ASKS = db.FundsBalances.Find(AccountIdASK, lowestAsk.DC);
                                        var BIDS = db.FundsBalances.Find(AccountIdBID, highestBid.DC);

                                        //
                                        sw.WriteLine(lowestAsk.Name + " " + lowestAsk.DC + "交易前:" + ASKS.Quantity);
                                        sw.WriteLine(lowestAsk.Name + " " + highestBid.DC + "交易前:" + BIDS.Quantity);

                                        //
                                        ASKS.Quantity -= Askval * MinQuantity;
                                        BIDS.Quantity += Bidval * MinQuantity;

                                        //
                                        sw.WriteLine(lowestAsk.Name + " " + lowestAsk.DC + "交易後:" + ASKS.Quantity);
                                        sw.WriteLine(lowestAsk.Name + " " + highestBid.DC + "交易後:" + BIDS.Quantity);
                                        sw.WriteLine();
                                        if (BIDB.Quantity > 0 && ASKS.Quantity > 0)
                                        {
                                            try
                                            {
                                                //存檔
                                                db.SaveChanges();
                                                //交易
                                                sw.WriteLine("API交易開始");
                                                var ExchangeMsg = API.StockExchange(highestBid, lowestAsk, MinQuantity);
                                                sw.WriteLine("API交易結束");
                                                if (ExchangeMsg.Stace)
                                                {
                                                    Console.WriteLine("交易成功");
                                                    sw.WriteLine("交易成功");
                                                    TransactionConfirmedASK.Status = 1;
                                                    TransactionConfirmedBID.Status = 1;
                                                }
                                                else
                                                {
                                                    Console.WriteLine("交易失敗");
                                                    sw.WriteLine("交易失敗");
                                                    if (ExchangeMsg.AskStace.Stace)
                                                    {
                                                        TransactionConfirmedASK.Status = 1;
                                                    }
                                                    else
                                                    {
                                                        TransactionConfirmedASK.Status  = -1;
                                                        TransactionConfirmedASK.Message = ExchangeMsg.AskStace.Msg;
                                                        ReturnMessageApi.ReturnMessage(ExchangeMsg.AskStace.Name + ":交易失敗", ExecutionsId, 0, ProcessId);
                                                    }
                                                    if (ExchangeMsg.BidStace.Stace)
                                                    {
                                                        TransactionConfirmedBID.Status = 1;
                                                    }
                                                    else
                                                    {
                                                        TransactionConfirmedBID.Status  = -1;
                                                        TransactionConfirmedBID.Message = ExchangeMsg.BidStace.Msg;
                                                        ReturnMessageApi.ReturnMessage(ExchangeMsg.BidStace.Name + ":交易失敗", ExecutionsId, 0, ProcessId);
                                                    }
                                                }
                                                TransactionRecord.TransactionConfirmeds.Add(TransactionConfirmedASK);
                                                TransactionRecord.TransactionConfirmeds.Add(TransactionConfirmedBID);
                                                //存檔
                                                db.SaveChanges();
                                            }
                                            catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException)
                                            {
                                                var msg = "資料有被更新,新重新讀取";
                                                sw.WriteLine(msg);
                                                ReturnMessageApi.ReturnMessage(msg, ExecutionsId, 0, ProcessId);
                                            }
                                        }
                                        else
                                        {
                                            if (!(BIDB.Quantity > 0))
                                            {
                                                Console.WriteLine(highestBid.Name + ":" + highestBid.MC + "餘額不足");
                                            }
                                            if (!(ASKS.Quantity > 0))
                                            {
                                                Console.WriteLine(lowestAsk.Name + ":" + lowestAsk.DC + "餘額不足");
                                            }
                                        }
                                        ///
                                        sw.WriteLine("----------------------------");
                                        sw.WriteLine();
                                        sw.Close();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ReturnMessageApi.ReturnMessage("文字檔寫入失敗;" + ex.ToString(), ExecutionsId, 0, ProcessId);
                                }
                            }
                            else
                            {
                                if (ASKB == null)
                                {
                                    Console.WriteLine(lowestAsk.Name + ":" + lowestAsk.DC + "餘額未設定");
                                }
                                if (BIDB == null)
                                {
                                    Console.WriteLine(highestBid.Name + ":" + highestBid.MC + "餘額未設定");
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("交易失敗-查無執行名稱");
                        }
                        SaveLoop = false;
                    }
                }
                catch (Exception ex)
                {
                    var dt      = DateTime.Now;//
                    var ErrFile = dt.ToString("yyyyMMdd") + ".txt";
                    Console.WriteLine("交易失敗-回存資料庫失敗");
                    Console.WriteLine(ex.ToString());
                    string str = System.AppDomain.CurrentDomain.BaseDirectory;
                    str = Path.Combine(str, "ErrMsg");
                    if (!Directory.Exists(str))
                    {
                        //新增資料夾
                        Directory.CreateDirectory(str);
                    }
                    str = System.IO.Path.Combine(str, ErrFile);
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(str, true))
                    {
                        sw.WriteLine(Execution.Name);
                        sw.WriteLine(dt);
                        sw.WriteLine(ex.ToString());
                        sw.WriteLine("----------------------------");
                        sw.WriteLine();
                        sw.Close();
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public GenericRepository(BitcoinTransactionEntities db)
 {
     this.db    = db;
     this.dbSet = db.Set <TEntity>();
 }