Пример #1
0
        public static async Task BonusPayout(BitcornContext dbContext, UserReferral userReferral, Referrer referrer, User user, User referrerUser,
                                             UserStat referrerStat)
        {
            if (userReferral != null &&
                userReferral.SignupReward != null &&
                userReferral.MinimumBalanceDate != null &&
                userReferral.WalletDownloadDate != null &&
                userReferral.SyncDate != null &&
                userReferral.Bonus == null &&
                userReferral.ReferrerBonus == null &&
                userReferral.UserSubscriptionId == null &&
                referrer != null &&
                !user.IsBanned &&
                !referrerUser.IsBanned)
            {
                var subQuery = SubscriptionUtils.GetActiveSubscription(dbContext, user, "BITCORNFarms", 1);
                if (subQuery == null)
                {
                    return;
                }

                var userSubInfo = await subQuery.FirstOrDefaultAsync();

                if (userSubInfo == null)
                {
                    return;
                }

                userReferral.UserSubscriptionId = userSubInfo.UserSubcriptionTierInfo.UserSubscription.UserSubscriptionId;

                var amount      = BONUS_PAYOUT;
                var bonusReward = await TxUtils.SendFromBitcornhub(user, amount, "BITCORNFarms", "Referral bonus reward", dbContext);

                if (IsValidReferrer(referrer))
                {
                    if (bonusReward)
                    {
                        userReferral.Bonus = DateTime.Now;
                        await LogReferralTx(dbContext, user.UserId, amount, "Recruit bonus reward");

                        var referrerBonusReward = await TxUtils.SendFromBitcornhub(referrerUser, amount, "BITCORNFarms", "Referral bonus reward", dbContext);

                        if (referrerBonusReward)
                        {
                            await UpdateYtdTotal(dbContext, referrer, amount);
                            await LogReferralTx(dbContext, referrerUser.UserId, amount, "Referral bonus reward");

                            referrerStat.TotalReferralRewardsCorn += amount;
                            referrerStat.TotalReferralRewardsUsdt += (amount * await ProbitApi.GetCornPriceAsync(dbContext));
                            userReferral.ReferrerBonus             = DateTime.Now;
                        }
                    }
                }
            }
        }
Пример #2
0
        public static async Task ReferralRewards(BitcornContext dbContext, WalletDownload walletDownload, UserReferral userReferral, User referrerUser,
                                                 User user, string type)
        {
            if (user.IsBanned || referrerUser.IsBanned)
            {
                return;
            }

            var referrer = await dbContext.Referrer
                           .FirstOrDefaultAsync(w => w.UserId == walletDownload.ReferralUserId);

            userReferral.WalletDownloadDate = DateTime.Now;

            if (IsValidReferrer(referrer))
            {
                var referralPayoutTotal = await ReferralUtils.TotalReward(dbContext, referrer) + await ReferralUtils.WalletBonusReward(dbContext, referrer, 10);;
                var referrerReward      = await TxUtils.SendFromBitcornhub(referrerUser, referralPayoutTotal, "BITCORNFarms",
                                                                           type, dbContext);

                var referreeReward = await TxUtils.SendFromBitcornhub(user, referrer.Amount + 10, "BITCORNFarms",
                                                                      type, dbContext);

                if (referrerReward && referreeReward)
                {
                    await ReferralUtils.LogReferralTx(dbContext, referrerUser.UserId, referralPayoutTotal,
                                                      type);

                    await ReferralUtils.LogReferralTx(dbContext, user.UserId, referrer.Amount + 10,
                                                      $"Recruit {type}");

                    await ReferralUtils.UpdateYtdTotal(dbContext, referrer, referralPayoutTotal);

                    if (referrerUser.UserStat.TotalReferralRewardsCorn == null)
                    {
                        referrerUser.UserStat.TotalReferralRewardsCorn = 0;
                    }
                    if (referrerUser.UserStat.TotalReferralRewardsUsdt == null)
                    {
                        referrerUser.UserStat.TotalReferralRewardsUsdt = 0;
                    }
                    referrerUser.UserStat.TotalReferralRewardsCorn += referralPayoutTotal;
                    referrerUser.UserStat.TotalReferralRewardsUsdt +=
                        ((referralPayoutTotal) * (await ProbitApi.GetCornPriceAsync(dbContext)));
                    userReferral.WalletDownloadDate = DateTime.Now;
                    await ReferralUtils.BonusPayout(dbContext, userReferral, referrer, user, referrerUser,
                                                    referrerUser.UserStat);
                }
            }
        }
