As it has been discussed before, it is very inneficient to find which transaction has spent a transaction output using bitcoin core alone. I found a way of doing it with an electrumx server running on top of bitcoin core generally under 1 second for an arbitrary output. I explain my method below:
- Find the address (or ScriptPubKey) of the output in question and at which height the output has been confirmed - for example, for the second output of
443e9b7fd1ede11570daefc5b10d35e4179b8237789046dfb93dec9148ecb81ait would be
1FkY6Gdhi9gCVQWWaSqDnxbpxgiBXUhasxand height 452100.
- Query electrumx for the address/scriptPubKey
electrumx_rpc query 1FkY6Gdhi9gCVQWWaSqDnxbpxgiBXUhasx Address: 1FkY6Gdhi9gCVQWWaSqDnxbpxgiBXUhasx History #0: height 452,100 tx_hash 443e9b7fd1ede11570daefc5b10d35e4179b8237789046dfb93dec9148ecb81a History #1: height 452,100 tx_hash 9fe1566b53d5b20e2a97a397ba874d0b8cac2f3f4c9fc28e1594758607bf196e History #2: height 475,195 tx_hash c472dae735bf2c1c430dbbbeb8f77a907f0caf78d125693cd667e313d66ad1b5 History #3: height 478,291 tx_hash a873cf2c628c5d116f5e2f4759fd02ddf5cca3eb786249bc5967655a05785573 No UTXOs found Balance: 0 BTC
If the txid of the original output appears in the UTXO section of the previous results, it hasn't been spent - in this case
443e9b7fd1ede11570daefc5b10d35e4179b8237789046dfb93dec9148ecb81ais not there, "No UTXOs found"
If there are no UTXOs (balance = 0) and there are only two transactions in the history, the other transaction has spent it - not the case here.
- In the remaining cases, loop through all the historical transactions starting at the height of the output and search for the txid&index that identify the output until found - in this example, run
bitcoin-cli getrawtransaction $historicalTx | grep 1ab8ec4891ec3db9df46907837829b17e4350db1c5efda7015e1edd17f9b3e4400000001
It's a pity that electrum doesn't allow to query txids or outputs.
My question is: is there a more efficient way of doing this with any open-source software?