Exemple #1
0
        public async Task <ActionResult <TaskResult> > SubmitStockSell(string ticker, int count, decimal price, 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."));
            }

            StockDefinition stockDef = await _context.StockDefinitions.FindAsync(ticker.ToUpper());

            if (stockDef == null)
            {
                return(new TaskResult(false, "Failed to find stock with ticker " + ticker));
            }

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

            if (count < 1)
            {
                return(new TaskResult(false, "You must sell a positive number of stocks!"));
            }

            // At this point the account is authorized //
            // Find currently owned stock
            var owned = await _context.StockObjects.AsQueryable().FirstOrDefaultAsync(s => s.Owner_Id == accountid && s.Ticker == ticker);

            if (owned == null || count > owned.Amount)
            {
                return(new TaskResult(false, "You don't own that many stocks!"));
            }

            if (price < 0)
            {
                return(new TaskResult(false, "Negatives are not allowed!"));
            }

            if (price == 0)
            {
                StockOffer highOffer = await ExchangeManager.GetHighestBuyOffer(ticker, _context);

                if (highOffer != null)
                {
                    price = highOffer.Target;
                }
                else
                {
                    return(new TaskResult(false, "There is no market rate!"));
                }
            }

            decimal totalPrice = count * price;

            // Perform transaction
            owned.Amount -= count;

            // If amount hit 0, remove the object
            if (owned.Amount == 0)
            {
                _context.StockObjects.Remove(owned);
            }
            else
            {
                _context.StockObjects.Update(owned);
            }

            // Create offer
            StockOffer sellOffer = new StockOffer()
            {
                Id         = Guid.NewGuid().ToString(),
                Amount     = count,
                Order_Type = "SELL",
                Owner_Name = account.Name,
                Owner_Id   = account.Id,
                Target     = price.Round(),
                Ticker     = ticker
            };

            _context.StockOffers.Add(sellOffer);
            await _context.SaveChangesAsync();

            string json = JsonConvert.SerializeObject(sellOffer);

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

            return(new TaskResult(true, $"Successfully posted SELL order {sellOffer.Id}"));
        }