Skip to content

Commit

Permalink
feat: differentiate between Sending and Receiving unconfirmed balance…
Browse files Browse the repository at this point in the history
… for Address
  • Loading branch information
grdddj committed Oct 21, 2024
1 parent 1c70a26 commit d949b9f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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

0 comments on commit d949b9f

Please sign in to comment.