< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Wallet.BitcoinWalletService
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Wallet/BitcoinWalletService.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 46
Coverable lines: 46
Total lines: 110
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%620%
SendTransactionAsync()100%210%
GetTransactionAsync()100%210%
GetCurrentBlockHeightAsync()100%210%
GetBlockAsync()100%210%
GetTransactionConfirmationsAsync()100%210%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Wallet/BitcoinWalletService.cs

#LineLine coverage
 1using System.Net;
 2using Microsoft.Extensions.Logging;
 3using Microsoft.Extensions.Options;
 4using NBitcoin;
 5using NBitcoin.RPC;
 6using NLightning.Domain.Node.Options;
 7using NLightning.Infrastructure.Bitcoin.Options;
 8using NLightning.Infrastructure.Bitcoin.Wallet.Interfaces;
 9
 10namespace NLightning.Infrastructure.Bitcoin.Wallet;
 11
 12public class BitcoinWalletService : IBitcoinWallet
 13{
 14    private readonly RPCClient _rpcClient;
 15    private readonly ILogger<BitcoinWalletService> _logger;
 16
 017    public BitcoinWalletService(IOptions<BitcoinOptions> bitcoinOptions, ILogger<BitcoinWalletService> logger,
 018                                IOptions<NodeOptions> nodeOptions)
 19    {
 020        _logger = logger;
 021        var network = Network.GetNetwork(nodeOptions.Value.BitcoinNetwork) ?? Network.Main;
 22
 023        var rpcCredentials = new RPCCredentialString
 024        {
 025            UserPassword = new NetworkCredential(bitcoinOptions.Value.RpcUser, bitcoinOptions.Value.RpcPassword)
 026        };
 27
 028        _rpcClient = new RPCClient(rpcCredentials, bitcoinOptions.Value.RpcEndpoint, network);
 029        _rpcClient.GetBlockchainInfo();
 030    }
 31
 32    public async Task<uint256> SendTransactionAsync(Transaction transaction)
 33    {
 34        try
 35        {
 036            _logger.LogInformation("Broadcasting transaction {TxId}", transaction.GetHash());
 037            var result = await _rpcClient.SendRawTransactionAsync(transaction);
 038            _logger.LogInformation("Successfully broadcast transaction {TxId}", result);
 039            return result;
 40        }
 041        catch (Exception ex)
 42        {
 043            _logger.LogError(ex, "Failed to broadcast transaction {TxId}", transaction.GetHash());
 044            throw;
 45        }
 046    }
 47
 48    public async Task<Transaction?> GetTransactionAsync(uint256 txId)
 49    {
 50        try
 51        {
 052            return await _rpcClient.GetRawTransactionAsync(new uint256(txId), false);
 53        }
 054        catch (RPCException ex) when (ex.RPCCode == RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY)
 55        {
 056            return null; // Transaction not found
 57        }
 058        catch (Exception ex)
 59        {
 060            _logger.LogError(ex, "Failed to get transaction {TxId}", txId);
 061            throw;
 62        }
 063    }
 64
 65    public async Task<uint> GetCurrentBlockHeightAsync()
 66    {
 67        try
 68        {
 069            var blockCount = await _rpcClient.GetBlockCountAsync();
 070            return (uint)blockCount;
 71        }
 072        catch (Exception ex)
 73        {
 074            _logger.LogError(ex, "Failed to get current block height");
 075            throw;
 76        }
 077    }
 78
 79    public async Task<Block?> GetBlockAsync(uint height)
 80    {
 81        try
 82        {
 083            var blockHash = await _rpcClient.GetBlockHashAsync((int)height);
 084            return await _rpcClient.GetBlockAsync(blockHash);
 85        }
 086        catch (Exception ex)
 87        {
 088            _logger.LogError(ex, "Failed to get block at height {Height}", height);
 089            throw;
 90        }
 091    }
 92
 93    public async Task<uint> GetTransactionConfirmationsAsync(uint256 txId)
 94    {
 95        try
 96        {
 097            var txInfo = await _rpcClient.GetRawTransactionInfoAsync(new uint256(txId));
 098            return txInfo.Confirmations;
 99        }
 0100        catch (RPCException ex) when (ex.RPCCode == RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY)
 101        {
 0102            return 0; // Transaction not found
 103        }
 0104        catch (Exception ex)
 105        {
 0106            _logger.LogError(ex, "Failed to get confirmations for transaction {TxId}", txId);
 0107            throw;
 108        }
 0109    }
 110}