/// <summary> /// Deducts the tip from the sender and adds it to the receivers tip balance /// </summary> /// <param name="tipAmountResult"></param> /// <param name="senderUserId">The id of the tip sender</param> /// <param name="receiverUserId">The id of the tip receiver</param> /// <returns>The amount that was tipped which includes the user tip default multiplier</returns> private async Task <double> SettleTip(TipAmountResult tipAmountResult, int senderUserId, int receiverUserId) { if (tipAmountResult == null || tipAmountResult.Amount <= 0) { throw new Exception("Tip amount cannot be null or 0"); } var senderProfile = await _userBalanceRepository.Get(senderUserId); double totalAmount = 0; if (tipAmountResult.RequiredMultiplier) { totalAmount = senderProfile.DefaultTipAmount * tipAmountResult.Amount; } else { totalAmount = tipAmountResult.Amount; } if (senderProfile.Balance < totalAmount) { return(0); } var receiverProfile = await _userBalanceRepository.Get(receiverUserId); senderProfile.GiveTip(totalAmount); receiverProfile.ReceiveTip(totalAmount); await _userBalanceRepository.Update(senderProfile); await _userBalanceRepository.Update(receiverProfile); await _userBalanceHistoryRepository.Update(new UserBalanceHistory(senderUserId, DateTime.UtcNow.Ticks) { Out = totalAmount, ToUserId = senderUserId }); await _userBalanceHistoryRepository.Update(new UserBalanceHistory(receiverUserId, DateTime.UtcNow.Ticks) { In = totalAmount, FromUserId = senderUserId }); return(totalAmount); }
public async Task ImportTransactions(DateTime startDateTime) { var allTransactionHistory = await _mhcHttpClient.FetchHistory(_botConfiguration.Value.TipWalletAddress); var startUnixTimeStamp = startDateTime.GetUnixEpochTimestamp(); var newTransactionHistory = allTransactionHistory.Result.Where(x => x.Timestamp > startUnixTimeStamp && x.To == _botConfiguration.Value.TipWalletAddress && x.Status == "ok" && !x.IsDelegate); var handledTransactions = 0; foreach (var newTransaction in newTransactionHistory) { var transactionHandled = await HasHandledTransaction(newTransaction.From, newTransaction.Timestamp); if (transactionHandled != null) { _logger.LogWarning($"Transaction '{newTransaction.Transaction}' was already handled (Status: '{transactionHandled.Status}'). Skipping."); continue; } var walletUser = GetAssociatedWalletUserId(newTransaction.From); if (walletUser == null) { _logger.LogInformation($"No associated user for wallet '{newTransaction.From} and transaction '{newTransaction.Transaction}'. Thanks for the donation of '{newTransaction.RealValue}' MHC!"); } else { _logger.LogInformation($"Attempting to add '{newTransaction.RealValue}' MHC from '{newTransaction.From} and transaction '{newTransaction.Transaction}'."); var userId = walletUser.GetUserId().GetValueOrDefault(); var transactionHistory = new TransactionHistory(newTransaction.To, newTransaction.Timestamp) { BlockIndex = newTransaction.BlockIndex, BlockNumber = newTransaction.BlockNumber, Value = newTransaction.Value, To = newTransaction.To, From = newTransaction.From, Status = "In Progress", Transaction = newTransaction.Transaction, ToChatUserId = userId }; await _transactionHistoryRepository.AddOrUpdate(transactionHistory); var user = await _userBalanceRepository.Get(userId); user.Balance += newTransaction.RealValue; await _userBalanceRepository.Update(user); await _userBalanceHistoryRepository.Update(new UserBalanceHistory(userId, newTransaction.Timestamp) { In = newTransaction.RealValue }); transactionHistory.Status = "Completed"; await _transactionHistoryRepository.AddOrUpdate(transactionHistory); if (walletUser.PrivateChatId != null) { await _botService.Client.SendTextMessageAsync( chatId : walletUser.PrivateChatId, text : string.Format(ReplyConstants.TopUp, newTransaction.RealValue), parseMode : ParseMode.Markdown, disableNotification : true ); _logger.LogInformation($"Notified user with chatId: '{walletUser.PrivateChatId}' for transaction '{newTransaction.Transaction}'"); } _logger.LogInformation($"Added '{newTransaction.RealValue}' MHC from '{newTransaction.From} and transaction '{newTransaction.Transaction}'."); handledTransactions++; } } int?lastTxTimestamp = null; var lastTransaction = allTransactionHistory.Result.OrderByDescending(x => x.Timestamp).FirstOrDefault(); if (lastTransaction != null) { lastTxTimestamp = lastTransaction.Timestamp; } await _transactionCheckHistoryRepository.AddOrUpdate(new TransactionCheckHistory { HandledTransactions = handledTransactions, LastTransactionTime = lastTxTimestamp }); }