/// <summary> /// Checks if money were deposited to an address associated with any user who has a deposit address. /// When money are deposited user's balance is updated. /// </summary> private void CheckDeposits(BotDbContext context) { this.logger.Trace("()"); List <DiscordUserModel> usersToTrack = context.Users.AsQueryable().Where(x => x.DepositAddress != null).ToList(); this.logger.Trace("Tracking {0} users.", usersToTrack.Count); try { foreach (DiscordUserModel user in usersToTrack) { var addressHistory = blockCoreNodeAPI.GetAddressesHistory(user.DepositAddress).Result; var transactionHistory = addressHistory.history.Where(x => x.accountName == AccountName).SelectMany(x => x.transactionsHistory); var receivedByAddress = transactionHistory.Where(x => x.type == "received"); if (receivedByAddress.Count() > 0) { decimal totalRecivedByAddress = receivedByAddress.Sum(x => x.amount); decimal balance = Money.FromUnit(totalRecivedByAddress, MoneyUnit.Satoshi).ToUnit(MoneyUnit.BTC); if (balance > user.LastCheckedReceivedAmountByAddress) { decimal recentlyReceived = balance - user.LastCheckedReceivedAmountByAddress; // Prevent users from spamming small amounts of coins. // Also keep in mind if you'd like to change that- EF needs to be configured to track such changes. // https://stackoverflow.com/questions/25891795/entityframework-not-detecting-changes-to-decimals-after-a-certain-precision if (recentlyReceived < 0.01m) { this.logger.Trace("Skipping dust {0} for user {1}.", recentlyReceived, user); continue; } this.logger.Debug("New value for received by address is {0}. Old was {1}. Address is {2}.", receivedByAddress, user.LastCheckedReceivedAmountByAddress, user.DepositAddress); user.LastCheckedReceivedAmountByAddress = balance; user.Balance += recentlyReceived; context.Attach(user); context.Entry(user).Property(x => x.Balance).IsModified = true; context.Entry(user).Property(x => x.LastCheckedReceivedAmountByAddress).IsModified = true; context.SaveChanges(); this.logger.Info("User '{0}' deposited {1}!. New balance is {2}.", user, recentlyReceived, user.Balance); } } } } catch (Exception ex) { this.logger.Error(ex.ToString); } this.logger.Trace("(-)"); }
/// <summary> /// Checks if money were deposited to an address associated with any user who has a deposit address. /// When money are deposited user's balance is updated. /// </summary> private void CheckDeposits(BotDbContext context) { this.logger.Trace("()"); List <DiscordUserModel> usersToTrack = context.Users.Where(x => x.DepositAddress != null).ToList(); this.logger.Trace("Tracking {0} users.", usersToTrack.Count); foreach (DiscordUserModel user in usersToTrack) { decimal receivedByAddress = this.coinService.GetReceivedByAddress(user.DepositAddress, this.settings.MinConfirmationsForDeposit); if (receivedByAddress > user.LastCheckedReceivedAmountByAddress) { decimal recentlyReceived = receivedByAddress - user.LastCheckedReceivedAmountByAddress; // Prevent users from spamming small amounts of coins. // Also keep in mind if you'd like to change that- EF needs to be configured to track such changes. // https://stackoverflow.com/questions/25891795/entityframework-not-detecting-changes-to-decimals-after-a-certain-precision if (recentlyReceived < 0.01m) { this.logger.Trace("Skipping dust {0} for user {1}.", recentlyReceived, user); continue; } this.logger.Debug("New value for received by address is {0}. Old was {1}. Address is {2}.", receivedByAddress, user.LastCheckedReceivedAmountByAddress, user.DepositAddress); user.LastCheckedReceivedAmountByAddress = receivedByAddress; user.Balance += recentlyReceived; context.Attach(user); context.Entry(user).Property(x => x.Balance).IsModified = true; context.Entry(user).Property(x => x.LastCheckedReceivedAmountByAddress).IsModified = true; context.SaveChanges(); this.logger.Info("User '{0}' deposited {1}!. New balance is {2}.", user, recentlyReceived, user.Balance); } } this.logger.Trace("(-)"); }