Can't see balance after some time

0

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)
}

g4s8

Posted 2018-09-20T08:55:58.847

Reputation: 113

i didn't understand as per your question: if I restart it, the balance becomes zero and the wallet loses all received previously transactions, if I start the code again and send new coins to the same address, how can you sent the coins with zero balance ?Zombie 2018-09-20T22:42:24.940

@Zombie my code only watches the balance, it can't send coins because the wallet doesn't have private key here. I mean when I send coins to the address of running code, not from.g4s8 2018-09-21T04:29:49.607

when you run the code again, are you loading wallet from the file ?Zombie 2018-09-21T15:45:20.193

@Zombie no, I'm creating it from fixed public key (watching key)g4s8 2018-09-21T17:00:16.743

Sounds like a watching wallet, but you need to save the wallet as well as you're saving spv fileZombie 2018-09-21T20:00:33.403

@Zombie thanks, it helped. Feel free to post an answer here, I'll accept it.g4s8 2018-09-26T04:39:11.550

pleasure, alrightZombie 2018-09-26T08:54:34.703

Answers

2

Sounds like a watching wallet, but you need to save the wallet file as well as you're saving spv file. As per documentation

Zombie

Posted 2018-09-20T08:55:58.847

Reputation: 528