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

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

<p>In case you are not running your node with the <code>txindex=1</code> 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:</p>

<pre><code>bitcoin-cli getblock $(bitcoin-cli getblockhash 610321) | jq '.tx' | grep 210d8e526f4d3f72f7cdc9014ef057aa4654f64a7b1bd059f2d8dea7fcce79cd
</code></pre>

<p>If the command above returns anything, the transaction is in that block.</p>

CC BY-SA 4.0
2020-02-11T13:11:33.597Z
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?
Allan Romanato
CC BY-SA 4.0
2020-02-11T15:06:09.963Z
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
CC BY-SA 4.0
2020-02-10T18:15:38.673Z
2020-02-10T18:23:47.857Z
2020-02-10T18:23:47.857Z
Pedro
<p>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.</p>

<p>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.</p>

<bitcoin-core><blockchain><block>
Calculate Transaction Confirmations
Allan Romanato
# Calculate Transaction Confirmations

(Asked by: username_0 on 2020-02-10)

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.

**Tags:** bitcoin-core, blockchain, block


---

## 1 answers

### Answer by username_1 on 2020-02-10. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  

---

