예제 #1
0
        public async Task <ActionResult <decimal> > GetStockBuyPrice(string ticker)
        {
            decimal    lowest   = 0;
            StockOffer lowOffer = await ExchangeManager.GetLowestSellOffer(ticker, _context);

            if (lowOffer != null)
            {
                lowest = lowOffer.Target;
            }

            if (lowest == 0)
            {
                return(NotFound($"No stocks with ticker {ticker} are for sale."));
            }

            return(lowest);
        }
예제 #2
0
        public async Task <ActionResult <TaskResult> > SubmitStockBuy(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!"));
            }

            // At this point the account is authorized //
            if (count < 1)
            {
                return(new TaskResult(false, "You must buy a positive number of stocks!"));
            }

            if (price == 0)
            {
                StockOffer lowOffer = await ExchangeManager.GetLowestSellOffer(ticker, _context);

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

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

            decimal totalPrice = count * price;

            if (totalPrice > account.Credits)
            {
                return(new TaskResult(false, "You cannot afford this!"));
            }

            TransactionRequest transaction = new TransactionRequest(account.Id, EconomyManager.VooperiaID, totalPrice, $"Stock purchase: {count} {ticker}@{price}", ApplicableTax.None, false);
            TaskResult         transResult = await transaction.Execute();

            if (!transResult.Succeeded)
            {
                return(transResult);
            }

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

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

            string json = JsonConvert.SerializeObject(buyOffer);

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

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