Пример #3
0
        public async Task <ActionResult <FullUser> > RegisterNewUser([FromBody] Auth0User auth0User, [FromQuery] string referral = null)
        {
            if (this.GetCachedUser() != null)
            {
                throw new InvalidOperationException();
            }
            if (auth0User == null)
            {
                throw new ArgumentNullException();
            }
            if (!StaticLockCollection.Lock(auth0User.Auth0Id))
            {
                return(StatusCode(UserLockCollection.UserLockedReturnCode));
            }

            var existingUserIdentity = await _dbContext.Auth0Query(auth0User.Auth0Id).Select(u => u.UserIdentity).FirstOrDefaultAsync();

            if (existingUserIdentity?.Auth0Id == auth0User.Auth0Id)
            {
                var user       = _dbContext.User.FirstOrDefault(u => u.UserId == existingUserIdentity.UserId);
                var userWallet = _dbContext.UserWallet.FirstOrDefault(u => u.UserId == existingUserIdentity.UserId);
                var userStat   = _dbContext.UserStat.FirstOrDefault(u => u.UserId == existingUserIdentity.UserId);
                StaticLockCollection.Release(auth0User.Auth0Id);
                return(BitcornUtils.GetFullUser(user, existingUserIdentity, userWallet, userStat));
            }

            int referralId;

            try
            {
                referralId = Convert.ToInt32(referral);
            }
            catch (Exception e)
            {
                referralId = 0;
            }

            try
            {
                var user = CreateUser(auth0User, referralId);
                _dbContext.User.Add(user);
                if (referral != null && referralId != 0)
                {
                    var referrer = await _dbContext.Referrer.FirstOrDefaultAsync(r => r.ReferralId == referralId);

                    if (ReferralUtils.IsValidReferrer(referrer))
                    {
                        var referrerUser = await _dbContext.User.FirstOrDefaultAsync(u => u.UserId == referrer.UserId);

                        if (referrerUser != null && !referrerUser.IsBanned)
                        {
                            var referralPayoutTotal = await ReferralUtils.TotalReward(_dbContext, referrer);

                            var referrerRegistrationReward = await TxUtils.SendFromBitcornhub(referrerUser, referralPayoutTotal, "BITCORNFarms", "Registrations reward", _dbContext);

                            var userRegistrationReward = await TxUtils.SendFromBitcornhub(user, referrer.Amount, "BITCORNFarms", "Recruit registrations reward", _dbContext);

                            if (referrerRegistrationReward && userRegistrationReward)
                            {
                                await ReferralUtils.UpdateYtdTotal(_dbContext, referrer, referralPayoutTotal);

                                await ReferralUtils.LogReferralTx(_dbContext, referrer.UserId, referralPayoutTotal, "Registration reward");

                                await ReferralUtils.LogReferralTx(_dbContext, user.UserId, referrer.Amount, "Recruit registration reward");

                                var referrerStat = await _dbContext.UserStat.FirstOrDefaultAsync(s => s.UserId == referrer.UserId);

                                if (referrerStat != null)
                                {
                                    if (referrerStat.TotalReferrals == null)
                                    {
                                        referrerStat.TotalReferrals = 0;
                                    }
                                    referrerStat.TotalReferrals++;

                                    if (referrerStat.TotalReferralRewardsCorn == null)
                                    {
                                        referrerStat.TotalReferralRewardsCorn = 0;
                                    }
                                    referrerStat.TotalReferralRewardsCorn += referralPayoutTotal;

                                    if (referrerStat.TotalReferralRewardsUsdt == null)
                                    {
                                        referrerStat.TotalReferralRewardsUsdt = 0;
                                    }
                                    referrerStat.TotalReferralRewardsUsdt += (referralPayoutTotal * (await ProbitApi.GetCornPriceAsync(_dbContext)));
                                }
                                user.UserReferral.SignupReward = DateTime.Now;
                            }
                        }
                    }
                }

                await _dbContext.SaveAsync();

                return(BitcornUtils.GetFullUser(user, user.UserIdentity, user.UserWallet, user.UserStat));
            }
            catch (Exception e)
            {
                await BITCORNLogger.LogError(_dbContext, e, JsonConvert.SerializeObject(auth0User));

                throw e;
            }
            finally
            {
                StaticLockCollection.Release(auth0User.Auth0Id);
            }
        }
