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

Grdddj/unconfirmed balance #1142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ type Address struct {
TotalSentSat *Amount `json:"totalSent,omitempty"`
UnconfirmedBalanceSat *Amount `json:"unconfirmedBalance"`
UnconfirmedTxs int `json:"unconfirmedTxs"`
UnconfirmedSending *Amount `json:"unconfirmedSending,omitempty"`
UnconfirmedReceiving *Amount `json:"unconfirmedReceiving,omitempty"`
Txs int `json:"txs"`
AddrTxCount int `json:"addrTxCount,omitempty"`
NonTokenTxs int `json:"nonTokenTxs,omitempty"`
Expand Down
19 changes: 16 additions & 3 deletions api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,8 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
txids []string
pg Paging
uBalSat big.Int
uBalSending big.Int
uBalReceiving big.Int
totalReceived, totalSent *big.Int
unconfirmedTxs int
totalResults int
Expand Down Expand Up @@ -1347,12 +1349,12 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
// skip already confirmed txs, mempool may be out of sync
if tx.Confirmations == 0 {
unconfirmedTxs++
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(addrDesc))
uBalReceiving.Add(&uBalReceiving, tx.getAddrVoutValue(addrDesc))
// ethereum has a different logic - value not in input and add maximum possible fees
if w.chainType == bchain.ChainEthereumType {
uBalSat.Sub(&uBalSat, tx.getAddrEthereumTypeMempoolInputValue(addrDesc))
uBalSending.Add(&uBalSending, tx.getAddrEthereumTypeMempoolInputValue(addrDesc))
} else {
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(addrDesc))
uBalSending.Add(&uBalSending, tx.getAddrVinValue(addrDesc))
}
if page == 0 {
if option == AccountDetailsTxidHistory {
Expand Down Expand Up @@ -1419,6 +1421,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
totalSecondaryValue = secondaryRate * totalBaseValue
}
}
uBalSat.Sub(&uBalReceiving, &uBalSending)
r := &Address{
Paging: pg,
AddrStr: address,
Expand All @@ -1430,6 +1433,8 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
InternalTxs: ed.internalTxs,
UnconfirmedBalanceSat: (*Amount)(&uBalSat),
UnconfirmedTxs: unconfirmedTxs,
UnconfirmedSending: amountOrNil(&uBalSending),
UnconfirmedReceiving: amountOrNil(&uBalReceiving),
Transactions: txs,
Txids: txids,
Tokens: ed.tokens,
Expand All @@ -1451,6 +1456,14 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
return r, nil
}

// Returns either the Amount or nil if the number is zero
func amountOrNil(num *big.Int) *Amount {
if num.Cmp(big.NewInt(0)) == 0 {
return nil
}
return (*Amount)(num)
}

func (w *Worker) balanceHistoryHeightsFromTo(fromTimestamp, toTimestamp int64) (uint32, uint32, uint32, uint32) {
fromUnix := uint32(0)
toUnix := maxUint32
Expand Down
2 changes: 2 additions & 0 deletions blockbook-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export interface Address {
totalSent?: string;
unconfirmedBalance: string;
unconfirmedTxs: number;
unconfirmedSending?: string;
unconfirmedReceiving?: string;
txs: number;
addrTxCount?: number;
nonTokenTxs?: number;
Expand Down