Bitcoinj wallet can't receive coins in Testnet3

1

I have a problem with bitcoinj.

I have created a wallet and than trying to send coins to it in Testnet3 using this faucet: https://coinfaucet.eu/en/btc-testnet/

Coins sended, transaction confirmed, but when I trying to get balance - it's always zero (available = available spendable = extimated = estimated spandable = 0).

Here is how I creating a Wallet:

NetworkParameters params = TestNet3Params.get();
Wallet wallet = new Wallet(params);
BlockChain chain = new BlockChain(params, wallet, new MemoryBlockStore(params));
PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.addWallet(wallet);
peerGroup.startAsync();
File file = new File("testwallet");
wallet.saveToFile(file);

Address:

wallet.currentReceiveAddress().toString()

Balance:

wallet.getBalance()
wallet.getBalance(Wallet.BalanceType.AVAILABLE_SPENDABLE)
wallet.getBalance(Wallet.BalanceType.AVAILABLE)
wallet.getBalance(Wallet.BalanceType.ESTIMATED_SPENDABLE)
wallet.getBalance(Wallet.BalanceType.ESTIMATED)

And it's always zero.

What I'm doing wrong? Why I can't receive coins?

Ilyam

Posted 2018-10-05T14:06:59.897

Reputation: 11

Answers

1

Use WalletAppKit this class is Utility class that wraps the boilerplate needed to set up a new SPV bitcoinj app. Instantiate it with a directory and file prefix, optionally configure a few things, then use startAsync and optionally awaitRunning.

i created the wallet and send the coins using your the faucet you have mentioned and its works fine and receiving coin, here is my code...

public class Main {
    static NetworkParameters params = NetworkParameters.testNet3();
    static String path = System.getProperty("user.home") + "/Desktop";
    private static File walletFile = new File(path);
    private final static String APP_NAME = "MyWallet";
    private static WalletAppKit kit;

    public static void main(String...args)
    {
        kit = new WalletAppKit(params, walletFile, APP_NAME)
        {
            @Override
            protected Wallet createWallet() {
                Wallet wallet = new Wallet(params);
                return wallet;
            }
            @Override
            protected void onSetupCompleted() {
                super.onSetupCompleted();
                System.out.println(kit.wallet().currentReceiveAddress());
                System.out.println(kit.wallet().getTotalReceived().toFriendlyString());
                txHistory();
            }
        };
        System.out.println("start syncing...");
        kit.startAsync();
        kit.awaitRunning();
    }



    private static void txHistory()
    {
        List<Transaction> txx = kit.wallet().getTransactionsByTime();
        if (!txx.isEmpty())
        {
            int i = 1;
            for (Transaction tx : txx)
            {
                System.out.println(i + "  ________________________");
                System.out.println("Date and Time: " + tx.getUpdateTime().toString());
                System.out.println("From Address: " + tx.getOutput(1).getAddressFromP2PKHScript(params));
                System.out.println("To Address: " + tx.getOutput(0).getAddressFromP2PKHScript(params));
                System.out.println("Amount Sent to me: " + tx.getValueSentToMe(kit.wallet()).toFriendlyString());
                System.out.println("Amount Sent from me: " + tx.getValueSentFromMe(kit.wallet()).toFriendlyString());
                long fee = (tx.getInputSum().getValue() > 0 ? tx.getInputSum().getValue() - tx.getOutputSum().getValue() : 0);
                System.out.println("Fee: " + Coin.valueOf(fee).toFriendlyString());
                System.out.println("Transaction Depth: " + tx.getConfidence().getDepthInBlocks());
                System.out.println("Transaction Blocks: " + tx.getConfidence().toString());
                System.out.println("Tx Hex: " + tx.getHashAsString());
                i++;
            }
        }
        else
        {

            System.err.println("No Transaction Found");
        }
    }
}

Zombie

Posted 2018-10-05T14:06:59.897

Reputation: 528