示例#1
0
        public async Task <WithdrawHistory> GetWithdrawHistory(DateTime startDate)
        {
            var toReturn = new WithdrawHistory {
                Success = true, WithdrawList = new List <Withdraw>()
            };

            while (true)
            {
                if (startDate > DateTime.Today)
                {
                    break;
                }

                DateTime endDate    = startDate.AddDays(90);
                var      parameters = $"startTime={startDate.ToUnixDateTime()}&endTime={endDate.ToUnixDateTime()}";
                parameters = _restClient.AddTimestampAndSign(parameters);
                var result = await _restClient.CallAsync(HttpMethod.Get, "/wapi/v3/withdrawHistory.html", parameters);

                var resultTyped = JsonConvert.DeserializeObject <WithdrawHistory>(result);

                if (!resultTyped.Success)
                {
                    throw new Exception();
                }

                toReturn.WithdrawList.AddRange(resultTyped.WithdrawList ?? Enumerable.Empty <Withdraw>());
                startDate = endDate.AddDays(1);
            }

            return(toReturn);
        }
示例#2
0
        public async Task <Event> Withdraw(WithdrawHistory obj)
        {
            var bidderId = (await _bllUsers.GetByUsernameAsync(obj.Username)).Id;
            var objEvent = await _bllEvents.GetAsync(obj.EventId);

            var latestBid = await _bllBidHistories.GetLatestUserBid(bidderId, obj.AuctionId, obj.EventId);

            //if latest bidder isn't our bidder (or if there are no bids yet), no need to use our processor any further and cause some unknown catastrophic error.
            if (latestBid == null || bidderId != latestBid.UserId)
            {
                return(null);
            }
            var throwbackBidHistory = await _bllWithdraws.GetThrowbackAsync(new BO.WithdrawHistory()
            {
                UserId    = bidderId,
                AuctionId = obj.AuctionId,
                EventId   = obj.EventId
            });

            if (throwbackBidHistory.UserId == 0 || throwbackBidHistory.BidAmount > objEvent.CurrentPrice)
            {
                return(null);
            }
            //update throwback details ( topBidder and currentPrice for throwback bidder ) of event.
            var updatedEvent = new BO.Event()
            {
                CurrentPrice            = throwbackBidHistory.BidAmount,
                TopBidder               = throwbackBidHistory.UserId,
                Id                      = objEvent.Id,
                ItemId                  = objEvent.ItemId,
                AuctionId               = objEvent.AuctionId,
                StartDate               = objEvent.StartDate,
                EndDate                 = objEvent.EndDate,
                Lun                     = objEvent.Lun,
                Lud                     = objEvent.Lud,
                MinPriceIncrementAmount = objEvent.MinPriceIncrementAmount
            };

            //Register withdraw history
            var successWithdraw = await _bllWithdraws.AddAsync(new BO.WithdrawHistory()
            {
                AuctionId      = obj.AuctionId,
                EventId        = obj.EventId,
                UserId         = bidderId,
                WithdrawAmount = latestBid.BidAmount,
                WithdrawDate   = DateTime.Now
            });

            var successEvent = await _bllEvents.UpdateAsync(updatedEvent);

            return(Mapper.Event(updatedEvent));
        }
        /// <summary>
        /// Fetch withdraw history.
        /// </summary>
        /// <param name="asset">Asset you want to see the information for.</param>
        /// <param name="status">Withdraw status.</param>
        /// <param name="startTime">Start time. </param>
        /// <param name="endTime">End time.</param>
        /// <param name="recvWindow">Specific number of milliseconds the request is valid for.</param>
        /// <returns></returns>
        public async Task <WithdrawHistory> GetWithdrawHistory(string asset, WithdrawStatus?status = null, DateTime?startTime = null, DateTime?endTime = null, long recvWindow = 50000)
        {
            if (string.IsNullOrWhiteSpace(asset))
            {
                throw new ArgumentException("asset cannot be empty. ", "asset");
            }

            string args = $"asset={asset.ToUpper()}"
                          + (status.HasValue ? $"&status={(int)status}" : "")
                          + (startTime.HasValue ? $"&startTime={Utilities.GenerateTimeStamp(startTime.Value)}" : "")
                          + (endTime.HasValue ? $"&endTime={Utilities.GenerateTimeStamp(endTime.Value)}" : "")
                          + $"&recvWindow={recvWindow}";

            WithdrawHistory result = await _apiClient.CallAsync <WithdrawHistory>(ApiMethod.POST, EndPoints.WithdrawHistory, true, args);

            return(result);
        }
示例#4
0
        public void SaveWithdraws(WithdrawHistory withdrawHistory)
        {
            var withdrawJson = withdrawHistory.WithdrawList.OrderBy(t => t.ApplyTime).Select(JsonConvert.SerializeObject);

            File.WriteAllLines(GetFileName(_withdrawFileName), withdrawJson);
        }
        /// <summary>Withdraws given amount of money to specified address.</summary>
        /// <exception cref="CommandExecutionException">Thrown when user supplied invalid input data.</exception>
        public void Withdraw(IUser user, decimal amount, string address)
        {
            this.logger.Trace("({0}:{1},{2}:{3},{4}:'{5}')", nameof(user), user.Id, nameof(amount), amount, nameof(address), address);

            this.AssertAmountPositive(amount);

            if (amount < this.settings.MinWithdrawAmount)
            {
                this.logger.Trace("(-)[MIN_WITHDRAW_AMOUNT]");
                throw new CommandExecutionException($"Minimal withdraw amount is {this.settings.MinWithdrawAmount} {this.settings.Ticker}.");
            }

            using (BotDbContext context = this.contextFactory.CreateContext())
            {
                DiscordUserModel discordUser = this.GetOrCreateUser(context, user);

                // Don't allow withdrawals to deposit address.
                if (discordUser.DepositAddress != null && discordUser.DepositAddress == address)
                {
                    this.logger.Trace("(-)[WITHDRAWAL_TO_DEPOSIT_ADDR]");
                    throw new CommandExecutionException("You can't withdraw to your own deposit address!");
                }

                decimal amountToSend = amount - this.settings.NetworkFee;
                this.logger.Trace("(The amount after fee: {0})", amountToSend);

                this.AssertBalanceIsSufficient(discordUser, amountToSend);

                try
                {
                    var withdrawResult = this.nodeIntegration.Withdraw(amountToSend, address);

                    discordUser.Balance -= amount;

                    context.Update(discordUser);
                    context.SaveChanges();

                    var withdrawHistoryItem = new WithdrawHistory
                    {
                        DiscordUserId = discordUser.DiscordUserId,
                        Amount        = amountToSend,
                        ToAddress     = address,
                        TransactionId = withdrawResult.TransactionId,
                        WithdrawTime  = DateTime.UtcNow
                    };

                    context.Add(withdrawHistoryItem);
                    context.SaveChanges();

                    this.logger.Debug("User '{0}' withdrew {1} to address '{2}'. After fee subtracted '{3}'", discordUser, amount, address, amountToSend);
                }
                catch (InvalidAddressException)
                {
                    this.logger.Trace("(-)[INVALID_ADDRESS]");
                    throw new CommandExecutionException("Address specified is invalid.");
                }
                catch (Exception ex)
                {
                    this.logger.Trace("(-)[FAILED_WITHDRAW]");
                    this.logger.Debug($"(WITHDRAW_ERROR)[{ex.Message}]");
                    throw new CommandExecutionException($"Failed to withdraw. Contact {this.settings.Discord.SupportUsername}");
                }
            }

            this.logger.Trace("(-)");
        }