Get Confirmation detail of TRX hash - always returns 0

HI All

Running Bitcore 4.1

When we run the GETConfirmation https://github.com/bitpay/bitcore-node/blob/master/lib/services/bitcoind.js#L1270 - bitcore node 3.1.1 we always get “0” as response - which is incorrect.

Any advice would be appreciated

Thanks

Where is that being called from?

var bitcore = require('bitcore-node');
var Insight_Api = require('insight-api');
//Services
var Bitcoin = bitcore.services.Bitcoin;
var Web = bitcore.services.Web;
var node = new bitcore.Node({
    network: 'livenet',
    services: [
        {
            name: 'bitcoind',
            module: Bitcoin,
            config: {
                spawn: {
                    "datadir": "/root/.bitcore/data",
                    "exec": "/root/.nvm/versions/v0.12.10/lib/node_modules/bitcore/node_modules/bitcore-node/bin/bitcoind"
                }
            }
        },
        {
            name: 'web',
            module: Web,
            config: {
                port: 3001
            }
        },
        {
            name: 'insight-api',
            module: Insight_Api,
            config: {
                port: 3001
            }
        }
    ]
});
node.start(function (err) {
    if (err) {
        console.log("Start Error", err)
        node.stop(function () {
            console.log('shutdown the node')
        });

    }
});

node.on('ready', function () {
    console.log('Bitcoin Node Ready');
    MongoClient.connect(config.db11, function (err, db) {
        assert.equal(null, err);
        console.log("Connected correctly to Db");
        var mainDb = db.collection('transactions_re');
        mainDb.createIndex({"hash": 1}, {unique: true});

        node.services.bitcoind.on('tx', function (transactionBuffer) {
            var trx = bitcore_lib.Transaction().fromBuffer(transactionBuffer);

            node.services.bitcoind.getDetailedTransaction(trx.hash, function (err, transaction) {
               
                console.log(node.services.bitcoind._getConfirmationsDetail("fb3cb67fbaaba012ff0e9ea0fa7daca9060ea1c4d1091a33f18c929c19c48aac"));
            });
        });

    })

});

node.on('error', function (err) {
    console.error(err);
});

The methods with underscores are not intended to be used externally, and the method accepts a transaction (with a height property), and not the hash of a transaction. Please see: https://github.com/bitpay/bitcore-node/blob/master/lib/services/bitcoind.js#L1270-L1280

The confirmations can be calculated using:

var confirmations = node.services.bitcoind.height - transaction.height + 1;

For readability, I’ve edited the code block above to have three backticks before and after so it displays properly.

Hi All,

We have tried the following but again we got only “0”:

for(var i = 0; i< inputs.length; ++i){
  node.services.bitcoind.getTransaction(inputs[i].prevTxId, function(err, transaction) {
    var tx = new Transaction(transaction)
    console.log(tx.toObject());
    console.log("confirmation:", node.services.bitcoind._getConfirmationsDetail(tx.toObject()));
  });
}

Also, when I want to calculate height, using:

var confirmations = node.services.bitcoind.height - transaction.height + 1;

I get transaction height by default.

Please advice,
Thanks.

Edited the above code block for readability.

The result from getTransaction does not have a height property.

Thanks for answer,

Could you tell which function result has height?

HI
Just to clarify - which function would have a height property that we can use to get the confirmations ?

Thanks

getDetailedTransaction: https://github.com/bitpay/bitcore-node/blob/master/lib/services/bitcoind.js#L1854-L1896

Hi Braydon,

Thank you for your reply. That is very true that function has height but the problem is from time to time I get the following error:

`AssertionError: null == { [RPCError: Bitcoin JSON-RPC: Work queue depth exceeded] code: 429 }
    at /root/myNode/app.js:176:40
    at /root/myNode/node_modules/async/lib/async.js:726:13
    at /root/myNode/node_modules/async/lib/async.js:52:16
    at done (/root/myNode/node_modules/async/lib/async.js:241:17)
    at /root/myNode/node_modules/async/lib/async.js:44:16
    at /root/myNode/node_modules/async/lib/async.js:723:17
    at /root/myNode/node_modules/async/lib/async.js:167:37
    at /root/myNode/app.js:158:46
    at /root/myNode/node_modules/async/lib/async.js:52:16
    at done (/root/myNode/node_modules/async/lib/async.js:241:17)

Could you advice please?
`

also I have get flowing error.
TypeError: Cannot read property ‘toString’ of null
at Array.types.str (/root/myNode/node_modules/bitcore-node/node_modules/bitcoind-rpc/lib/index.js:260:17)
at RpcClient. (/root/myNode/node_modules/bitcore-node/node_modules/bitcoind-rpc/lib/index.js:236:35)
at /root/myNode/node_modules/bitcore-node/lib/services/bitcoind.js:1956:14
at retry (/root/myNode/node_modules/bitcore-node/lib/services/bitcoind.js:443:5)
at /root/myNode/node_modules/bitcore-node/node_modules/async/lib/async.js:651:21
at /root/myNode/node_modules/bitcore-node/node_modules/async/lib/async.js:718:13
at iterate (/root/myNode/node_modules/bitcore-node/node_modules/async/lib/async.js:262:13)
at async.forEachOfSeries.async.eachOfSeries (/root/myNode/node_modules/bitcore-node/node_modules/async/lib/async.js:281:9)
at _parallel (/root/myNode/node_modules/bitcore-node/node_modules/async/lib/async.js:717:9)
at Object.async.series (/root/myNode/node_modules/bitcore-node/node_modules/async/lib/async.js:739:9)
root@localhost:~/myNode#

In my case this code node.services.bitcoind.height is not that accurate because its not getting the latest block. I accomplished this using getInfo’s block.

For Example (Getting Confirmations):

function getConfirmation(args, opt, callback) {
  node.services.bitcoind.getDetailedTransaction("<hash here>", function(err, transaction) {
    var txHeight = 0;
    if(err) {
      //return error message here ...
    } else {
      node.services.bitcoind.getInfo(function(err, info) {
        if(transaction.height + 1 > 0) {
          txHeight = info.blocks - transaction.height + 1
        }
        // return total confirmation(s)
        callback(null, (err ? err : {
          hash : args,
          confirmation : txHeight
        }))
      })
    }
  });
}

txHeight has the total amount of the confirmation…

Maybe this will help… :slight_smile: