Пример #1
0
        /// <summary>
        /// Rewards the user.
        /// </summary>
        /// <param name="rewardItem">The reward item.</param>
        private async Task RewardUser(RewardItem rewardItem)
        {
            try
            {
                var rewardUser = await GetRewardUser(rewardItem.RewardType);

                if (rewardUser == Guid.Empty)
                {
                    Log.Message(LogLevel.Info, "[RewardUser] - No new reward user fround for reward, Reward: {0}.",
                                rewardItem.RewardType);
                    return;
                }

                Log.Message(LogLevel.Info,
                            "[RewardUser] - Sending user reward transfer, User: {0}, Reward: {1}, CurrencyId: {2}, Amount: {3}", rewardUser,
                            rewardItem.RewardType, rewardItem.Currency, rewardItem.Amount);
                using (var tradeService = new TradeProcessorClient())
                    using (var context = ExchangeDataContextFactory.CreateContext())
                    {
                        var response = await tradeService.SubmitTransferAsync(new SubmitTransferRequest
                        {
                            UserTo              = rewardUser,
                            UserId              = Constant.SYSTEM_USER_REWARD,
                            TransferType        = TransferType.Reward,
                            CurrencyId          = rewardItem.Currency,
                            Amount              = rewardItem.Amount,
                            NotificationTitle   = "Coin Reward!",
                            NotificationMessage = rewardItem.Message
                        });

                        if (!string.IsNullOrEmpty(response.Error))
                        {
                            Log.Message(LogLevel.Error, "[RewardUser] - TradeProcessorClient faled to process transfer, Error: {0}",
                                        response.Error);
                            return;
                        }

                        context.Reward.Add(new Reward
                        {
                            UserId     = rewardUser,
                            Amount     = rewardItem.Amount,
                            CurrencyId = rewardItem.Currency,
                            RewardType = rewardItem.RewardType.ToString(),
                            Message    = rewardItem.Message,
                            TimeStamp  = DateTime.UtcNow,
                            Percent    = rewardItem.Percent
                        });
                        await context.SaveChangesAsync();

                        Log.Message(LogLevel.Info, "[RewardUser] - Sending user reward transfer complete.");
                    }
            }
            catch (Exception ex)
            {
                Log.Exception("[RewardUser] - An exception occured paying user reward", ex);
            }
        }
