Exemple #1
0
        private async Task <IWriterResult <int> > ProcessWithdraw(CreateWithdraw model)
        {
            using (var context = DataContextFactory.CreateContext())
            {
                var currency = await context.Currency.FirstOrDefaultAsync(x => x.Id == model.CurrencyId);

                if (currency == null || !currency.IsEnabled || currency.Status != CurrencyStatus.OK)
                {
                    return(WriterResult <int> .ErrorResult("Currency not found or is currently disabled."));
                }

                var user = await context.Users.FirstOrDefaultAsync(x => x.Id == model.UserId);

                if (user == null || !user.IsWithdrawEnabled)
                {
                    return(WriterResult <int> .ErrorResult("Your withdrawals are currently disabled."));
                }

                var auditResult = await AuditService.AuditUserCurrency(context, model.UserId, model.CurrencyId);

                if (!auditResult.Success || model.Amount > auditResult.Avaliable)
                {
                    return(WriterResult <int> .ErrorResult("Insufficient funds."));
                }

                if (model.Amount < currency.MinWithdraw || model.Amount > currency.MaxWithdraw)
                {
                    return(WriterResult <int> .ErrorResult("Withdrawal amount must be between {0} and {1} {2}", currency.MinWithdraw, currency.MaxWithdraw, currency.Symbol));
                }

                var isValidAddress = await WalletService.ValidateAddress(model.Address, currency.WalletHost, currency.WalletPort, currency.WalletUser, currency.WalletPass);

                if (!isValidAddress)
                {
                    return(WriterResult <int> .ErrorResult($"Invalid {currency.Symbol} address."));
                }

                var newWithdraw = new Entity.Withdraw
                {
                    IsApi          = model.IsApi,
                    TimeStamp      = DateTime.UtcNow,
                    TwoFactorToken = model.ConfirmationToken,
                    Address        = model.Address,
                    CurrencyId     = model.CurrencyId,
                    Amount         = Math.Max(0, model.Amount),
                    Fee            = currency.WithdrawFee,
                    WithdrawType   = WithdrawType.Normal,
                    WithdrawStatus = model.IsApi
                                         ? WithdrawStatus.Pending
                                         : WithdrawStatus.Unconfirmed,
                    UserId = model.UserId
                };

                context.Withdraw.Add(newWithdraw);
                await context.SaveChangesAsync();

                await AuditService.AuditUserCurrency(context, model.UserId, model.CurrencyId);

                return(WriterResult <int> .SuccessResult(newWithdraw.Id));
            }
        }
Exemple #2
0
 public async Task <IWriterResult <int> > QueueWithdraw(CreateWithdraw withdraw)
 {
     return(await WithdrawProcessor.QueueItem(withdraw).ConfigureAwait(false));
 }