0

I am writing an API for one of my systems which can verify if a monitored address is receiving Bitcoin, I can already check if the address is on the newly received block, save it to a database for future use as UTXO, it is being managed pretty well.

I am just running into an issue. Since raw blocks do not hold the height information, how can I calculate (without using any external API) my database stored transaction confirmations? It does not make any sense also in my architecture RPC calls for that.

1 Answer 1

1

For this task, it's better to run a full-node with the transaction indexing txindex=1. Then you can get the number of confirmations from the txid of a transaction by calling:

bitcoin-cli getrawtransaction "txid" true and checking under the field confirmations. If you have jq installed you can call for example bitcoin-cli getrawtransaction 64749d0a6ab7292ed7445609e1adb8df8d67b29e344d752971699ca75bbda78e true | jq '.confirmations' and you get your answer.

In case you are not running your node with the txindex=1 option, you need to loop through the blocks to find your transaction and the number of confirmations is obtained by subtracting the block height of the transaction from the block count. You could check if the transaction is in a block like this:

bitcoin-cli getblock $(bitcoin-cli getblockhash 610321) | jq '.tx' | grep 210d8e526f4d3f72f7cdc9014ef057aa4654f64a7b1bd059f2d8dea7fcce79cd

If the command above returns anything, the transaction is in that block.

2
  • I am trying to minimize the number of calls to my RPC server since ZMQ is already notifying me when a new block arrives(along with its contents). Instead of querying the full node would I keep local storage of block hashes to check transaction confirmation? Feb 11, 2020 at 13:11
  • If you want to know the number of confirmations of a future transaction you can look for the txid of the transaction in the contents of that block, if it's present, then it got one confirmation. Then add one for every new block.
    – Pedro
    Feb 11, 2020 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.