1

I wrote this function using bitcoinJ

public Wallet getWallet(ECKey ecKey) {
        Wallet wallet = walletAppKit.wallet();
        wallet.importKey(ecKey);
        wallet.addEventListener(new AbstractWalletEventListener() {
            @Override
            public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
                System.out.println("\nReceived tx " + tx.getHashAsString());
                System.out.println(tx.toString());
                System.out.println(wallet.getBalance());
            }
        });
        System.out.println("Address created:" + ecKey.toAddress(wallet.getNetworkParameters()));
        return wallet;
    }

Basically I am preparing the wallet to be used. I go to one of those testnet faucets to send some coinst to that wallet, and the event reaches the listener but prints that the balance in the wallet is 0. This is what I see in the terminal:

3376017266d6e2924ae578c8af752edd5ccc9371c38a3b9304f149dfaba36f99: Seen by 2 peers. Pending/unconfirmed. in PUSHDATA(72)[3045022100cd0f19af359abb49c429974122d2baa2d29d3975018b19d18a972d934d508e0e02207b42c2a07ddc540382266f03586299926fecf35c3f998609d1e229e7bf52df8801] PUSHDATA(33)[0363f08c5d814459ea2b8d0109b791cbd762ce266b9b6f2e02802d4934bf09d2a6] outpoint:e636ba115d9f0222e04a78cd856ebf0b184ba1a5279ecc1331241e30d44b28a3:0 out DUP HASH160 PUSHDATA(20)[5e9b9195b7aee85b53ade382f4f444a9d2522e26] EQUALVERIFY CHECKSIG 0.05922 BTC out DUP HASH160 PUSHDATA(20)[4a88f0063671c6ddd1a3faa121d1b69f82f8dc82] EQUALVERIFY CHECKSIG 0.001 BTC

Sep 17, 2015 11:29:17 PM org.bitcoinj.wallet.WalletFiles saveNow INFO: Saving wallet, last seen block is 556391/00000000096e9b05c0986940553240db3d497900881bcc22df8bdad39144b053 Sep 17, 2015 11:29:17 PM org.bitcoinj.wallet.WalletFiles saveNowInternal INFO: Save completed in 98msec 0

So why getBalance() is returning me 0? Do I need to wait longer? How long? Because I sued the debugger and i called wallet.getBalance() again and again but I don't see it changing, its always 0.

  • I'd wait until the transaction gets a few confirmations. You can check on a block explorer. – David Schwartz Sep 18 '15 at 1:37
  • 1
    @David Schwartz You are right the transaction appeared and I can see my balance now thanks. – sfrj Sep 19 '15 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.