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 } }
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 } }
public async Task <(bool, string)> SendBtcAsync(int fromWalletId, string toWalletAddress, decimal amount) { //TODO: get and update transaction var wallet = await _walletRepository.GetAsync(fromWalletId); if (wallet == null) { return(false, $"Wallet with Id={fromWalletId} not found"); } if (!wallet.BalanceOut(amount)) { return(false, $"Wallet with Id={fromWalletId} insufficient funds"); } await _walletRepository.UpdateAsync(wallet); string txId; try { var bitcoinService = _bitcoinServiceFactory.CreateForWallet(wallet); txId = bitcoinService.SendToAddress(toWalletAddress, amount, string.Empty, string.Empty, false); if (string.IsNullOrEmpty(txId)) { throw new ApplicationException($"Failed to create out transaction from wallet with Id={wallet.Id}"); } } catch (Exception ex) { wallet.BalanceIn(amount); await _walletRepository.UpdateAsync(wallet); return(false, ex.Message); } var outTransaction = new OutTransaction(txId, fromWalletId, toWalletAddress, amount); await _outTransactionRepository.SaveAsync(outTransaction); return(true, string.Empty); }