mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-26 17:52:25 +01:00
rpc: break down wallet balance by accounts
This commit is contained in:
parent
4964801c6f
commit
cb3e179eaf
1832
lnrpc/rpc.pb.go
1832
lnrpc/rpc.pb.go
File diff suppressed because it is too large
Load Diff
@ -2254,8 +2254,17 @@ message ChannelEventUpdate {
|
||||
UpdateType type = 5;
|
||||
}
|
||||
|
||||
message WalletAccountBalance {
|
||||
// The confirmed balance of the account (with >= 1 confirmations).
|
||||
int64 confirmed_balance = 1;
|
||||
|
||||
// The unconfirmed balance of the account (with 0 confirmations).
|
||||
int64 unconfirmed_balance = 2;
|
||||
}
|
||||
|
||||
message WalletBalanceRequest {
|
||||
}
|
||||
|
||||
message WalletBalanceResponse {
|
||||
// The balance of the wallet
|
||||
int64 total_balance = 1;
|
||||
@ -2265,6 +2274,9 @@ message WalletBalanceResponse {
|
||||
|
||||
// The unconfirmed balance of a wallet(with 0 confirmations)
|
||||
int64 unconfirmed_balance = 3;
|
||||
|
||||
// A mapping of each wallet account's name to its balance.
|
||||
map<string, WalletAccountBalance> account_balance = 4;
|
||||
}
|
||||
|
||||
message Amount {
|
||||
|
@ -5770,6 +5770,21 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"lnrpcWalletAccountBalance": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"confirmed_balance": {
|
||||
"type": "string",
|
||||
"format": "int64",
|
||||
"description": "The confirmed balance of the account (with \u003e= 1 confirmations)."
|
||||
},
|
||||
"unconfirmed_balance": {
|
||||
"type": "string",
|
||||
"format": "int64",
|
||||
"description": "The unconfirmed balance of the account (with 0 confirmations)."
|
||||
}
|
||||
}
|
||||
},
|
||||
"lnrpcWalletBalanceResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@ -5787,6 +5802,13 @@
|
||||
"type": "string",
|
||||
"format": "int64",
|
||||
"title": "The unconfirmed balance of a wallet(with 0 confirmations)"
|
||||
},
|
||||
"account_balance": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"$ref": "#/definitions/lnrpcWalletAccountBalance"
|
||||
},
|
||||
"description": "A mapping of each wallet account's name to its balance."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
83
rpcserver.go
83
rpcserver.go
@ -25,6 +25,7 @@ import (
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/btcsuite/btcutil/psbt"
|
||||
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||
"github.com/btcsuite/btcwallet/wallet/txauthor"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||
@ -2769,34 +2770,80 @@ func (r *rpcServer) SubscribePeerEvents(req *lnrpc.PeerEventSubscription,
|
||||
func (r *rpcServer) WalletBalance(ctx context.Context,
|
||||
in *lnrpc.WalletBalanceRequest) (*lnrpc.WalletBalanceResponse, error) {
|
||||
|
||||
// Get total balance, from txs that have >= 0 confirmations.
|
||||
totalBal, err := r.server.cc.Wallet.ConfirmedBalance(
|
||||
0, lnwallet.DefaultAccountName,
|
||||
)
|
||||
// Retrieve all existing wallet accounts. We'll compute the confirmed
|
||||
// and unconfirmed balance for each and tally them up.
|
||||
accounts, err := r.server.cc.Wallet.ListAccounts("", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get confirmed balance, from txs that have >= 1 confirmations.
|
||||
// TODO(halseth): get both unconfirmed and confirmed balance in one
|
||||
// call, as this is racy.
|
||||
confirmedBal, err := r.server.cc.Wallet.ConfirmedBalance(
|
||||
1, lnwallet.DefaultAccountName,
|
||||
var totalBalance, confirmedBalance, unconfirmedBalance btcutil.Amount
|
||||
rpcAccountBalances := make(
|
||||
map[string]*lnrpc.WalletAccountBalance, len(accounts),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, account := range accounts {
|
||||
// There are two default accounts, one for NP2WKH outputs and
|
||||
// another for P2WKH outputs. The balance will be computed for
|
||||
// both given one call to ConfirmedBalance with the default
|
||||
// wallet and imported account, so we'll skip the second
|
||||
// instance to avoid inflating the balance.
|
||||
switch account.AccountName {
|
||||
case waddrmgr.ImportedAddrAccountName:
|
||||
// Omit the imported account from the response unless we
|
||||
// actually have any keys imported.
|
||||
if account.ImportedKeyCount == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get unconfirmed balance, from txs with 0 confirmations.
|
||||
unconfirmedBal := totalBal - confirmedBal
|
||||
fallthrough
|
||||
|
||||
case lnwallet.DefaultAccountName:
|
||||
if _, ok := rpcAccountBalances[account.AccountName]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
default:
|
||||
}
|
||||
|
||||
// Get total balance, from txs that have >= 0 confirmations.
|
||||
totalBal, err := r.server.cc.Wallet.ConfirmedBalance(
|
||||
0, account.AccountName,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
totalBalance += totalBal
|
||||
|
||||
// Get confirmed balance, from txs that have >= 1 confirmations.
|
||||
// TODO(halseth): get both unconfirmed and confirmed balance in
|
||||
// one call, as this is racy.
|
||||
confirmedBal, err := r.server.cc.Wallet.ConfirmedBalance(
|
||||
1, account.AccountName,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
confirmedBalance += confirmedBal
|
||||
|
||||
// Get unconfirmed balance, from txs with 0 confirmations.
|
||||
unconfirmedBal := totalBal - confirmedBal
|
||||
unconfirmedBalance += unconfirmedBal
|
||||
|
||||
rpcAccountBalances[account.AccountName] = &lnrpc.WalletAccountBalance{
|
||||
ConfirmedBalance: int64(confirmedBal),
|
||||
UnconfirmedBalance: int64(unconfirmedBal),
|
||||
}
|
||||
}
|
||||
|
||||
rpcsLog.Debugf("[walletbalance] Total balance=%v (confirmed=%v, "+
|
||||
"unconfirmed=%v)", totalBal, confirmedBal, unconfirmedBal)
|
||||
"unconfirmed=%v)", totalBalance, confirmedBalance,
|
||||
unconfirmedBalance)
|
||||
|
||||
return &lnrpc.WalletBalanceResponse{
|
||||
TotalBalance: int64(totalBal),
|
||||
ConfirmedBalance: int64(confirmedBal),
|
||||
UnconfirmedBalance: int64(unconfirmedBal),
|
||||
TotalBalance: int64(totalBalance),
|
||||
ConfirmedBalance: int64(confirmedBalance),
|
||||
UnconfirmedBalance: int64(unconfirmedBalance),
|
||||
AccountBalance: rpcAccountBalances,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user