Repository: bitcoin
Issue: gettransaction details does not include send to myself balance changes for imported addresses
Labels: Wallet,RPC/REST/ZMQ

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current behaviour

At first, thanks for your work, Bitcoin Core team.

AFAIK, if I send some amount to myself (retrieved with `getnewaddress`), it should include both `send` and `receive` elements with the same negative and positive amount respectively. This behavior is described e.g. in github issue [#4807](https://github.com/bitcoin/bitcoin/issues/4807#issuecomment-54081380) and I can confirm that it's a fact in *some* cases.

Hovewer, the `.details` array does not include neither `send` nor `receive` elements _sometimes_. It's omitted from the output as if nothing had been sent or received.

Example:

```
$ bitcoin-cli gettransaction XXX...
{
  "amount": -0.1,
  "fee": -0.00002,
  "confirmations": 7967,
  "txid": "XXX...",
  ...,
  "details": [
    {
      "address": "SOME_RECIPIENT",
      "category": "send",
      "amount": -0.1,
      "vout": 0,
      "fee": -0.00002,
      "abandoned": false
    }
  ],
  "hex": "..."
}
```

The transaction in mempool.space or other blockchain explorers shows other outputs as well. 

```
[
  {
    "value": 0.1,
    "n": 0,
    "address": "SOME_RECIPIENT",
    ...
  },
  {
    "value": 0.05,
    "n": 0,
    "address": "MY_ANOTHER_ADDRESS_1",
    ...
  },
  {
    "value": 0.04,
    "n": 0,
    "address": "MY_ANOTHER_ADDRESS_2",
    ...
  }
]
```

Raw transaction in `.hex` element includes these of course but they are missing in the `.details` element.

Please note that both `MY_ANOTHER_ADDRESS_n` is tracked as `.mine == true` when I call `getaddressinfo MY_ANOTHER_ADDRESS_n`.




### Expected behaviour

Both `send` and `receive` balance changes with the same amount appears in the `details` section of `gettransaction` output.

### Steps to reproduce

These transactions are constructed with `sendmany` RPC call, but I am not able to reproduce them. (I tried it on testnet).

### Relevant log output

I am not sure if it is relevant, but these are log lines about that transaction:

```
2023-09-29T19:37:16Z [default] Fee Calculation: Fee:10233 Bytes:393 Tgt:0 (requested 0) Reason:"PayTxFee set" Decay 0.00000: Estimation: (-1 - -1) 0.00% 0.0/(0.0 0 mem 0.0 out) Fail: (-1 - -1) 0.00% 0.0/(0.0 0 mem 0.0 out)
2023-09-29T19:37:41Z [default] Fee Calculation: Fee:10233 Bytes:393 Tgt:0 (requested 0) Reason:"PayTxFee set" Decay 0.00000: Estimation: (-1 - -1) 0.00% 0.0/(0.0 0 mem 0.0 out) Fail: (-1 - -1) 0.00% 0.0/(0.0 0 mem 0.0 out)
2023-09-29T19:37:41Z [default] Fee non-grouped = 10233, grouped = 10233, using grouped
2023-09-29T19:37:41Z [default] CommitTransaction:
CTransaction(hash=XXX, ver=2, vin.size=5, vout.size=1, nLockTime=809912)
    CTxIn(COutPoint(..., 30), scriptSig=, nSequence=4294967293)
    CTxIn(COutPoint(..., 7), scriptSig=, nSequence=4294967293)
    CTxIn(COutPoint(..., 11), scriptSig=, nSequence=4294967293)
    CTxIn(COutPoint(..., 0), scriptSig=, nSequence=4294967293)
    CTxIn(COutPoint(..., 19), scriptSig=, nSequence=4294967293)
    CScriptWitness(...., ...)
    CScriptWitness(...., ...)
    CScriptWitness(...., ...)
    CScriptWitness(...., ...)
    CScriptWitness(...., ...)
    CTxOut(nValue=0.14500000, scriptPubKey=...)
2023-09-29T19:37:41Z [default] AddToWallet XXX...  newupdate
2023-09-29T19:37:41Z [default] Submitting wtx XXX... to mempool for relay
2023-09-29T19:37:41Z [default] MarkUnusedAddresses: Detected a used keypool item at index 121340, mark all keypool items up to this item as used
2023-09-29T19:37:41Z [default] AddToWallet XXX...  
```

### How did you obtain Bitcoin Core

Pre-built binaries

### What version of Bitcoin Core are you using?

v25.0.0

### Operating system and version

Debian 11 bullseye (oldstable)

### Machine specifications

_No response_