From 315bc25bf5a2438a769439cd043350e2e66ec922 Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 20 Nov 2024 16:50:30 +0100 Subject: [PATCH 1/2] feat: GetWalletBalance call --- src/Proto/nodeguard.proto | 17 +++++++++++++++++ src/Rpc/NodeGuardService.cs | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Proto/nodeguard.proto b/src/Proto/nodeguard.proto index 59fe8629..afc5f691 100644 --- a/src/Proto/nodeguard.proto +++ b/src/Proto/nodeguard.proto @@ -34,6 +34,12 @@ service NodeGuardService { Gets a list of available wallets */ rpc GetAvailableWallets(GetAvailableWalletsRequest) returns (GetAvailableWalletsResponse); + + /* + Get balance from a specific wallet + */ + rpc GetWalletBalance(GetWalletBalanceRequest) returns (GetWalletBalanceResponse); + /* Opens a channel to a given node */ @@ -168,6 +174,17 @@ message GetAvailableWalletsResponse { repeated Wallet wallets = 1; } +message GetWalletBalanceRequest { + int32 wallet_id = 1; +} + +message GetWalletBalanceResponse { + // Confirmed balance in satoshis + int64 confirmed_balance = 1; + // Unconfirmed balance in satoshis + int64 unconfirmed_balance = 2; +} + message AddNodeRequest{ string pub_key = 1; string name = 2; diff --git a/src/Rpc/NodeGuardService.cs b/src/Rpc/NodeGuardService.cs index 1a3b9433..ae209394 100644 --- a/src/Rpc/NodeGuardService.cs +++ b/src/Rpc/NodeGuardService.cs @@ -28,6 +28,8 @@ Task GetNewWalletAddress(GetNewWalletAddressRequest Task GetAvailableWallets(GetAvailableWalletsRequest request, ServerCallContext context); + + Task GetWalletBalance(GetWalletBalanceRequest request, ServerCallContext context); Task GetNodes(GetNodesRequest request, ServerCallContext context); @@ -377,6 +379,28 @@ public override async Task GetAvailableWallets(GetA throw new RpcException(new Status(StatusCode.Internal, e.Message)); } } + + public override async Task GetWalletBalance(GetWalletBalanceRequest request, ServerCallContext context) + { + var wallet = await _walletRepository.GetById(request.WalletId); + if (wallet == null) + { + throw new RpcException(new Status(StatusCode.NotFound, "Wallet not found")); + } + + var balance = await _lightningService.GetWalletBalance(wallet); + if (balance == null) + { + throw new RpcException(new Status(StatusCode.Internal, "Error getting wallet balance")); + } + + return new GetWalletBalanceResponse + { + ConfirmedBalance = ((Money)balance.Confirmed).Satoshi, + UnconfirmedBalance = ((Money)balance.Unconfirmed).Satoshi + }; + } + public override async Task AddNode(AddNodeRequest request, ServerCallContext context) { From 11e8518f9e6904ab3db0b190a41b78e19cde6382 Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 22 Nov 2024 08:58:52 +0100 Subject: [PATCH 2/2] fix: capture error from getting balance --- src/Rpc/NodeGuardService.cs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Rpc/NodeGuardService.cs b/src/Rpc/NodeGuardService.cs index ae209394..7b2de096 100644 --- a/src/Rpc/NodeGuardService.cs +++ b/src/Rpc/NodeGuardService.cs @@ -382,23 +382,31 @@ public override async Task GetAvailableWallets(GetA public override async Task GetWalletBalance(GetWalletBalanceRequest request, ServerCallContext context) { - var wallet = await _walletRepository.GetById(request.WalletId); - if (wallet == null) + try { - throw new RpcException(new Status(StatusCode.NotFound, "Wallet not found")); - } + var wallet = await _walletRepository.GetById(request.WalletId); + if (wallet == null) + { + throw new RpcException(new Status(StatusCode.NotFound, "Wallet not found")); + } - var balance = await _lightningService.GetWalletBalance(wallet); - if (balance == null) - { - throw new RpcException(new Status(StatusCode.Internal, "Error getting wallet balance")); + var balance = await _lightningService.GetWalletBalance(wallet); + if (balance == null) + { + throw new RpcException(new Status(StatusCode.Internal, "Error getting wallet balance")); + } + + return new GetWalletBalanceResponse + { + ConfirmedBalance = ((Money)balance.Confirmed).Satoshi, + UnconfirmedBalance = ((Money)balance.Unconfirmed).Satoshi + }; } - - return new GetWalletBalanceResponse + catch (Exception e) { - ConfirmedBalance = ((Money)balance.Confirmed).Satoshi, - UnconfirmedBalance = ((Money)balance.Unconfirmed).Satoshi - }; + _logger?.LogError(e, "Error getting wallet balance through gRPC"); + throw new RpcException(new Status(StatusCode.Internal, e.Message)); + } }