public async Task spUpdate_BTC_USDT_OpenOrder(BTC_USDT_OpenOrderTableModel model, bool isBuy) { if (isBuy) { await _db.ExecuteAsync( "Update_BTC_USDT_OpenOrder_Buy", new { userid = model.CreateUserId, price = model.Price, amount = model.Amount, total = model.Total, id = model.Id }, commandType : CommandType.StoredProcedure); } else { await _db.ExecuteAsync( "Update_BTC_USDT_OpenOrder_Sell", new { userid = model.CreateUserId, price = model.Price, amount = model.Amount, total = model.Total, id = model.Id }, commandType : CommandType.StoredProcedure); } }
public async Task <spProcess_BTC_USDT_OrderResult> spProcess_BTC_USDT_Order( BTC_USDT_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); if (isBuy) { return(await _db.QueryFirstAsync <spProcess_BTC_USDT_OrderResult>( $"Process_BTC_USDT_BuyOrder", p, commandType : CommandType.StoredProcedure)); } else { return(await _db.QueryFirstAsync <spProcess_BTC_USDT_OrderResult>( $"Process_BTC_USDT_SellOrder", p, commandType : CommandType.StoredProcedure)); } } catch (Exception ex) { return(await spProcess_BTC_USDT_Order(openOrder, isBuy)); } }
public async Task <ActionResult> CreateOrder([FromBody] OrderModel orderModel) { 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; if (orderModel.Pair == "BTCUSDT") { //Buy - USDT if (orderModel.IsBuy) { var wallet = await _walletsRepository.GetUserWalletAsync(userId, "USDT"); 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, "BTC"); 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); } BTC_USDT_OpenOrderTableModel order = new BTC_USDT_OpenOrderTableModel { Price = priceDecimal, Amount = amountDecimal, Total = total, CreateUserId = userId, CreateDate = DateTime.Now, }; var result = await _tradeRepository.spProcess_BTC_USDT_Order(order, orderModel.IsBuy); while (result.Amount != order.Amount && result.Amount != 0) { order.Amount = result.Amount; result = await _tradeRepository.spProcess_BTC_USDT_Order(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())); }