1

This question already has an answer here:

I can get my bitcoin balance from private key but when i tried retrieving from my address its not possible posted below my code can any one tell me what i'm doing wrong

public static void main( String[] args )

        {
            NetworkParameters params = TestNet3Params.get();
            WalletAppKit kit = new  WalletAppKit(params, new File("."), "sano");
            kit.start();
            kit.isRunning();
            Address address = new Address(params, "mj255GgoGKN6uTjtWdNcF7obu88iLSTZdm");
            boolean add =kit.wallet().isAddressWatched(address);
            Wallet wallet = new Wallet(params);
            DumpedPrivateKey key = new DumpedPrivateKey(params,
                   "cTH6YyRZSqF8VvtjkfqABqjGF682cQkW3mMuageraH4CEzCrweqT");
            wallet.addKey(key.getKey());
            wallet.addWatchedAddress(new Address(params, "mj255GgoGKN6uTjtWdNcF7obu88iLSTZdm"));
            BlockChain chain = new BlockChain(params, wallet,
                   new MemoryBlockStore(params));
         PeerGroup peerGroup = new PeerGroup(params, chain);
         peerGroup.addPeerDiscovery(new DnsDiscovery(params));
         peerGroup.addWallet(wallet);
         peerGroup.start();
         peerGroup.downloadBlockChain();
         BigInteger balance = wallet.getBalance();
         System.out.println("Wallet balance: " + balance);
}

Edit 1: The below given code retrieves balance from address but each and everytime it is creating a new address.

public static void main(String[] args) {

    NetworkParameters params = TestNet3Params.get();

    String filePrefix = "forwarding-service-testnet";

    WalletAppKit kit = new WalletAppKit(params, new File("."), filePrefix);

    // Download the block chain and wait until it's done.
    kit.startAsync();
    kit.awaitRunning();

    List<Address> list = kit.wallet().getWatchedAddresses();
    if (list.size() < 2) {
        kit.wallet().addWatchedAddress(kit.wallet().freshReceiveAddress());
        System.out.println("New address created");
    }

    System.out.println("You have " + list.size() + " addresses!");
    for (Address a: list) {
        System.out.println(a.toString());
    }

    String balance = kit.wallet().getBalance().toFriendlyString();
    System.out.println(balance);
}

marked as duplicate by Pieter Wuille, j4ck, Jestin, MeshCollider, Andrew Chow Aug 7 '17 at 6:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @alcio I did check that one but still I'm not getting my balance – sano143 Aug 4 '17 at 8:54
0

I can offer you the workaround for this problem. I used web API for this feature. For example https://www.blockcypher.com/dev/bitcoin/#address-balance-endpoint.

It will allow you refuse block chain downloading part and make your code more simple.

  • Thanks @Konstantin ,But I need it this way "using bitcoinj" as mentioned – sano143 Aug 4 '17 at 7:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.