示例#1
0
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     using (var _UserRepo = new UsuarioRepository())
     {
         var user = _UserRepo.GetByEmail(filterContext.HttpContext.User.Identity.Name);
         if (!string.IsNullOrEmpty(user.BinanceAPIKey) && !string.IsNullOrWhiteSpace(user.BinanceAPISecret) && user.IsValidBinanceKeys)
         {
             var apicontent = BinanceRestApi.GetAccountInformation(user.BinanceAPIKey, user.BinanceAPISecret);
             if (apicontent.IsSuccessStatusCode)
             {
                 var result            = apicontent.Content.ReadAsStringAsync().Result;
                 var AccounInformation = JsonConvert.DeserializeObject <Account_Information>(result);
                 filterContext.Controller.ViewBag.BTC            = AccounInformation.balances.Where(x => x.asset == "BTC").Select(x => x.free).FirstOrDefault();
                 filterContext.Controller.ViewBag.datavencimento = user.DataVencimentoLicenca.ToString("dd/MM/yyyy");
                 filterContext.Controller.ViewBag.username       = user.Nome;
             }
             else
             {
                 filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { area = "usuario", controller = "Errors", action = "BinanceApiError" }));
             }
         }
         else
         {
             filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { area = "usuario", controller = "binance", action = "setkeys" }));
         }
     }
 }
 public HttpResponseMessage TestKeys(SetKeysViewModel ViewModel)
 {
     try
     {
         if (ViewModel == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Campos Inválidos", "text/plain"));
         }
         if (ModelState.IsValid)
         {
             var BinanceResult = BinanceRestApi.GetAccountInformation(ViewModel.binanceKey, ViewModel.binanceSecret);
             if (BinanceResult.IsSuccessStatusCode)
             {
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
             else
             {
                 var BinanceerrorObj = Helper.GetBinanceErrorObj(BinanceResult);
                 return(Request.CreateResponse(HttpStatusCode.BadRequest, BinanceerrorObj));
             }
         }
         var modelstateError = ModelStateErrors.DisplayModelStateError(ModelState);
         return(Request.CreateResponse(HttpStatusCode.NotAcceptable, modelstateError, "text/plain"));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
        public async Task <ActionResult> Limites()
        {
            var user = _UserRepo.GetByEmail(User.Identity.Name);

            ViewBag.datavencimento = user.DataVencimentoLicenca.ToString("dd/MM/yyyy");
            ViewBag.username       = user.Nome;
            var apicontent = BinanceRestApi.GetAccountInformation(user.BinanceAPIKey, user.BinanceAPISecret);

            if (apicontent.IsSuccessStatusCode)
            {
                var result = await apicontent.Content.ReadAsStringAsync();

                var AccounInformation = JsonConvert.DeserializeObject <Account_Information>(result);
                ViewBag.BTC = AccounInformation.balances.Where(x => x.asset == "BTC").Select(x => x.free).FirstOrDefault();
                var symbolsContent = BinanceRestApi.SymbolsPriece();
                if (symbolsContent.IsSuccessStatusCode)
                {
                    var symbolResult = await symbolsContent.Content.ReadAsStringAsync();

                    var symbolsInformation = JsonConvert.DeserializeObject <List <SymbolTicker> >(symbolResult);

                    var viewmodel = new LimitesViewModel
                    {
                        balances      = AccounInformation.balances.Where(x => x.free != 0 || x.locked != 0).OrderByDescending(x => x.asset == "BTC").ThenByDescending(x => x.free).ToList(),
                        symbolTickers = symbolsInformation
                    };
                    return(View(viewmodel));
                }
            }
            return(RedirectToAction("BinanceApiError", "Errors", new { area = "Usuario" }));
        }
 public HttpResponseMessage GetSaldoBTC()
 {
     try
     {
         var userId        = int.Parse(Helper.GetJWTPayloadValue(Request, "id"));
         var user          = _UserRepo.GetById(userId);
         var BinanceResult = BinanceRestApi.GetAccountInformation(user.BinanceAPIKey, user.BinanceAPISecret);
         if (BinanceResult.IsSuccessStatusCode)
         {
             var result            = BinanceResult.Content.ReadAsStringAsync().Result;
             var AccounInformation = JsonConvert.DeserializeObject <Account_Information>(result);
             var saldo             = AccounInformation.balances.Where(x => x.asset == "BTC").Select(x => x.free).FirstOrDefault();
             return(Request.CreateResponse(HttpStatusCode.OK, saldo));
         }
         var BinanceerrorObj = Helper.GetBinanceErrorObj(BinanceResult);
         return(Request.CreateResponse(HttpStatusCode.BadRequest, BinanceerrorObj));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
 /// <summary>
 /// Atualiza as Chaves da api binance
 /// </summary>
 /// <param name="ViewModel"></param>
 /// <returns></returns>
 public HttpResponseMessage Post([FromBody] SetKeysViewModel ViewModel)
 {
     try
     {
         if (ViewModel == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Campos Inválidos", "text/plain"));
         }
         if (ModelState.IsValid)
         {
             var userId        = int.Parse(Helper.GetJWTPayloadValue(Request, "id"));
             var user          = _UserRepo.GetById(userId);
             var BinanceResult = BinanceRestApi.GetAccountInformation(ViewModel.binanceKey, ViewModel.binanceSecret);
             if (BinanceResult.IsSuccessStatusCode)
             {
                 user.BinanceAPIKey      = ViewModel.binanceKey;
                 user.BinanceAPISecret   = ViewModel.binanceSecret;
                 user.IsValidBinanceKeys = true;
                 _UserRepo.Update(user);
                 var monitor = WSMonitor.Instancia;
                 monitor.RemoveMonitor(userId);
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
             else
             {
                 user.IsValidBinanceKeys = false;
                 _UserRepo.Update(user);
                 var BinanceerrorObj = Helper.GetBinanceErrorObj(BinanceResult);
                 return(Request.CreateResponse(HttpStatusCode.BadRequest, BinanceerrorObj));
             }
         }
         var modelstateError = ModelStateErrors.DisplayModelStateError(ModelState);
         return(Request.CreateResponse(HttpStatusCode.NotAcceptable, modelstateError, "text/plain"));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }