private static async Task <bool> TrySharePaymentWithReferrer(BitcornContext dbContext, SubscriptionResponse output, SubRequest subRequest, Subscription subInfo, SubscriptionTier requestedTierInfo, User user, int subscriptionPaymentRecipientId, decimal cost, SubscriptionState previousSubState, SubTx subTx) { //if (previousSubState != SubscriptionState.None) return false; //if subscription referrar share is defined and its between 0 and 1 //get subscriber userreferral info var userReferral = await dbContext.UserReferral.FirstOrDefaultAsync(r => r.UserId == user.UserId); if (userReferral != null && userReferral.ReferralId != 0) { //get info of the person who referred the subscriber var referrer = await dbContext.Referrer.FirstOrDefaultAsync(u => u.ReferralId == userReferral.ReferralId); //check if referrer can get rewards if (ReferralUtils.IsValidReferrer(referrer)) { var referrerUser = await dbContext.JoinUserModels().FirstOrDefaultAsync(u => u.UserId == referrer.UserId); if (referrerUser != null && !referrerUser.IsBanned) { bool success = false; if (subInfo.ReferrerPercentage != null && subInfo.ReferrerPercentage > 0 && subInfo.ReferrerPercentage <= 1) { //get the user who received the subscription payment var subscriptionPaymentRecipient = await dbContext.JoinUserModels().FirstOrDefaultAsync(u => u.UserId == subscriptionPaymentRecipientId); success = await ShareSubscriptionPaymentWithReferrer(dbContext, subInfo, cost, subTx, referrer, referrerUser, subscriptionPaymentRecipient); } if (previousSubState == SubscriptionState.None && subInfo.Name == "BITCORNFarms") { await ReferralUtils.BonusPayout(dbContext, userReferral, referrer, user, referrerUser, referrerUser.UserStat); } return(success); } } } return(false); }
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); } } }
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); } } }