private async Task <WithdrawCurrencyModel> CreateWithdrawModel(Cryptopia.Entity.ApplicationUser user, WithdrawCurrencyModel model) { var currencyData = await CurrencyReader.GetCurrency(model.Symbol); var balanceData = await BalanceReader.GetCurrencyBalance(User.Identity.GetUserId(), currencyData.CurrencyId); var addressBook = await AddressBookReader.GetAddressBook(User.Identity.GetUserId(), currencyData.CurrencyId); var verificationData = await UserVerificationReader.GetVerificationStatus(User.Identity.GetUserId()); var estimatedCoinNzd = await BalanceEstimationService.GetNZDPerCoin(currencyData.CurrencyId); model = user.GetTwoFactorModel(TwoFactorComponent.Withdraw, model); model.Name = currencyData.Name; model.CurrencyId = currencyData.CurrencyId; model.CurrencyType = currencyData.Type; model.Symbol = balanceData.Symbol; model.Balance = balanceData.Available; model.Fee = currencyData.WithdrawFee; model.WithdrawFeeType = currencyData.WithdrawFeeType; model.MinWithdraw = currencyData.WithdrawMin; model.MaxWithdraw = currencyData.WithdrawMax; model.AddressBookOnly = !user.IsUnsafeWithdrawEnabled; model.AddressBook = addressBook; model.AddressType = currencyData.AddressType; model.HasWithdrawLimit = verificationData.Limit > 0; model.WithdrawLimit = verificationData.Limit; model.WithdrawTotal = verificationData.Current; model.EstimatedCoinNZD = estimatedCoinNzd; model.Instructions = currencyData.WithdrawInstructions; model.Message = currencyData.WithdrawMessage; model.MessageType = currencyData.WithdrawMessageType.ToString().ToLower(); model.Decimals = currencyData.CurrencyDecimals; return(model); }
public async Task <ActionResult> Create(WithdrawCurrencyModel model) { var currencyData = await CurrencyReader.GetCurrency(model.Symbol); var user = await UserManager.FindValidByIdAsync(User.Identity.GetUserId()); if (currencyData == null || user == null) { ViewBag.Message = $"Currency {model.Symbol} not found."; return(View("Error")); } if (currencyData.CurrencyId == Constant.NZDT_ID && !UserVerificationReader.IsVerified(user.VerificationLevel)) { return(View("NotVerified")); } // Verify Address var address = model.AddressData?.Trim(); if (string.IsNullOrEmpty(address)) { address = model.SelectedAddress; } else if (currencyData.AddressType != AddressType.Standard) { if (currencyData.AddressType == AddressType.PaymentId && !CurrencyExtensions.IsValidPaymentId(model.AddressData2?.Trim())) { ModelState.AddModelError("AddressData2", $"Please enter a valid Cryptonote PaymentId."); return(View(await CreateWithdrawModel(user, model))); } address = $"{model.AddressData?.Trim()}:{model.AddressData2?.Trim()}"; if (currencyData.Type == CurrencyType.Fiat) { address = address.TrimEnd(':'); } } if (string.IsNullOrEmpty(address)) { ModelState.AddModelError(user.IsUnsafeWithdrawEnabled ? "AddressData" : "AddressBookError", $"Please enter a valid {model.Symbol} address"); return(View(await CreateWithdrawModel(user, model))); } // Verify Amount var withdrawAmount = decimal.Round(model.Amount, currencyData.CurrencyDecimals); if (withdrawAmount < currencyData.WithdrawMin || withdrawAmount >= currencyData.WithdrawMax) { ModelState.AddModelError("Amount", $"Withdrawal amount must be between {currencyData.WithdrawMin} {currencyData.Symbol} and {currencyData.WithdrawMax} {currencyData.Symbol}"); return(View(await CreateWithdrawModel(user, model))); } // Verify Two Factor if (!await UserManager.VerifyUserTwoFactorCodeAsync(TwoFactorComponent.Withdraw, user.Id, model.Code1, model.Code2)) { ModelState.AddModelError("TwoFactorError", "The TFA code entered was incorrect or has expired, please try again."); return(View(await CreateWithdrawModel(user, model))); } // Create withdraw var twoFactortoken = await GenerateUserTwoFactorTokenAsync(TwoFactorTokenType.WithdrawConfirm, user.Id); var result = await WithdrawWriter.CreateWithdraw(User.Identity.GetUserId(), new CreateWithdrawModel { Address = address, Amount = withdrawAmount, CurrencyId = model.CurrencyId, TwoFactorToken = twoFactortoken, Type = WithdrawType.Normal, }); if (!result.Success) { ModelState.AddModelError("Error", result.Message); return(View(await CreateWithdrawModel(user, model))); } // Send Confirmation email if (!user.DisableWithdrawEmailConfirmation) { await SendConfirmationEmail(user, model.Symbol, withdrawAmount - model.Fee, address, result.Result, twoFactortoken, currencyData.AddressType); } return(RedirectToAction("Summary", new { withdrawId = result.Result, returnUrl = GetLocalReturnUrl(model.ReturnUrl) })); }