How to get pending balance in with bitcoinj?

1

I'm try get pending balance from wallet, but results do not match with pending transactions? Thanks for advice!

Ander Acosta

Posted 2015-07-23T13:33:55.213

Reputation: 372

Answers

0

Edit: I don't think there's a method to do that, but this should do the same thing:

// calculate balance, only include transactions that are not in a block
List<TransactionOutput> candidates = wallet.calculateAllSpendCandidates(false, false);
Coin value = Coin.ZERO;
for (TransactionOutput out : candidates) {
    if(out.getParentTransaction().getAppearsInHashes() == null) {
        // Not seen in any block that we know of
        value = value.add(out.getValue());
    }
}

Nick ODell

Posted 2015-07-23T13:33:55.213

Reputation: 26 536

I want to get only pending amount, Wallet.getBalance() returns the available amount. Wallet.getBalance(Wallet.BalanceType.ESTIMATED) returns a total amount (available + pending). In theory total amount - available amount = pending amount, but this result not match with amount that I send to test.Ander Acosta 2015-07-23T17:23:44.793

@AnderAcosta Gotcha. Let me check the source to see if what you're looking for is possible.Nick ODell 2015-07-23T17:42:06.317

@AnderAcosta See edit.Nick ODell 2015-07-23T17:56:39.523

I'm using this:
Coin pending = Coin.ZERO; for (Transaction transaction : wallet.getPendingTransactions()) { pending = pending.add(transaction.getValueSentToMe(wallet)); } I test your code and comparate the values, this returns the same value of your code
Ander Acosta 2015-07-23T18:41:20.037

I think found a error. When I get the pending transactions , I get one, according blockchain.info has more than 1000 confirmations , but in the wallet , this transaction is in pending state. I checked the downloaded blocks and is synchronized with the blockchain . It may be a bug of bitcoinJ? This is the transaction: https://blockchain.info/tx/282fdbc7139669cad97468949285ccf102f289f54f0dc3d9a6653448bab41192

Ander Acosta 2015-07-24T09:07:52.143

As I supposed , the transaction was pending had not been included as confirmed in the wallet , I erased the blockStore and returned to synchronize the wallet from the beginning. The transaction became confirmed once synchronized the wallet. Your code works! Thanks!!Ander Acosta 2015-07-24T10:51:39.420