首页 > 解决方案 > BitcoinJ 钱包显示错误的余额

问题描述

我越来越熟悉BitcoinJ。所以现在有一个用户第一次注册的功能,应该为他创建地址。但它不起作用。它说已经有一些钱了。

// USER HAS NEVER USED COIN_ATLAS ( IT IS THE FIRST TIME )
public void addUser()
{
    DBObject dbUser;

    WalletAppKit kit = new WalletAppKit(TestNet3Params.get(), new File("."), "forwarding-service-testnet");
    params = TestNet3Params.get();

    Debug.Log("Wait a bit...");

    kit.startAsync();
    kit.awaitRunning();
    BlockChain chain = null;
    my_wallet = kit.wallet(); // Wallet created for that user

    try {
        chain = new BlockChain(params, my_wallet,
                new MemoryBlockStore(params));
    } catch (BlockStoreException e) {
        e.printStackTrace();
    }

    PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.addPeerDiscovery(new DnsDiscovery(params));
    peerGroup.addWallet(my_wallet);
    peerGroup.startAsync();

    Debug.Log("WALLET: " + my_wallet.currentReceiveAddress());
    Debug.Log("WALLET BALANCE: " + my_wallet.getBalance().toFriendlyString());

    my_wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
        @Override
        public void onCoinsReceived(Wallet wallet, Transaction transaction, Coin coin, Coin coin1) {
            send("NEW TRANSACTION:");
            sendWithMarkdown("FROM: " + wallet.currentReceiveAddress() + " : " + bold(coin1.toFriendlyString()));

            Coin value = transaction.getValueSentToMe(wallet);
            System.out.println("Received tx for " + value.toFriendlyString() + ": " + transaction);

            Futures.addCallback(transaction.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() {
                @Override
                public void onSuccess(@Nullable TransactionConfidence transactionConfidence) {
                    send(bold("Transaction confirmed!"));
                    send(bold(value.toFriendlyString() + " is deposited!"));
                }

                @Override
                public void onFailure(Throwable throwable) {

                }
            });
        }
    });

    peerGroup.downloadBlockChain();
    peerGroup.stopAsync();

    dbUser = new BasicDBObject("_id", bot_user.getId())
            .append("firstName", bot_user.getFirstName())
            .append("password", bot_user.getPassword())
            .append("address", my_wallet.currentReceiveAddress().toString())
            .append("BTC", (double)my_wallet.getBalance().getValue());


    collection.insert(dbUser);
}

我在输出中有什么:

WALLET: mtrjbnpq5wDzoywrKqod63tpB7ZUFrr7q5
WALLET BALANCE: 1.21503904 BTC

但是如果我在区块链中查看这个地址,它显示为 0。那么如何正确创建钱包呢?

标签: javabitcoinbitcoinj

解决方案


推荐阅读