Beispiel #1
0
        public async Task <IActionResult> Create(CreateDCAViewModel viewModel)
        {
            var services = await GetServices();

            viewModel.ExchangeServices = new SelectList(services, nameof(ExternalServiceData.Id),
                                                        nameof(ExternalServiceData.Name));
            if (viewModel.FiatAmount <= 0)
            {
                ModelState.AddModelError(nameof(viewModel.FiatAmount), "Amount needs to be more than 0.");
            }

            if (ModelState.IsValid)
            {
                var serviceData =
                    await _externalServiceManager.GetExternalServiceData(viewModel.SelectedExchangeServiceId, GetUserId());

                var exchangeService = new ExchangeService(serviceData);
                var symbols         = (await(await exchangeService.ConstructClient()).GetMarketSymbolsAsync()).ToArray();
                if (!symbols.Contains(viewModel.MarketSymbol))
                {
                    viewModel.AddModelError(nameof(viewModel.MarketSymbol), $"The market symbols you entered is invalid. Please choose from the following: {string.Join(",", symbols)}", ModelState);
                }
            }
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            return(await SetItUp(viewModel));
        }
Beispiel #2
0
        private async Task <IActionResult> SetItUp(CreateDCAViewModel vm)
        {
            var presetName = $"Generated_DCA";

            var recipe = new Recipe()
            {
                Name        = presetName,
                Description = "Generated from a preset",
                UserId      = _userManager.GetUserId(User),
                Enabled     = false
            };
            await _recipeManager.AddOrUpdateRecipe(recipe);

            var recipeTrigger = new RecipeTrigger()
            {
                TriggerId = new TimerTrigger().Id,
                RecipeId  = recipe.Id
            };

            recipeTrigger.Set(new TimerTriggerParameters()
            {
                StartOn            = vm.StartOn,
                TriggerEvery       = vm.TriggerEvery,
                TriggerEveryAmount = vm.TriggerEveryAmount
            });
            await _recipeManager.AddOrUpdateRecipeTrigger(recipeTrigger);

            var recipeActionGroup = new RecipeActionGroup()
            {
                RecipeId = recipe.Id
            };

            await _recipeManager.AddRecipeActionGroup(recipeActionGroup);

            var tradeAction = new RecipeAction()
            {
                RecipeId            = recipe.Id,
                RecipeActionGroupId = recipeActionGroup.Id,
                ActionId            = new PlaceOrderDataActionHandler().ActionId,
                ExternalServiceId   = vm.SelectedExchangeServiceId,
                Order    = 0,
                DataJson = JsonConvert.SerializeObject(new PlaceOrderData()
                {
                    Amount       = vm.FiatAmount.ToString(CultureInfo.InvariantCulture),
                    IsBuy        = vm.IsBuy,
                    MarketSymbol = vm.MarketSymbol,
                    OrderType    = OrderType.Market
                })
            };

            await _recipeManager.AddOrUpdateRecipeAction(tradeAction);


            return(RedirectToAction("EditRecipe", "Recipes", new
            {
                id = recipe.Id,
                statusMessage =
                    "Preset generated. Recipe is currently disabled for now. Please verify details are correct before enabling!"
            }));
        }