Recently I had an error, which I could never reproduce again. I don’t want this error to occur in production, but I don’t know how to fix this either or what could even be the reason for it.
First, here is what I did, in short:
- My backend generated a new address.
- I sent some funds to the address.
- Backend waited, until the funds appear in the address (by polling mempool.space API).
- Backend took the array of UTXOs of this address, iterated through this array only once, adding UTXOs to transaction inputs, until the necessary sending amount is covered.
- Backend submitted TX Hex of this transaction to mempool.space.
This resulted in the “code”: -26, “bad-txns-inputs-duplicate” error.
Google says it occurs, when a UTXO is spent multiple times in one transaction. I reviewed the code thoroughly, it is impossible that a UTXO was added into tx inputs multiple times.
Also, it was the first transaction of the wallet, so there was no previous transaction, that could spend a UTXO.
Any ideas regarding what could happen there?