Example #1
0
        public async Task <ActionResult <SubscriptionResponse> > New([FromBody] SubRequest subRequest)
        {
            var platformId = BitcornUtils.GetPlatformId(subRequest.Id);
            var user       = await BitcornUtils.GetUserForPlatform(platformId, _dbContext).FirstOrDefaultAsync();

            if (user != null && UserLockCollection.Lock(user.UserId))
            {
                try
                {
                    subRequest.Platform = platformId.Platform;
                    var tx = await SubscriptionUtils.Subscribe(_dbContext, user, subRequest);

                    if (tx != null)
                    {
                        return(tx);
                    }
                }
                catch (Exception e)
                {
                    await BITCORNLogger.LogError(_dbContext, e, JsonConvert.SerializeObject(subRequest));

                    throw e;
                }
                finally
                {
                    UserLockCollection.Release(user.UserId);
                }
            }

            return(StatusCode((int)HttpStatusCode.BadRequest));
        }
        public async Task <ActionResult> Download(WalletDownload walletDownload, DateTime now)
        {
            var user = await _dbContext.User.FirstOrDefaultAsync(r => r.UserId == walletDownload.UserId);

            var userReferral = await _dbContext.UserReferral.FirstOrDefaultAsync(r => r.UserId == walletDownload.UserId);

            if (userReferral != null && !UserLockCollection.Lock(userReferral.UserId))
            {
                return(StatusCode(UserLockCollection.UserLockedReturnCode));
            }

            try
            {
                var downloads = _dbContext.WalletDownload.Where(w => w.TimeStamp.AddDays(7) > now).Where(d => d.IPAddress == walletDownload.IPAddress);

                if (!downloads.Any() && userReferral != null && (userReferral.ReferralId != 0 && userReferral.WalletDownloadDate == null))
                {
                    try
                    {
                        walletDownload.ReferralUserId = _dbContext.Referrer
                                                        .FirstOrDefault(r => r.ReferralId == Convert.ToInt32(userReferral.ReferralId))?.UserId;
                        walletDownload.ReferralCode = userReferral.ReferralId.ToString();
                    }
                    catch (Exception e)
                    {
                        throw new Exception($"could not convert referral code: {walletDownload.ReferralCode} to an integer");
                    }

                    var referrerUser = await _dbContext.JoinUserModels().FirstOrDefaultAsync(u => u.UserId == walletDownload.ReferralUserId);

                    if (referrerUser != null && userReferral != null && userReferral?.WalletDownloadDate == null)
                    {
                        if (!referrerUser.IsBanned && !user.IsBanned)
                        {
                            await ReferralUtils.ReferralRewards(_dbContext, walletDownload, userReferral, referrerUser, user, "Wallet download");
                        }
                    }
                }

                walletDownload.TimeStamp = now;
                _dbContext.WalletDownload.Add(walletDownload);
                await _dbContext.SaveAsync();

                return(StatusCode((int)HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                await BITCORNLogger.LogError(_dbContext, e, JsonConvert.SerializeObject(walletDownload));

                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
            finally
            {
                if (userReferral != null)
                {
                    UserLockCollection.Release(userReferral.UserId);
                }
            }
        }
Example #3
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);
                }
            }
        }
Example #4
0
        public static async Task <int> TryClaimTx(PlatformId platformId, User user, BitcornContext dbContext)
        {
            if (user == null)
            {
                user = await BitcornUtils.GetUserForPlatform(platformId, dbContext).FirstOrDefaultAsync();
            }
            if (user == null)
            {
                return(0);
            }

            bool lockApplied = false;

            try
            {
                if (UserLockCollection.Lock(user))
                {
                    lockApplied = true;
                }
                else
                {
                    return(0);
                }

                var now = DateTime.Now;
                var txs = await dbContext.UnclaimedTx.Where(u => u.Platform == platformId.Platform &&
                                                            u.ReceiverPlatformId == platformId.Id &&
                                                            !u.Claimed && !u.Refunded &&
                                                            u.Expiration > now).ToArrayAsync();

                var sql = new StringBuilder();
                var pk  = nameof(UserWallet.UserId);

                foreach (var tx in txs)
                {
                    tx.Claimed = true;

                    var log = new CornTx();
                    log.Amount     = tx.Amount;
                    log.Platform   = tx.Platform;
                    log.ReceiverId = user.UserId;
                    log.SenderId   = tx.SenderUserId;
                    log.Timestamp  = tx.Timestamp;
                    log.TxGroupId  = Guid.NewGuid().ToString();
                    log.TxType     = tx.TxType;
                    log.UnclaimedTx.Add(tx);
                    dbContext.CornTx.Add(log);

                    sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), tx.Amount, '+', pk, user.UserId));
                    sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.AmountOfTipsReceived), 1, '+', pk, user.UserId));
                    sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.TotalReceivedBitcornTips), tx.Amount, '+', pk, user.UserId));
                    sql.Append(TxUtils.UpdateNumberIfTop(nameof(UserStat), nameof(UserStat.LargestReceivedBitcornTip), tx.Amount, pk, user.UserId));


                    sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.AmountOfTipsSent), 1, '+', pk, tx.SenderUserId));
                    sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.TotalSentBitcornViaTips), tx.Amount, '+', pk, tx.SenderUserId));
                    sql.Append(TxUtils.UpdateNumberIfTop(nameof(UserStat), nameof(UserStat.LargestSentBitcornTip), tx.Amount, pk, tx.SenderUserId));
                }
                if (txs.Length > 0)
                {
                    await DbOperations.ExecuteSqlRawAsync(dbContext, sql.ToString());

                    await dbContext.SaveAsync();
                }
                return(txs.Length);
            }
            catch (Exception e)
            {
                await BITCORNLogger.LogError(dbContext, e, JsonConvert.SerializeObject(platformId));

                return(0);
            }
            finally
            {
                if (lockApplied)
                {
                    UserLockCollection.Release(user);
                }
            }
        }
        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);
            }
        }