public async Task <spProcessOrderResult> ProcessOrder( OpenOrderTableModel openOrder, bool isBuy) { try { var p = new DynamicParameters(); p.Add("createUserId", openOrder.CreateUserId); p.Add("price", openOrder.Price); p.Add("amount", openOrder.Amount); p.Add("total", openOrder.Total); p.Add("createDate", openOrder.CreateDate); return(await _db.QueryFirstAsync <spProcessOrderResult>( $"Process_{openOrder.CryptExchangePair.ToUpper()}_{(isBuy ? "Buy" : "Sell")}Order", p, commandType : CommandType.StoredProcedure)); } catch (Exception ex) { return(await ProcessOrder(openOrder, isBuy)); } }
public async Task <ActionResult> CryptoCreateOrder([FromBody] OrderModel orderModel) { if (orderModel == null) { return(BadRequest("orderModel is null.")); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (!string.IsNullOrEmpty(orderModel.BotAuthCode)) { var botAuthCode = await _botsRepository.GetBotByBotAuthCode(orderModel.BotAuthCode); if (botAuthCode != null) { userId = botAuthCode.UserId; } } if (string.IsNullOrEmpty(userId)) { return(BadRequest("You're not authorized")); } decimal priceDecimal = orderModel.Price.ParseToDecimal(); decimal amountDecimal = orderModel.Amount.ParseToDecimal(); decimal total = priceDecimal * amountDecimal; decimal updatedWalletBalance = 0; var pair = await _pairsRepository.GetPairByAcronimAsync(orderModel.Pair); if (pair == null) { return(BadRequest("Wrong pair.")); } string firstCurrency = pair.Currency1; string secondCurrency = pair.Currency2; if (orderModel.IsBuy) { var wallet = await _walletsRepository.GetUserWalletAsync(userId, secondCurrency); if (wallet == null) { return(BadRequest("You dont have a wallets. Create them")); } if (wallet.Value < total) { return(BadRequest("You dont have wallet balance.")); } var updatedWallet = wallet; updatedWallet.Value -= total; updatedWalletBalance = updatedWallet.Value; await _walletsRepository.UpdateWalletBalanceAsync(updatedWallet); } else //Sell - BTC { var wallet = await _walletsRepository.GetUserWalletAsync(userId, firstCurrency); if (wallet == null) { return(BadRequest("You dont have a wallets. Create them")); } if (wallet.Value < amountDecimal) { return(BadRequest("You dont have wallet balance.")); } var updatedWallet = wallet; updatedWallet.Value -= amountDecimal; updatedWalletBalance = updatedWallet.Value; await _walletsRepository.UpdateWalletBalanceAsync(updatedWallet); } OpenOrderTableModel order = new OpenOrderTableModel { Price = priceDecimal, Amount = amountDecimal, Total = total, CreateUserId = userId, CreateDate = DateTime.Now, CryptExchangePair = pair.SQLTableName }; var result = await _tradeRepository.ProcessOrder(order, orderModel.IsBuy); while (result.Amount != order.Amount && result.Amount != 0) { order.Amount = result.Amount; result = await _tradeRepository.ProcessOrder(order, orderModel.IsBuy); if (result.ClosedOrderUserId != "-1") { await _hubcontext.Clients.User(result.ClosedOrderUserId).SendAsync("OrderWasClosed", JsonConvert.SerializeObject(result.ClosedOrderId)); } } order.Id = result.Id; if (result.Id == -1) { await _hubcontext.Clients.User(userId).SendAsync("OrderWasClosed", result.Id); } else { await _hubcontext.Clients.User(userId).SendAsync("OrderWasCreated", JsonConvert.SerializeObject(order)); } return(Ok(updatedWalletBalance.ToString())); }