lnrpc: implement wallet balance RPC call
This commit is contained in:
parent
9c0566cb42
commit
c7e9bb8c58
@ -7,6 +7,7 @@ service Lightning {
|
||||
rpc NewAddress(NewAddressRequest) returns (NewAddressResponse);
|
||||
|
||||
rpc ConnectPeer(ConnectPeerRequest) returns (ConnectPeerResponse);
|
||||
rpc WalletBalance(WalletBalanceRequest) returns (WalletBalanceResponse);
|
||||
}
|
||||
|
||||
message SendManyRequest {
|
||||
@ -36,4 +37,9 @@ message ConnectPeerRequest {
|
||||
|
||||
message ConnectPeerResponse {
|
||||
bytes lnID = 1;
|
||||
message WalletBalanceRequest {
|
||||
bool witness_only = 1;
|
||||
}
|
||||
message WalletBalanceResponse {
|
||||
double balance = 1;
|
||||
}
|
||||
|
36
rpcserver.go
36
rpcserver.go
@ -134,4 +134,40 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
|
||||
|
||||
rpcsLog.Infof("Connected to peer: %v", peerAddr.String())
|
||||
return &lnrpc.ConnectPeerResponse{[]byte(peerAddr.String())}, nil
|
||||
// WalletBalance returns the sum of all confirmed unspent outputs under control
|
||||
// by the wallet. This method can be modified by having the request specify
|
||||
// only witness outputs should be factored into the final output sum.
|
||||
// TODO(roasbeef): split into total and confirmed/unconfirmed
|
||||
func (r *rpcServer) WalletBalance(ctx context.Context,
|
||||
in *lnrpc.WalletBalanceRequest) (*lnrpc.WalletBalanceResponse, error) {
|
||||
|
||||
var balance float64
|
||||
|
||||
if in.WitnessOnly {
|
||||
witnessOutputs, err := r.server.lnwallet.ListUnspentWitness(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We need to convert from BTC to satoshi here otherwise, and
|
||||
// incorrect sum will be returned.
|
||||
var outputSum btcutil.Amount
|
||||
for _, witnessOutput := range witnessOutputs {
|
||||
outputSum += btcutil.Amount(witnessOutput.Amount * 1e8)
|
||||
}
|
||||
|
||||
balance = outputSum.ToBTC()
|
||||
} else {
|
||||
// TODO(roasbeef): make num confs a param
|
||||
outputSum, err := r.server.lnwallet.CalculateBalance(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
balance = outputSum.ToBTC()
|
||||
}
|
||||
|
||||
rpcsLog.Debugf("walletbalance query response: %v", balance)
|
||||
|
||||
return &lnrpc.WalletBalanceResponse{balance}, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user