<p>Sounds like a watching wallet, but you need to save the <code>wallet</code> file as well as you're saving <code>spv</code> file. As per <a href="https://bitcoinj.github.io/working-with-the-wallet#saving-the-wallet-at-the-right-times" rel="nofollow noreferrer">documentation</a></p>

CC BY-SA 4.0
2018-09-26T08:53:38.610Z
2018-09-26T08:53:38.610Z
Zombie
<p>I'm trying to use <a href="https://bitcoinj.github.io" rel="nofollow noreferrer">bitcoinj</a> library to receive bitcoin payments and monitor the balance on the address. My implementation is based on <code>DeterministicKey</code> 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 (<code>getTransactionsByTime()</code> is empty list), so it's working <strong>only</strong> 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):</p>

<pre><code>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 -&gt; $newBalance")
    }
  }
)
while (true) {
  println("BALANCE: ${wlt.getBalance(Wallet.BalanceType.ESTIMATED)}")
  println("transactions:\n$wlt.transactionsByTime")
  Thread.sleep(10000)
}
</code></pre>

CC BY-SA 4.0
2018-09-20T22:42:24.940Z
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
CC BY-SA 4.0
2018-09-21T04:29:49.607Z
@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
CC BY-SA 4.0
2018-09-21T15:45:20.193Z
when you run the code again, are you loading wallet from the file ?
Zombie
CC BY-SA 4.0
2018-09-21T17:00:16.743Z
@Zombie no, I'm creating it from fixed public key (watching key)
g4s8
CC BY-SA 4.0
2018-09-21T20:00:33.403Z
Sounds like a watching wallet, but you need to save the wallet as well as you're saving spv file
Zombie
CC BY-SA 4.0
2018-09-26T04:39:11.550Z
@Zombie thanks, it helped. Feel free to post an answer here, I'll accept it.
g4s8
CC BY-SA 4.0
2018-09-26T08:54:34.703Z
pleasure, alright
Zombie
<bitcoinj><java><receiving>
Can't see balance after some time
g4s8
# Can't see balance after some time

(Asked by: username_0 on 2018-09-20)

I'm trying to use [bitcoinj](https://bitcoinj.github.io) 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)
}

```

**Tags:** bitcoinj, java, receiving

> **Comments:**
> **username_1**: 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 ?  
> **username_0**: @username_1 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.  
> **username_1**: when you run the code again, are you loading wallet from the file ?  
> **username_0**: @username_1 no, I'm creating it from fixed public key (watching key)  
> **username_1**: Sounds like a watching wallet, but you need to save the wallet as well as you're saving spv file  
> **username_0**: @username_1 thanks, it helped. Feel free to post an answer here, I'll accept it.  
> **username_1**: pleasure, alright  

---

## 1 answers

### Answer by username_1 on 2018-09-26. Score: 2 | Accepted answer

Sounds like a watching wallet, but you need to save the `wallet` file as well as you're saving `spv` file. As per [documentation](https://bitcoinj.github.io/working-with-the-wallet#saving-the-wallet-at-the-right-times)


---

