Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GEN-1400] Add GetWalletBalance call #401

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Proto/nodeguard.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/Rpc/NodeGuardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Task<GetNewWalletAddressResponse> GetNewWalletAddress(GetNewWalletAddressRequest

Task<GetAvailableWalletsResponse>
GetAvailableWallets(GetAvailableWalletsRequest request, ServerCallContext context);

Task<GetWalletBalanceResponse> GetWalletBalance(GetWalletBalanceRequest request, ServerCallContext context);

Task<GetNodesResponse> GetNodes(GetNodesRequest request, ServerCallContext context);

Expand Down Expand Up @@ -377,6 +379,36 @@ public override async Task<GetAvailableWalletsResponse> GetAvailableWallets(GetA
throw new RpcException(new Status(StatusCode.Internal, e.Message));
}
}

public override async Task<GetWalletBalanceResponse> GetWalletBalance(GetWalletBalanceRequest request, ServerCallContext context)
{
try
{
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
};
}
catch (Exception e)
{
_logger?.LogError(e, "Error getting wallet balance through gRPC");
throw new RpcException(new Status(StatusCode.Internal, e.Message));
}
}


public override async Task<AddNodeResponse> AddNode(AddNodeRequest request, ServerCallContext context)
{
Expand Down
Loading