| | | 1 | | using MessagePack; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Daemon.Ipc.Handlers; |
| | | 5 | | |
| | | 6 | | using Domain.Bitcoin.Interfaces; |
| | | 7 | | using Domain.Client.Constants; |
| | | 8 | | using Domain.Client.Enums; |
| | | 9 | | using Infrastructure.Bitcoin.Wallet.Interfaces; |
| | | 10 | | using Interfaces; |
| | | 11 | | using Services.Ipc.Factories; |
| | | 12 | | using Transport.Ipc; |
| | | 13 | | using Transport.Ipc.Responses; |
| | | 14 | | |
| | | 15 | | internal class GetWalletBalanceIpcHandler : IIpcCommandHandler |
| | | 16 | | { |
| | | 17 | | private readonly IBlockchainMonitor _blockchainMonitor; |
| | | 18 | | private readonly ILogger<GetWalletBalanceIpcHandler> _logger; |
| | | 19 | | private readonly IUtxoMemoryRepository _utxoMemoryRepository; |
| | 0 | 20 | | public ClientCommand Command => ClientCommand.WalletBalance; |
| | | 21 | | |
| | 0 | 22 | | public GetWalletBalanceIpcHandler(IBlockchainMonitor blockchainMonitor, ILogger<GetWalletBalanceIpcHandler> logger, |
| | 0 | 23 | | IUtxoMemoryRepository utxoMemoryRepository) |
| | | 24 | | { |
| | 0 | 25 | | _blockchainMonitor = blockchainMonitor; |
| | 0 | 26 | | _logger = logger; |
| | 0 | 27 | | _utxoMemoryRepository = utxoMemoryRepository; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public Task<IpcEnvelope> HandleAsync(IpcEnvelope envelope, CancellationToken ct) |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | 0 | 34 | | var currentBlockHeight = _blockchainMonitor.LastProcessedBlockHeight; |
| | 0 | 35 | | var confirmedBalance = _utxoMemoryRepository.GetConfirmedBalance(currentBlockHeight); |
| | 0 | 36 | | var unconfirmedBalance = _utxoMemoryRepository.GetUnconfirmedBalance(currentBlockHeight); |
| | | 37 | | |
| | | 38 | | // Create a success response |
| | 0 | 39 | | var response = new WalletBalanceIpcResponse |
| | 0 | 40 | | { |
| | 0 | 41 | | ConfirmedBalance = confirmedBalance, |
| | 0 | 42 | | UnconfirmedBalance = unconfirmedBalance |
| | 0 | 43 | | }; |
| | | 44 | | |
| | 0 | 45 | | var payload = MessagePackSerializer.Serialize(response, cancellationToken: ct); |
| | 0 | 46 | | var respEnvelope = new IpcEnvelope |
| | 0 | 47 | | { |
| | 0 | 48 | | Version = envelope.Version, |
| | 0 | 49 | | Command = envelope.Command, |
| | 0 | 50 | | CorrelationId = envelope.CorrelationId, |
| | 0 | 51 | | Kind = IpcEnvelopeKind.Response, |
| | 0 | 52 | | Payload = payload |
| | 0 | 53 | | }; |
| | | 54 | | |
| | 0 | 55 | | return Task.FromResult(respEnvelope); |
| | | 56 | | } |
| | 0 | 57 | | catch (Exception e) |
| | | 58 | | { |
| | 0 | 59 | | _logger.LogError(e, "Error getting a unused address"); |
| | 0 | 60 | | return Task.FromResult(IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ServerError, |
| | 0 | 61 | | $"Error getting a unused address: {e.Message}")); |
| | | 62 | | } |
| | 0 | 63 | | } |
| | | 64 | | } |