Пример #4
0
        public static async Task UpdateReferralSync(BitcornContext dbContext, PlatformId platformId)
        {
            var user = BitcornUtils.GetUserForPlatform(platformId, dbContext).FirstOrDefault();

            try
            {
                if (user != null && user.UserId != 0)
                {
                    if (user.IsBanned)
                    {
                        return;
                    }
                    if (!UserLockCollection.Lock(user.UserId))
                    {
                        throw new Exception("User is locked");
                    }

                    var userReferral = await dbContext.UserReferral.FirstOrDefaultAsync(u => u.UserId == user.UserId);

                    if (userReferral != null && userReferral.SyncDate == null)
                    {
                        var referrer = await
                                       dbContext.Referrer.FirstOrDefaultAsync(r => r.ReferralId == userReferral.ReferralId);

                        if (referrer != null && (referrer.YtdTotal < 600 || (referrer.ETag != null && referrer.Key != null)))
                        {
                            var referralPayoutTotal = await ReferralUtils.TotalReward(dbContext, referrer);

                            var referrerUser = await dbContext.User.FirstOrDefaultAsync(u => u.UserId == referrer.UserId);

                            if (referrerUser.IsBanned)
                            {
                                if (user != null)
                                {
                                    UserLockCollection.Release(user.UserId);
                                }
                                return;
                            }
                            var referreeReward = await TxUtils.SendFromBitcornhub(user, referrer.Amount, "BITCORNfarms", "Recruit social sync", dbContext);

                            var referrerReward = await TxUtils.SendFromBitcornhub(referrerUser, referralPayoutTotal, "BITCORNfarms", "Social sync", dbContext);

                            if (referrerReward && referreeReward)
                            {
                                userReferral.SyncDate = DateTime.Now;
                                await UpdateYtdTotal(dbContext, referrer, referralPayoutTotal);
                                await LogReferralTx(dbContext, referrer.UserId, referralPayoutTotal, "Social Sync");
                                await LogReferralTx(dbContext, user.UserId, referralPayoutTotal, "Recruit social Sync");

                                var userStat = await dbContext.UserStat.FirstOrDefaultAsync(s => s.UserId == referrer.UserId);

                                userStat.TotalReferralRewardsCorn += referralPayoutTotal;
                                userStat.TotalReferralRewardsUsdt += (referralPayoutTotal * (await ProbitApi.GetCornPriceAsync(dbContext)));
                                await ReferralUtils.BonusPayout(dbContext, userReferral, referrer, user, referrerUser, userStat);
                            }
                        }
                    }

                    await dbContext.SaveAsync();
                }
            }
            catch (Exception e)
            {
                await BITCORNLogger.LogError(dbContext, e, null);

                throw;
            }
            finally
            {
                if (user != null)
                {
                    UserLockCollection.Release(user.UserId);
                }
            }
        }
        public async Task <UserWallet> Wallet([FromRoute] string id)
        {
            var platformId = BitcornUtils.GetPlatformId(id);
            var user       = await BitcornUtils.GetUserForPlatform(platformId, _dbContext).FirstOrDefaultAsync();

            if (user != null)
            {
                var userWallet = user.UserWallet;
                if (!UserLockCollection.Lock(userWallet.UserId))
                {
                    return(userWallet);
                }
                try
                {
                    var referralId = _dbContext.UserReferral.FirstOrDefault(r => r.UserId == userWallet.UserId)?.ReferralId;
                    if (referralId != 0 && referralId != null)
                    {
                        var userReferral = await _dbContext.UserReferral.FirstOrDefaultAsync(r => r.UserId == userWallet.UserId);

                        var referrer = await _dbContext.Referrer.FirstOrDefaultAsync(r => r.ReferralId == userReferral.ReferralId);

                        var referrerUser = await _dbContext.JoinUserModels().FirstOrDefaultAsync(w => w.UserId == referrer.UserId);

                        var referrerStat = await _dbContext.UserStat.FirstOrDefaultAsync(s => s.UserId == referrer.UserId);

                        await ReferralUtils.BonusPayout(_dbContext, userReferral, referrer, user, referrerUser, referrerStat);

                        if (referrer != null &&
                            userReferral != null &&
                            userReferral.MinimumBalanceDate == null &&
                            userWallet.Balance >= MIN_BALANCE_QUEST_AMOUNT)
                        {
                            if (referrer.YtdTotal < 600 || (referrer.ETag != null && referrer.Key != null))
                            {
                                var referralPayoutTotal = await ReferralUtils.TotalReward(_dbContext, referrer);

                                var miniumBalanceRewardUser = await TxUtils.SendFromBitcornhub(user, referrer.Amount, "BITCORNFarms", "Minium balance reward", _dbContext);

                                var miniumBalanceReward = await TxUtils.SendFromBitcornhub(referrerUser, referralPayoutTotal, "BITCORNFarms", "Minium balance reward", _dbContext);

                                if (miniumBalanceReward && miniumBalanceRewardUser)
                                {
                                    referrerStat.TotalReferralRewardsCorn += referralPayoutTotal;
                                    referrerStat.TotalReferralRewardsUsdt += (referralPayoutTotal * (await ProbitApi.GetCornPriceAsync(_dbContext)));
                                    userReferral.MinimumBalanceDate        = DateTime.Now;
                                    await ReferralUtils.UpdateYtdTotal(_dbContext, referrer, referralPayoutTotal);

                                    await ReferralUtils.LogReferralTx(_dbContext, user.UserId, referrer.Amount, "Recruit minimum balance Reward");

                                    await ReferralUtils.LogReferralTx(_dbContext, referrerUser.UserId, referralPayoutTotal, "Minimum balance Reward");

                                    await ReferralUtils.BonusPayout(_dbContext, userReferral, referrer, user, referrerUser, referrerStat);
                                }
                            }
                        }
                        await _dbContext.SaveAsync();
                    }
                }
                catch (Exception e)
                {
                    await BITCORNLogger.LogError(_dbContext, e, id);
                }
                finally
                {
                    UserLockCollection.Release(userWallet.UserId);
                }
                return(user.UserWallet);
            }
            else
            {
                return(null);
            }
        }