public async Task <List <WalletTransaction> > GetWalletTransactionsSince(TransactionDataType transactionDataType, int currencyId, int walletTimeoutMinutes, int searchBlockLength) { try { using (var context = _exchangeDataContextFactory.CreateReadOnlyContext()) { var currency = await context.Currency.FirstOrDefaultNoLockAsync(c => c.Id == currencyId); var walletConnection = new WalletConnector(currency.WalletHost, currency.WalletPort, currency.WalletUser, currency.WalletPass, Math.Max(walletTimeoutMinutes, 1) * 60000); var blockHash = await walletConnection.GetBlockHashAsync(searchBlockLength); var walletTransactions = await walletConnection.GetTransactionsAsync(blockHash, transactionDataType); return(walletTransactions.Select(x => new WalletTransaction { Timestamp = x.Time.ToDateTime(), Account = x.Account, Amount = Math.Abs(x.Amount), Txid = x.Txid, Type = Extensions.ToTransactionType(x.Category), Address = x.Address, Confirmations = x.Confirmations }) .OrderByDescending(x => x.Timestamp).ToList()); } } catch (Exception e) { _log.Exception("[GetWalletTransactions] - An exception occured while Loading Wallet Transactions", e); } return(new List <WalletTransaction>()); }
private async Task UpdateWithdrawConfirmations(IDataContext context, Currency currency) { try { Log.Message(LogLevel.Info, "Processing {0} withdrawals...", currency.Symbol); if (currency.Status == CurrencyStatus.Maintenance || currency.Status == CurrencyStatus.Offline) { return; // currency is skipped } var connector = new WalletConnector(currency.WalletHost, currency.WalletPort, currency.WalletUser, currency.WalletPass); var walletTransactions = await connector.GetTransactionsAsync(currency.LastBlockHash, TransactionDataType.Withdraw); if (walletTransactions.IsNullOrEmpty()) { Log.Message(LogLevel.Info, "No {0} withdrawals.", currency.Symbol); return; // no deposits } var existingWithdraws = await context.Withdraw.Where(x => x.CurrencyId == currency.Id && x.WithdrawStatus == WithdrawStatus.Complete).ToListAsync(); foreach (var walletWithdraw in walletTransactions.OrderBy(x => x.Time)) { var existingWithdraw = existingWithdraws.FirstOrDefault(x => x.Txid == walletWithdraw.Txid); if (existingWithdraw != null && existingWithdraw.Confirmations != walletWithdraw.Confirmations) { existingWithdraw.Confirmations = walletWithdraw.Confirmations; if (existingWithdraw.Confirmations >= 20) { currency.LastWithdrawBlockHash = walletWithdraw.Blockhash; } } } await context.SaveChangesAsync(); Log.Message(LogLevel.Info, "Processing {0} withdrawals complete.", currency.Symbol); } catch (Exception ex) { Log.Exception("An exception occured updating withdraw confirmations.", ex); } }
private async Task LoadWalletTransactions(Wallet wallet) { WalletTransactions.Clear(); try { var walletConnection = new WalletConnector(wallet.Host, wallet.Port, wallet.User, wallet.Pass, 60000 * 4); var wallettransactions = await walletConnection.GetTransactionsAsync("", _selectedTransactionType); var transactions = wallettransactions.Select(x => new WalletTransaction { Timestamp = x.Time.ToDateTime(), Account = x.Account, Amount = Math.Abs(x.Amount), Txid = x.Txid, Type = Cryptopia.WalletAPI.Helpers.Extensions.ToTransactionType(x.Category), Address = x.Address, Confirmations = x.Confirmations }) .OrderByDescending(x => x.Timestamp); int refesh = 10; foreach (var item in transactions) { WalletTransactions.Add(item); refesh--; if (refesh < 0) { refesh = 10; await Task.Delay(1); } } } catch (Exception ex) { MessageBox.Show("Something went wrong \n" + ex.ToString()); } }
/// <summary> /// Updates the confirmations. /// </summary> private async Task UpdateConfirmations() { using (var currencyRepo = new Repository <Currency>()) using (var withdrawRepo = new Repository <Withdraw>()) { var withdrawals = withdrawRepo.GetAll(x => x.Confirmations < _maxConfirmations).ToListNoLock(); if (!withdrawals.Any()) { Log.Message(LogLevel.Info, "[UpdateConfirmations] - No withdrawals found with less than {0} confirmations", _maxConfirmations); return; } var currencyGroups = withdrawals.GroupBy(x => x.CurrencyId); foreach (var currencyWithdrawals in currencyGroups) { try { if (!_isEnabled) { return; } var currency = await currencyRepo.GetOrDefaultAsync(x => x.Id == currencyWithdrawals.Key && x.IsEnabled); if (currency == null) { Log.Message(LogLevel.Info, "[UpdateConfirmations] - CurrencyId {0} not found or is disabled, skipping.", currencyWithdrawals.Key); continue; } if (!IsWalletOnline(currency.Status)) { Log.Message(LogLevel.Info, "[UpdateConfirmations] - {0} wallet current status is '{1}', skipping.", currency.Symbol, currency.Status); continue; } Log.Message(LogLevel.Info, "[UpdateConfirmations] - Updating {0} transaction confirmations", currency.Symbol); var wallet = new WalletConnector(currency.WalletHost, currency.WalletPort, currency.WalletUser, currency.WalletPass); var transactions = await wallet.GetTransactionsAsync(currency.LastWithdrawBlockHash, TransactionDataType.Withdraw); foreach (var withdrawal in currencyWithdrawals) { try { if (!_isEnabled) { return; } var transaction = transactions.FirstOrDefault(x => x.Txid == withdrawal.Txid); if (transaction == null) { // LogError("UpdateConfirmations", "Withdraw transaction not found in wallet, WithdrawId: {0}, TxId: {1}", withdrawal.Id, withdrawal.Txid); continue; } if (withdrawal.Confirmations != transaction.Confirmations) { withdrawal.Confirmations = transaction.Confirmations; if (withdrawal.Confirmations >= _maxConfirmations) { currency.LastWithdrawBlockHash = transaction.Blockhash; } Log.Message(LogLevel.Debug, "[UpdateConfirmations] - Updated confirmations on transaction, Confirms: {0}, TxId: {1}", transaction.Confirmations, transaction.Txid); } } catch (Exception ex) { LogError(ex, "UpdateConfirmations", "An exception occured in processing {0} withdrawal, Id: {1}", currency.Symbol, withdrawal.Id); } } await currencyRepo.SaveOrMergeAsync(); await withdrawRepo.SaveAsync(); Log.Message(LogLevel.Info, "[UpdateConfirmations] - {0} transaction confirmation updates complete", currency.Symbol); } catch (Exception ex) { LogError(ex, "UpdateConfirmations", "An exception occured in processing withdrawals, CurrencyId: {0}", currencyWithdrawals.Key); } } } }