Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet's 'confirmed' doesn't work #570

Closed
ales-tsurko opened this issue Aug 16, 2018 · 5 comments
Closed

Wallet's 'confirmed' doesn't work #570

ales-tsurko opened this issue Aug 16, 2018 · 5 comments

Comments

@ales-tsurko
Copy link

Hi.
I'm trying to catch 'confirmed' event for my wallet on SPV node.
I found that this event emits only for the first confirmation. But for me it doesn't work even in that way. I'm almost sure that I'm doing something wrong, but I didn't found any help for this neither in examples nor guides.
Here's how I'm trying to do this:

const bcoin = require('bcoin');
bcoin.set('testnet');

const node = new bcoin.node.SPVNode({
    config: true,
    argv: true,
    env: true,
    logFile: true,
    logConsole: true,
    logLevel: 'debug',
    db: 'leveldb',
    memory: false,
    persistent: true,
    workers: true,
    listen: true,
    loader: require,
    network: 'testnet'
});

// Temporary hack
if (!node.has('walletdb')) {
    const plugin = require('./node_modules/bcoin/lib/wallet/plugin');
    node.use(plugin);
}

process.on('unhandledRejection', (err, promise) => {
  throw err;
});

const walletdb = new bcoin.wallet.WalletDB({ memory: false, network: 'testnet', prefix: '/Users/alestsurko/.bcoin/spvchain' });

(async () => {
    await node.ensure();
    await node.open();
    await node.connect();
    await walletdb.open();

    const wallet = await walletdb.create();

    console.log('Created wallet with address %s', await wallet.receiveAddress());

    // Add our address to the spv filter.
    node.pool.watchAddress(await wallet.receiveAddress());

    node.startSync();

    node.on('tx', async (tx) => {
        console.log('------ New tx. Adding to walletdb...' + tx);
        console.log(tx);
        await walletdb.addTX(tx);
    });

    node.chain.on('block', async (block) => {
        await walletdb.addBlock(block);
    });

    wallet.on('confirmed', (tx) => {
        console.log('---------------- confirmed', tx);
    });
})().catch((err) => {
    console.error(err.stack);
    process.exit(1);
});

Please note that this issue isn't duplicate of #419 — I can't get even a single notification.

@pinheadmz
Copy link
Member

pinheadmz commented Aug 17, 2018

Hi @AlesTsurko thanks for checking out bcoin!
I played around with your script a bunch today and eventually got it to work. Below is my version, which correctly logs all events tx, confirmed and block.

The main issue here is that you sorta have created two wallets. The first is instantiated by the plugin statement towards the top, and then you also create a new walletDB right after.

In my version, I removed the second wallet, and then also included a WalletClient object from bclient. The client connects to the node's built-in (plugged-in) wallet and catches all the events (once both wallet and client are open'ed. Since this uses the built-in wallet, we probably don't have to also watch it in the pool.

For more info on the client object, check out http://bcoin.io/api-docs/#wallet.

I also switched SPV to Full Node and testnet to regtest, so I could generate blocks and test teh confirmations :-) but that shouldn't matter.

I agree its weird that your script doesn't work -- the wallet object should catch the 'confirmed' event from its txdb object... I'm not sure why, will spend some more time on this tomorrow.

const bcoin = require('bcoin');
bcoin.set('regtest');

const node = new bcoin.node.FullNode({
    config: true,
    argv: true,
    env: true,
    logFile: true,
    logConsole: true,
    logLevel: 'debug',
    db: 'leveldb',
    memory: false,
    persistent: true,
    workers: true,
    listen: true,
    loader: require,
    network: 'regtest'
});

// Temporary hack
if (!node.has('walletdb')) {
    const plugin = require('../bcoin/lib/wallet/plugin');
    node.use(plugin);
}

process.on('unhandledRejection', (err, promise) => {
  throw err;
});


const {WalletClient} = require('bclient');
const {Network} = require('bcoin');
const network = Network.get('regtest');

const walletOptions = {
  network: network.type,
  port: network.walletPort,
  apiKey: 'api-key'
}

const walletClient = new WalletClient(walletOptions);
const id = 'primary'; // or whatever your wallet name is
const wallet = walletClient.wallet(id);

(async () => {
    await node.ensure();
    await node.open();
    await node.connect();

    await walletClient.open();
    await wallet.open();

   // const wallet = await walletdb.create();
    let addr = await wallet.createAddress('default');
    addr = addr['address'];
    console.log('Created wallet with address %s', addr);

    // Add our address to the spv filter.
    node.pool.watchAddress(addr);

    node.startSync();
    node.on('tx', async (tx) => {
        console.log('------ New tx. Adding to walletdb...' + tx);
        console.log(tx);
    });

    node.chain.on('block', async (block) => {
        console.log('------ block', block);
    });

    wallet.on('confirmed', (tx) => {
        console.log('---------------- confirmed', tx);
    });
})().catch((err) => {
    console.error(err.stack);
    process.exit(1);
});

@ales-tsurko
Copy link
Author

Thanks very much, @pinheadmz!
Should I keep this issue opened because of this?

I agree its weird that your script doesn't work -- the wallet object should catch the 'confirmed' event from its txdb object... I'm not sure why, will spend some more time on this tomorrow.

@pinheadmz
Copy link
Member

pinheadmz commented Aug 17, 2018

Hi Ales, ok -- so, the wallet events are accessible, and they are catchable like this:

// this instantiates a wallet and plugs it into the node
if (!node.config.bool('no-wallet') && !node.has('walletdb')) {
  const plugin = require('../lib/wallet/plugin');
  node.use(plugin);
}

// this syntax will fire on all of these events now:

// NODE
  node.on('tx', (details) => {
    console.log(' -- node tx -- \n', details)
  });
  node.on('block', (details) => {
    console.log(' -- node block -- \n', details)
  });

// NODE MEMPOOL
  node.mempool.on('confirmed', (details) => {
    console.log(' -- mempool confirmed -- \n', details)
  });

// WALLET
  node.plugins.walletdb.wdb.on('balance', (details) => {
    console.log(' -- wallet balance -- \n', details)
  });

  node.plugins.walletdb.wdb.on('confirmed', (details) => {
    console.log(' -- wallet confirmed -- \n', details)
  });

  node.plugins.walletdb.wdb.on('address', (details) => {
    console.log(' -- wallet address -- \n', details)
  });

What's interesting is I can't get change event to fire, even using the bwallet-cli listen command. This may be unexpected behavior, and I'll continue to dive in :-)

@pinheadmz
Copy link
Member

@AlesTsurko I think you can close this issue unless you have any further questions? Thanks!

@ales-tsurko
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants