Пример #1
0
        public async Task TrackNewAsync()
        {
            try
            {
                var wallets = await _walletRepository.GetAllAsync();

                foreach (var wallet in wallets)
                {
                    var lastTransaction = await _inTransactionRepository.FindLastAsync(wallet.Id);

                    var bitcoinService = _bitcoinServiceFactory.CreateForWallet(wallet);

                    var listSinceBlockResponse = bitcoinService.ListSinceBlock(lastTransaction?.BlockHash);
                    foreach (var transaction in listSinceBlockResponse.Transactions)
                    {
                        var inTransaction = new InTransaction(transaction.TxId, wallet.Id, transaction.Amount, transaction.BlockHash);
                        inTransaction.UpdateConfirmations(transaction.Confirmations);
                        await _inTransactionRepository.SaveAsync(inTransaction);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log
            }
        }
        private async Task ProcessInTransactionAsync(InTransaction inTransaction)
        {
            try
            {
                IBitcoinService bitcoinService;
                if (_memoryCache.TryGetValue($"BitcoinService_{inTransaction.ToWalletId}", out var service))
                {
                    bitcoinService = service as IBitcoinService;
                }
                else
                {
                    var wallet = await _walletRepository.GetAsync(inTransaction.ToWalletId);

                    bitcoinService = _bitcoinServiceFactory.CreateForWallet(wallet);
                    _memoryCache.Set($"BitcoinService_{inTransaction.ToWalletId}", bitcoinService);
                }

                var getTransactionResponse = bitcoinService.GetTransaction(inTransaction.TxId);
                inTransaction.UpdateConfirmations(getTransactionResponse.Confirmations);
                await _inTransactionRepository.UpdateAsync(inTransaction);
            }
            catch (Exception ex)
            {
                //Log
            }
        }