mirror of
https://github.com/aljazceru/breez-lnd.git
synced 2025-12-18 14:44:22 +01:00
rpc: break down wallet balance by accounts
This commit is contained in:
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;
|
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 WalletBalanceRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
message WalletBalanceResponse {
|
message WalletBalanceResponse {
|
||||||
// The balance of the wallet
|
// The balance of the wallet
|
||||||
int64 total_balance = 1;
|
int64 total_balance = 1;
|
||||||
@@ -2265,6 +2274,9 @@ message WalletBalanceResponse {
|
|||||||
|
|
||||||
// The unconfirmed balance of a wallet(with 0 confirmations)
|
// The unconfirmed balance of a wallet(with 0 confirmations)
|
||||||
int64 unconfirmed_balance = 3;
|
int64 unconfirmed_balance = 3;
|
||||||
|
|
||||||
|
// A mapping of each wallet account's name to its balance.
|
||||||
|
map<string, WalletAccountBalance> account_balance = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Amount {
|
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": {
|
"lnrpcWalletBalanceResponse": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -5787,6 +5802,13 @@
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"format": "int64",
|
"format": "int64",
|
||||||
"title": "The unconfirmed balance of a wallet(with 0 confirmations)"
|
"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/btcd/wire"
|
||||||
"github.com/btcsuite/btcutil"
|
"github.com/btcsuite/btcutil"
|
||||||
"github.com/btcsuite/btcutil/psbt"
|
"github.com/btcsuite/btcutil/psbt"
|
||||||
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||||
"github.com/btcsuite/btcwallet/wallet/txauthor"
|
"github.com/btcsuite/btcwallet/wallet/txauthor"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
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,
|
func (r *rpcServer) WalletBalance(ctx context.Context,
|
||||||
in *lnrpc.WalletBalanceRequest) (*lnrpc.WalletBalanceResponse, error) {
|
in *lnrpc.WalletBalanceRequest) (*lnrpc.WalletBalanceResponse, error) {
|
||||||
|
|
||||||
// Get total balance, from txs that have >= 0 confirmations.
|
// Retrieve all existing wallet accounts. We'll compute the confirmed
|
||||||
totalBal, err := r.server.cc.Wallet.ConfirmedBalance(
|
// and unconfirmed balance for each and tally them up.
|
||||||
0, lnwallet.DefaultAccountName,
|
accounts, err := r.server.cc.Wallet.ListAccounts("", nil)
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get confirmed balance, from txs that have >= 1 confirmations.
|
var totalBalance, confirmedBalance, unconfirmedBalance btcutil.Amount
|
||||||
// TODO(halseth): get both unconfirmed and confirmed balance in one
|
rpcAccountBalances := make(
|
||||||
// call, as this is racy.
|
map[string]*lnrpc.WalletAccountBalance, len(accounts),
|
||||||
confirmedBal, err := r.server.cc.Wallet.ConfirmedBalance(
|
|
||||||
1, lnwallet.DefaultAccountName,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
for _, account := range accounts {
|
||||||
return nil, err
|
// 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.
|
fallthrough
|
||||||
unconfirmedBal := totalBal - confirmedBal
|
|
||||||
|
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, "+
|
rpcsLog.Debugf("[walletbalance] Total balance=%v (confirmed=%v, "+
|
||||||
"unconfirmed=%v)", totalBal, confirmedBal, unconfirmedBal)
|
"unconfirmed=%v)", totalBalance, confirmedBalance,
|
||||||
|
unconfirmedBalance)
|
||||||
|
|
||||||
return &lnrpc.WalletBalanceResponse{
|
return &lnrpc.WalletBalanceResponse{
|
||||||
TotalBalance: int64(totalBal),
|
TotalBalance: int64(totalBalance),
|
||||||
ConfirmedBalance: int64(confirmedBal),
|
ConfirmedBalance: int64(confirmedBalance),
|
||||||
UnconfirmedBalance: int64(unconfirmedBal),
|
UnconfirmedBalance: int64(unconfirmedBalance),
|
||||||
|
AccountBalance: rpcAccountBalances,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user