小编Cot*_*oty的帖子

如何监视 Bitcoinj (java) 中某个地址的交易?

我的目标是监视公共比特币地址,并在资金发送到该地址时打印到控制台。就这样。我现在使用的是之前在 Bitcoin Core 中生成的地址。

我正在执行以下操作:

 NetworkParameters params = MainNetParams.get();
 Wallet wallet = Wallet.loadFromFile(file);
 BlockStore blockStore = new MemoryBlockStore(params);
 BlockChain chain = new BlockChain(params, wallet, blockStore);

 PeerGroup peerGroup = new PeerGroup(params, chain);
 peerGroup.addPeerDiscovery(new DnsDiscovery(params));
 peerGroup.setUseLocalhostPeerWhenPossible(true);
 peerGroup.startAsync();

 Address add = new Address(params, "1NpxxxxxxxxxxxxxxxaSC4");
 wallet.addWatchedAddress(add);

 wallet.addEventListener(new AbstractWalletEventListener() {
        @Override
        public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
            System.out.println("[main]: COINS RECIEVED!"); 
            System.out.println("\nReceived tx " + tx.getHashAsString());
            System.out.println(tx.toString());
        }
    });

 System.out.println("\nDone!\n");
 System.out.println(wallet.toString());
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,我没有正确处理 AbstractWalletEventListener。当我向该地址汇款时,我没有收到预期在控制台中看到的文本。相反,我只是从peerGroup.startAsync()方法的[NioClientManager]中看到“对等宣布的新交易”的连续流。

我做错了什么以及如何纠正?我在看似简单的任务上花费了比应有的时间更多的时间。

附言。我调用的“loadFromFile”文件只是一个由bitcoinj 生成的空白默认钱包文件。没什么特别的。

编辑:另外,我不想看到钱包的总余额。我只想知道新交易何时进入。旧交易在我的程序中无关紧要。

java bitcoin bitcoinj

4
推荐指数
1
解决办法
2462
查看次数

标签 统计

bitcoin ×1

bitcoinj ×1

java ×1