static async Task <bool> ShareSubscriptionPaymentWithReferrer(BitcornContext dbContext,
                                                                      Subscription subInfo,
                                                                      decimal cost,
                                                                      SubTx subTx,
                                                                      Referrer referrer,
                                                                      User referrerUser,
                                                                      User subscriptionPaymentRecipient)
        {
            if (referrer.EnableSubscriptionRewards)
            {
                //get referrer user info
                if (!subInfo.RestrictReferralRewards || (subInfo.RestrictReferralRewards && (referrerUser.Level == "BEST" ||
                                                                                             referrerUser.Level == "BAIT" ||
                                                                                             referrerUser.IsAdmin() ||
                                                                                             referrer.Tier >= 3)))
                {
                    //calculate amount that will be sent to the referrer
                    var referralShare = cost * subInfo.ReferrerPercentage.Value;
                    //prepare transaction to the referrer
                    var referralShareTx = await TxUtils.PrepareTransaction(subscriptionPaymentRecipient, referrerUser, referralShare, "BITCORNFarms", "$sub referral share", dbContext);

                    //make sure stat tracking values have been initialized
                    if (referrerUser.UserStat.TotalReferralRewardsCorn == null)
                    {
                        referrerUser.UserStat.TotalReferralRewardsCorn = 0;
                    }
                    //make sure stat tracking values have been initialized
                    if (referrerUser.UserStat.TotalReferralRewardsUsdt == null)
                    {
                        referrerUser.UserStat.TotalReferralRewardsUsdt = 0;
                    }
                    //increment total received corn rewards
                    referrerUser.UserStat.TotalReferralRewardsCorn += referralShare;
                    //inceremnt total received usdt rewards
                    referrerUser.UserStat.TotalReferralRewardsUsdt +=
                        ((referralShare) * (await ProbitApi.GetCornPriceAsync(dbContext)));
                    //execute transaction
                    if (await TxUtils.ExecuteTransaction(referralShareTx, dbContext))
                    {
                        //if transaction was made, log & update ytd total
                        await ReferralUtils.UpdateYtdTotal(dbContext, referrer, referralShare);

                        var referralTx = await ReferralUtils.LogReferralTx(dbContext, referrerUser.UserId, referralShare, "$sub referral share");

                        subTx.ReferralTxId = referralTx.ReferralTxId;
                        await dbContext.SaveAsync();

                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 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);
                }
            }
        }