private static void SavePortfolioHistoryEvery(int minutes, WalletBusiness.WalletBusiness walletBusiness, PortfolioHistoryBusiness.PortfolioHistoryBusiness portfolioHistoryBusiness) { while (true) { var lsUserId = GetDistinctUserId(walletBusiness); foreach (var userId in lsUserId) { _logger.Info("Scanned UserId = " + userId + " at " + CommonHelper.GetUnixTimestamp()); portfolioHistoryBusiness.InsertWithPrice(userId); } Thread.Sleep(minutes * 60 * 1000); } }
/// <summary> /// CreateNewBtcDepositTransaction /// </summary> /// <param name="transactionModel"></param> /// <param name="transactionModelDetail"></param> /// <param name="btcDepositTransactionRepository"></param> /// <param name="currentTime"></param> private static void CreateNewBtcDepositTransaction(BtcTransactionModel transactionModel, BtcTransactionDetailModel transactionModelDetail, IBitcoinDepositTransactionRepository btcDepositTransactionRepository, long currentTime ) { try { Logger.Debug("cretateNewBtcDepositTransaction "); var btcDepositTransaction = new BitcoinDepositTransaction { Hash = transactionModel.Txid, BlockNumber = 0, BlockHash = transactionModel.BlockHash, Amount = transactionModel.Amount, FromAddress = "", ToAddress = transactionModelDetail.Address, Fee = 0, Status = Status.STATUS_PENDING, CreatedAt = currentTime, UpdatedAt = currentTime }; var userId = GetUserIdByAddress(transactionModelDetail.Address); if (userId == null) { return; } var portfolioHistoryBusiness = new PortfolioHistoryBusiness.PortfolioHistoryBusiness(_persistenceFactory, false); portfolioHistoryBusiness.InsertWithPrice(userId); btcDepositTransaction.UserId = userId; Logger.Debug("cretateNewBtcDepositTransaction =>> btcDepositTransaction: " + btcDepositTransaction.ToJson()); btcDepositTransactionRepository.Insert(btcDepositTransaction); } catch (Exception e) { Logger.Error(e, "cretateNewBtcDepositTransaction "); } }
public virtual async Task <ReturnObject> ScanBlockAsync <TWithDraw, TDeposit, TBlockResponse, TTransaction>( string networkName, IWalletBusiness wallet, IRepositoryBlockchainTransaction <TWithDraw> withdrawRepoQuery, IRepositoryBlockchainTransaction <TDeposit> depositRepoQuery, IBlockchainRpc rpcClass) where TWithDraw : BlockchainTransaction where TDeposit : BlockchainTransaction where TBlockResponse : EthereumBlockResponse where TTransaction : EthereumTransactionResponse { try { int lastBlock = -1; int blockNumber = -1; //Get lastBlock from last time int.TryParse( CacheHelper.GetCacheString(String.Format(RedisCacheKey.KEY_SCANBLOCK_LASTSCANBLOCK, networkName)), out lastBlock); if (lastBlock < 0) { lastBlock = 0; } //get blockNumber: var _result = rpcClass.GetBlockNumber(); if (_result.Status == Status.STATUS_ERROR) { throw new Exception("Cant GetBlockNumber"); } if (!int.TryParse(_result.Data.ToString(), out blockNumber)) { throw new Exception("Cant parse block number"); } //Get list of new block that have transactions if (lastBlock >= blockNumber) { lastBlock = blockNumber; } Console.WriteLine("SCAN FROM " + lastBlock + "___" + blockNumber); List <TBlockResponse> blocks = new List <TBlockResponse>(); for (int i = lastBlock; i <= blockNumber; i++) { if (i < 0) { continue; } _result = rpcClass.GetBlockByNumber(i); if (_result.Status == Status.STATUS_ERROR) { return(_result); } if (_result.Data == null) { continue; } TBlockResponse _block = JsonHelper.DeserializeObject <TBlockResponse>(_result.Data.ToString()); if (_block.TransactionsResponse.Length > 0) { blocks.Add(_block); } } CacheHelper.SetCacheString(String.Format(RedisCacheKey.KEY_SCANBLOCK_LASTSCANBLOCK, networkName), blockNumber.ToString()); if (blocks.Count <= 0) { throw new Exception("no blocks have transaction"); } //Get done,List<> blocks now contains all block that have transaction //check Transaction and update: //Search transactions which need to scan: var withdrawPendingTransactions = withdrawRepoQuery.FindTransactionsNotCompleteOnNet(); //Scan all block and check Withdraw transaction: Console.WriteLine("Scan withdrawPendingTransactions"); if (withdrawPendingTransactions.Count > 0) { foreach (TBlockResponse _block in blocks) { if (withdrawPendingTransactions.Count <= 0) { //SCAN DONE: break; } for (int i = withdrawPendingTransactions.Count - 1; i >= 0; i--) { BlockchainTransaction _currentPending = withdrawPendingTransactions[i]; EthereumTransactionResponse _trans = _block.TransactionsResponse.SingleOrDefault(x => x.Hash.Equals(_currentPending.Hash)); int _blockNumber = -1; int _fee = 0; if (_trans != null) { _trans.BlockNumber.HexToInt(out _blockNumber); if (_trans.Fee != null) { _trans.Fee.HexToInt(out _fee); } Console.WriteLine("HELLO " + _currentPending.Hash); _currentPending.BlockNumber = _blockNumber; _currentPending.Fee = _fee; _currentPending.UpdatedAt = (int)CommonHelper.GetUnixTimestamp(); // _currentPending.Status = Status.StatusCompleted; // _currentPending.InProcess = 0; Console.WriteLine("CaLL UPDATE"); portfolioHistoryBusiness.InsertWithPrice(_currentPending.UserId); withdrawRepoQuery.Update((TWithDraw)_currentPending); withdrawPendingTransactions.RemoveAt(i); } } } } Console.WriteLine("Scan withdrawPendingTransactions Done"); //check wallet balance and update foreach (TBlockResponse _block in blocks) { foreach (EthereumTransactionResponse _trans in _block.TransactionsResponse) { string _toAddress = _trans.To; string _fromAddress = _trans.From; if (!wallet.CheckExistedAddress(_toAddress, networkName)) { //logger.Info(to + " is not exist in Wallet!!!"); continue; } else { //Console.WriteLine("value" + _trans.value); int _transaValue = 0; if (_trans.Value.HexToInt(out _transaValue)) { var userID = ""; // portfolioHistoryBusiness.InsertWithPrice(_trans.i); wallet.UpdateBalanceDeposit(_toAddress, (Decimal)_transaValue, networkName); } } } } return(new ReturnObject { Status = Status.STATUS_COMPLETED, Message = "Scan done" }); } catch (Exception e) { return(new ReturnObject { Status = Status.STATUS_ERROR, Message = e.Message }); } }
public ReturnObject UpdateBalanceDeposit(string toAddress, decimal addedBalance, string networkName) { try { // userID = ""; if (_connectionDb.State != ConnectionState.Open) { _connectionDb.Open(); } var wallet = FindByAddressAndNetworkName(toAddress, networkName); if (wallet == null) { return(new ReturnObject { Status = Status.STATUS_ERROR, Message = "Update fail, Address not exists" }); } if (!wallet.Currency.Equals(networkName)) { return(new ReturnObject { Status = Status.STATUS_ERROR, Message = "Not same network" }); } // wallet.Balance += addedBalance; // wallet.Version += 1; // var result = walletRepository.Update(wallet); var result = UpdateBalance(addedBalance, wallet.Id, wallet.Version); if (result.Status == Status.STATUS_ERROR) { return(result); } else { User user = userBusiness.GetUserById(wallet.UserId); // userID = user.Id; if (user != null) { portfolioHistoryBusiness.InsertWithPrice(user.Id); //send mail: EmailQueue email = new EmailQueue() { ToEmail = user.Email, NetworkName = wallet.Currency, Amount = addedBalance, Template = EmailTemplate.Received, Subject = EmailConfig.SUBJECT_SENT_OR_RECEIVED, // Content = networkName + "+" + addedBlance }; result = sendMailBusiness.CreateEmailQueueAsync(email).Result; } } return(result); } catch (Exception e) { // userID = ""; return(new ReturnObject { Status = Status.STATUS_ERROR, Message = e.Message }); } }