Re: bitcoinj check address balance

2018-01-05 Thread Andreas Schildbach
You're probably not starting blockchain download and waiting until it is
finished. Have a look at https://bitcoinj.github.io/ for documentation,
particularly: https://bitcoinj.github.io/working-with-the-wallet


On 12/19/2017 07:49 PM, Ивайло Иванов wrote:
> Hallo. I am using bitcoinj (I know very little this library) for check
> address balance, but I have problem with her. I don't understand how I
> can do it.
> I am using these 3 files for this exercise. I'm wrong something, but I
> can't figure out what!
> 
> Can anyone help with this problem?
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "bitcoinj" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bitcoinj+unsubscr...@googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups 
"bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bitcoinj+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


bitcoinj check address balance

2017-12-19 Thread Ивайло Иванов
Hallo. I am using bitcoinj (I know very little this library) for check 
address balance, but I have problem with her. I don't understand how I can 
do it. 
I am using these 3 files for this exercise. I'm wrong something, but I 
can't figure out what!

Can anyone help with this problem?

-- 
You received this message because you are subscribed to the Google Groups 
"bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bitcoinj+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package org.my.balance;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionConfidence;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.wallet.CoinSelection;
import org.bitcoinj.wallet.CoinSelector;

/**
 * This class implements a {@link org.bitcoinj.wallet.CoinSelector} which attempts to select all outputs
 * from a designated address. Outputs are selected in order of highest priority.  Note that this means we may
 * end up "spending" more priority than would be required to get the transaction we are creating confirmed.
 */

public class AddressBalance implements CoinSelector {

private Address addressToQuery;

public AddressBalance(Address addressToQuery) {
this.addressToQuery = addressToQuery;
}

@Override
public CoinSelection select(Coin biTarget, List candidates) {
long target = biTarget.longValue();
HashSet selected = new HashSet();
// Sort the inputs by age*value so we get the highest "coindays" spent.
// TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
ArrayList sortedOutputs = new ArrayList(candidates);
// When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting
// them in order to improve performance.
if (!biTarget.equals(NetworkParameters.MAX_MONEY)) {
sortOutputs(sortedOutputs);
}
// Now iterate over the sorted outputs until we have got as close to the target as possible or a little
// bit over (excessive value will be change).
long totalOutputValue = 0;
for (TransactionOutput output : sortedOutputs) {
if (totalOutputValue >= target) break;
// Only pick chain-included transactions, or transactions that are ours and pending.
if (!shouldSelect(output)) continue;
selected.add(output);
totalOutputValue += output.getValue().longValue();
 }
// Total may be lower than target here, if the given candidates were insufficient to create to requested
// transaction.
return new CoinSelection(Coin.valueOf(totalOutputValue), selected);
}

static void sortOutputs(ArrayList outputs) {
Collections.sort(outputs, new Comparator() {
public int compare(TransactionOutput a, TransactionOutput b) {
int depth1 = 0;
int depth2 = 0;
TransactionConfidence conf1 = a.getParentTransaction().getConfidence();
TransactionConfidence conf2 = b.getParentTransaction().getConfidence();
if (conf1.getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING)
depth1 = conf1.getDepthInBlocks();
if (conf2.getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING)
depth2 = conf2.getDepthInBlocks();
Coin aValue = a.getValue();
Coin bValue = b.getValue();
BigInteger aCoinDepth = BigInteger.valueOf(aValue.value).multiply(BigInteger.valueOf(depth1));
BigInteger bCoinDepth = BigInteger.valueOf(bValue.value).multiply(BigInteger.valueOf(depth2));
int c1 = bCoinDepth.compareTo(aCoinDepth);
if (c1 != 0) return c1;
// The "coin*days" destroyed are equal, sort by value alone to get the lowest transaction size.
int c2 = bValue.compareTo(aValue);
if (c2 != 0) return c2;
// They are entirely equivalent (possibly pending) so sort by hash to ensure a total ordering.
BigInteger aHash = a.getParentTransaction().getHash().toBigInteger();
BigInteger bHash = b.getParentTransaction().getHash().toBigInteger();
return aHash.compareTo(bHash);
}
});
}

/** Sub-classes can override this to just customize whether transactions are usable, but keep age sorting. */
protected boolean shouldSelect(TransactionOutput output) {
Address outputToAddress =