Пример #2
0
        private async Task PayoutUsers(IEnumerable <PrizePayout> payouts)
        {
            Log.Message(LogLevel.Info, "[PayoutUsers] - Processing user payouts.");
            if (payouts.IsNullOrEmpty())
            {
                Log.Message(LogLevel.Info, "[PayoutUsers] - No users found to payout.");
                return;
            }

            foreach (var payout in payouts)
            {
                try
                {
                    Log.Message(LogLevel.Info, "[PayoutUsers] - Processing user payout, UserId: {0}, CurrncyId: {1}, Amount: {2}", payout.UserId, payout.CurrencyId, payout.Amount);
                    using (var tradeService = new TradeProcessorClient())
                    {
                        var result = await tradeService.SubmitTransferAsync(new SubmitTransferRequest
                        {
                            Amount       = payout.Amount,
                            CurrencyId   = payout.CurrencyId,
                            IsApi        = false,
                            TransferType = TransferType.Lotto,
                            UserId       = Constant.SYSTEM_USER_LOTTO,
                            UserTo       = payout.UserId
                        });

                        if (!string.IsNullOrEmpty(result.Error))
                        {
                            Log.Message(LogLevel.Error, "[PayoutUsers] - Failed to payout user, Error: {0}", result.Error);
                            continue;
                        }
                    }
                    Log.Message(LogLevel.Info, "[PayoutUsers] -  Processing user payout complete, UserId: {0}", payout.UserId);
                }
                catch (Exception ex)
                {
                    Log.Exception("[PayoutUsers] - An exception occured processing payout.", ex);
                }
            }

            Log.Message(LogLevel.Info, "[PayoutUsers] - User payouts complete.");
        }
        private async Task ProcessReferralPayouts(int roundId)
        {
            try
            {
                var referralsToProcess = new List <int>();
                using (var context = ExchangeDataContextFactory.CreateContext())
                {
                    Log.Message(LogLevel.Info, $"[ProcessReferralPayouts] - Processing payouts for round {roundId}");
                    var referrals = await context.ReferralInfo
                                    .Where(x => x.RoundId == roundId && x.Status == ReferralStatus.Active && x.TransferId == 0)
                                    .ToListNoLockAsync();

                    if (referrals.Any())
                    {
                        foreach (var referralInfo in referrals)
                        {
                            referralsToProcess.Add(referralInfo.Id);
                            referralInfo.Status = ReferralStatus.Processing;
                        }
                        await context.SaveChangesAsync();
                    }
                }

                if (!referralsToProcess.Any())
                {
                    Log.Message(LogLevel.Info, "[ProcessReferralPayouts] - No referral payouts found.");
                    return;
                }

                Log.Message(LogLevel.Info, $"[ProcessReferralPayouts] - {referralsToProcess.Count} referrals found to process...");
                using (var tradeService = new TradeProcessorClient())
                    using (var context = ExchangeDataContextFactory.CreateContext())
                    {
                        foreach (var referralId in referralsToProcess)
                        {
                            var referralInfo = await context.ReferralInfo.FirstOrDefaultNoLockAsync(x => x.Id == referralId && x.Status == ReferralStatus.Processing && x.TransferId == 0);

                            if (referralInfo == null)
                            {
                                Log.Message(LogLevel.Info, $"[ProcessReferralPayouts] - No referral info found, Id: {referralId}");
                                continue;
                            }

                            var amount = referralInfo.TradeFeeAmount;
                            if (amount <= 0)
                            {
                                Log.Message(LogLevel.Info, $"[ProcessReferralPayouts] - No payout required for user, UserId: {referralInfo.UserId}");
                                referralInfo.LastUpdate = DateTime.UtcNow;
                                referralInfo.Status     = ReferralStatus.Complete;
                                continue;
                            }

                            Log.Message(LogLevel.Info, $"[ProcessReferralPayouts] - Processing referral transfer, UserId: {referralInfo.UserId}, Round: {referralInfo.RoundId}, Amount: {amount}");
                            var response = await tradeService.SubmitTransferAsync(new SubmitTransferRequest
                            {
                                UserTo              = referralInfo.UserId,
                                UserId              = Constant.SYSTEM_USER_REFERRAL,
                                TransferType        = TransferType.ReferralBonus,
                                CurrencyId          = Constant.DOTCOIN_ID,
                                Amount              = amount,
                                NotificationTitle   = "Referral Bonus!",
                                NotificationMessage = "New referral bonus payment received."
                            });

                            if (!string.IsNullOrEmpty(response.Error))
                            {
                                Log.Message(LogLevel.Error, $"[ProcessReferralPayouts] - Failed to transfer funds to user, Error: {response.Error}");
                                continue;
                            }

                            referralInfo.LastUpdate = DateTime.UtcNow;
                            referralInfo.Status     = ReferralStatus.Complete;
                            referralInfo.TransferId = response.TransferId;
                            Log.Message(LogLevel.Info, "[ProcessReferralPayouts] - Processing referral complete.");
                        }

                        Log.Message(LogLevel.Info, "[ProcessReferralPayouts] - Saving changes..");
                        await context.SaveChangesAsync();

                        Log.Message(LogLevel.Info, "[ProcessReferralPayouts] - Referral processing complete.");
                    }
            }
            catch (Exception ex)
            {
                Log.Exception("[ProcessReferralPayouts] - An exception occurred processing Referral items.", ex);
            }
        }