Esempio n. 1
0
        public async Task <ActionResult <TaskResult> > CancelOrder(string orderid, string accountid, string auth)
        {
            // Account logic first
            Entity account = await Entity.FindAsync(accountid);

            if (account == null)
            {
                return(new TaskResult(false, "Failed to find account " + accountid));
            }

            User authUser = await _context.Users.AsQueryable().FirstOrDefaultAsync(u => u.Api_Key == auth);

            if (authUser == null)
            {
                return(new TaskResult(false, "Failed to find auth account."));
            }

            StockOffer stockOffer = await _context.StockOffers.FindAsync(orderid);

            if (stockOffer == null)
            {
                return(new TaskResult(false, "Failed to find offer " + orderid));
            }

            // Authority check
            if (!await account.HasPermissionAsync(authUser, "eco"))
            {
                return(new TaskResult(false, "You do not have permission to trade for this entity!"));
            }

            if (stockOffer.Order_Type == "BUY")
            {
                _context.StockOffers.Remove(stockOffer);

                await _context.SaveChangesAsync();

                // Refund user and delete offer
                decimal refund = stockOffer.Amount * stockOffer.Target;

                TransactionRequest transaction = new TransactionRequest(EconomyManager.VooperiaID, account.Id, refund, $"Stock buy cancellation: {stockOffer.Amount} {stockOffer.Ticker}@{stockOffer.Target}", ApplicableTax.None, true);
                TaskResult         transResult = await transaction.Execute();

                if (!transResult.Succeeded)
                {
                    return(transResult);
                }
            }
            else if (stockOffer.Order_Type == "SELL")
            {
                _context.StockOffers.Remove(stockOffer);

                await _context.SaveChangesAsync();

                // Refund user stock and delete offer
                await ExchangeManager.AddStock(stockOffer.Ticker, stockOffer.Amount, accountid, _context);
            }

            string json = JsonConvert.SerializeObject(stockOffer);

            await ExchangeHub.Current.Clients.All.SendAsync("StockOfferCancel", json);

            return(new TaskResult(true, "Successfully removed order."));
        }