Ejemplo n.º 1
0
        public static async Task TestWalletDownloadWithReferrerInternal(BitcornContext dbContext, string ip, User testUser, bool shouldCompleteBonus)
        {
            TestUtils.GetBalances(testUser, out decimal? downloadStartBalance, out decimal? referralStartBalance);
            var userReferral = dbContext.UserReferral.FirstOrDefault(u => u.UserId == testUser.UserId);
            var referrer     = dbContext.Referrer.FirstOrDefault(u => u.ReferralId == userReferral.ReferralId);
            var controller   = new WalletDownloadController(dbContext);
            var res          = await controller.Download(TestUtils.CreateDownload(ip, DateTime.Now, testUser, referrer.UserId), DateTime.Now);

            Assert.Equal(200, (res as StatusCodeResult).StatusCode);

            var dbContext2 = TestUtils.CreateDatabase();

            try
            {
                var referralPayoutTotal = await ReferralUtils.TotalReward(dbContext, referrer) + await ReferralUtils.WalletBonusReward(dbContext, referrer, 10);;

                TestUtils.GetBalances(testUser, out decimal? downloadEndBalance, out decimal? referralEndBalance);
                decimal bonus = 0;
                if (shouldCompleteBonus)
                {
                    bonus = ReferralUtils.BONUS_PAYOUT;
                }
                Assert.Equal(referralStartBalance + referralPayoutTotal + bonus, referralEndBalance);
                Assert.Equal(downloadStartBalance + 10 + referrer.Amount + bonus, downloadEndBalance);
            }
            finally
            {
                dbContext2.Dispose();
            }
        }
Ejemplo n.º 2
0
        public static async Task RegisterNewUserWithReferralArgs(string testName, string testId, int referralId)
        {
            TestUtils.TryRemoveTestUser(testId);

            var dbContext = TestUtils.CreateDatabase();

            try
            {
                var referrer         = dbContext.Referrer.FirstOrDefault(u => u.ReferralId == referralId);
                var startReferrerBal = dbContext.JoinUserModels().Where(u => u.UserId == referrer.UserId).Select(u => u.UserWallet.Balance).FirstOrDefault();

                var response = await RegisterNewUserWithReferralInternal(testName, testId, referralId.ToString());

                var wallet = dbContext.UserWallet.FirstOrDefault(u => u.UserId == response.Value.UserId);
                Assert.Equal(referrer.Amount, wallet.Balance);

                var dbContext2 = TestUtils.CreateDatabase();
                try
                {
                    var endReferrerBal      = dbContext2.JoinUserModels().Where(u => u.UserId == referrer.UserId).Select(u => u.UserWallet.Balance).FirstOrDefault();
                    var referralPayoutTotal = await ReferralUtils.TotalReward(dbContext2, referrer);

                    Assert.Equal(startReferrerBal + referralPayoutTotal, endReferrerBal);
                }
                finally
                {
                    dbContext2.Dispose();
                }
            }
            finally
            {
                dbContext.Dispose();
            }
        }
Ejemplo n.º 3
0
        static async Task TestSyncPlatformWithReferralInternal(string testName, string testId, bool expectSuccess, bool shouldCreateNewUser = true, int referrerId = 2)
        {
            var dbContext = TestUtils.CreateDatabase();

            try
            {
                var referrer         = dbContext.Referrer.FirstOrDefault(u => u.ReferralId == referrerId);
                var startReferrerBal = dbContext.JoinUserModels().Where(u => u.UserId == referrer.UserId).Select(u => u.UserWallet.Balance).FirstOrDefault();

                await TestUtils.RegisterNewUserWithReferralInternal(testName, testId, referrerId.ToString());

                var userStartBalance = dbContext.JoinUserModels()
                                       .Where(u => u.UserIdentity.Auth0Id == testId).Select(u => u.UserWallet.Balance).FirstOrDefault();

                var response = await TestUtils.SyncPlatform(testId, "twitch");


                var dbContext2 = TestUtils.CreateDatabase();
                try
                {
                    var wallet = dbContext2.UserWallet.FirstOrDefault(u => u.UserId == response.User.UserId);


                    var endReferrerBal      = dbContext2.JoinUserModels().Where(u => u.UserId == referrer.UserId).Select(u => u.UserWallet.Balance).FirstOrDefault();
                    var referralPayoutTotal = await ReferralUtils.TotalReward(dbContext2, referrer);

                    if (expectSuccess)
                    {
                        Assert.Equal(referrer.Amount + userStartBalance, wallet.Balance);
                        if (shouldCreateNewUser)
                        {
                            referralPayoutTotal *= 2;
                        }

                        Assert.Equal(startReferrerBal + referralPayoutTotal, endReferrerBal);
                    }
                    else
                    {
                        Assert.Equal(referrer.Amount, wallet.Balance);
                        decimal plus = 0;
                        if (shouldCreateNewUser)
                        {
                            plus += referralPayoutTotal;
                        }

                        Assert.Equal(startReferrerBal + plus, endReferrerBal);
                    }
                }
                finally
                {
                    dbContext2.Dispose();
                }
            }
            finally
            {
                dbContext.Dispose();
            }
        }
Ejemplo n.º 4
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);
            }
        }
        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);
            }
        }