Can't see balance after some time

by g4s8   Last Updated September 20, 2018 09:27 AM

I'm trying to use bitcoinj library to receive bitcoin payments and monitor the balance on the address. My implementation is based on DeterministicKey watching public key. The code below is working fine for some time, it prints a message when coins received and print correct balance after it. But if I restart it, the balance becomes zero and the wallet loses all received previously transactions (getTransactionsByTime() is empty list), so it's working only if I send some coins to the static address while the code is running. Also if I start the code again and send new coins to the same address (it's static), I'll see only new transaction, all previous are lost. This is my demo code to check it's working (groovy):

def net = TestNet3Params.get()
def wlt = Wallet.fromWatchingKey(net, DeterministicKey.deserializeB58('tpubD9xtwS1WLdbdLQKCSiR8kiwBwWzL1C1qq7LTgMq12ugeqyGbDCoFic2zqrNgwthyvq8DUubn6CSev58Ja2o9KgMQxafDd6UZWG9JSZWcdGY', net))
def store = new SPVBlockStore(net, new File("/tmp/spvbstore.blockchain"))
def chain = new BlockChain(net, store)
def peers = new PeerGroup(net, chain)
peers.maxConnections = 5
peers.maxPeersToDiscoverCount = 5
peers.addPeerDiscovery(new DnsDiscovery.DnsSeedDiscovery(net, 'testnet-seed.bitcoin.jonasschnelli.ch'))
chain.addWallet(wlt)
peers.addWallet(wlt)
peers.start()
peers.downloadBlockChain()
wlt.addCoinsReceivedEventListener(
  new WalletCoinsReceivedEventListener() {
    @Override
    void onCoinsReceived(
      final Wallet wallet,
      final Transaction tx, final Coin prevBalance, final Coin newBalance) {
        println("RECEIVED: $tx: $prevBalance -> $newBalance")
    }
  }
)
while (true) {
  println("BALANCE: ${wlt.getBalance(Wallet.BalanceType.ESTIMATED)}")
  println("transactions:\n$wlt.transactionsByTime")
  Thread.sleep(10000)
}


Related Questions


Bitcoinj wallet can't receive coins in Testnet3

Updated October 05, 2018 14:27 PM


Wallet balance is not updating

Updated June 27, 2017 06:27 AM

How to convert Satoshi to BTC in Java?

Updated July 01, 2017 07:27 AM