That's what I had in mind, thanks for writing this down precisely.
My latest concern is that when I'll do this:
Then, loop through the vins and do another lookup on each of them
bitcoind might complain that it cannot find the transaction (as it is not linked to my wallet addresses in any way)
I couldn't test yet if it works, if it works only with "txindex=1", if does not work at all (I fear once all outputs are spent the information won't remain anywhere locally)... I'll try soon.
I've recently done a similar thing for a bitcoin project.
You'll have to set txindex=1 and then run bitcoind again with the -reindex flag. When you use getrawtransaction again, it will work on all possible transactions, regardless if all the outputs have been spent or not.
Then, as kjj mentioned, loop through vouts (add them up) and then lookup each vin using getrawtransaction on the txid. However, you might be confused on how to find the exact output amount for your vin. The vout parameter in the vin is a reference to which output it refers to from your input.
Look below:
"vin": [
{
"txid": "2da6ea77348c20885a7d80969bc67eadbb41bbcd817cd154c85f84979661a248",
"vout": 1,
"scriptSig": {
"asm": "3045022046778cb18a3dbec25959e9426de06219241233ebe173db8b1b090f7162daa7ec022100ab5bc2ced0f77046c5aba10000a1884dc1dcdc3991ab3106f6ac664ef7b4add801 023763e069253c6dfeab973e069d4f2461abac442135bf8ffd484ff0e7ca3f9762",
"hex": "483045022046778cb18a3dbec25959e9426de06219241233ebe173db8b1b090f7162daa7ec022100ab5bc2ced0f77046c5aba10000a1884dc1dcdc3991ab3106f6ac664ef7b4add80121023763e069253c6dfeab973e069d4f2461abac442135bf8ffd484ff0e7ca3f9762"
},
"sequence": 4294967295
}
],
The vout is 1. Now if you run a getrawtransaction on that txid you'll see this in the vout:
"vout": [
{
"value": 0.9,
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 60d602d14e71717373101521eafb5c1f8f8904d3 OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a91460d602d14e71717373101521eafb5c1f8f8904d388ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"19q2FAM1MkdBHiUvhvQheh2RvbpUEZ3xx4"
]
}
},
{
"value": 4.7305,
"n": 1,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 694239b7dcd7b05f291add07bcf98c5d92e4ab64 OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a914694239b7dcd7b05f291add07bcf98c5d92e4ab6488ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"1AbZHZwa9oAbVpiWsESrozUwNwx4H3kHFY"
]
}
}
],
n:1 is the one you're looking for. Thus the value is 4.7305.
Now add up all the values for all the vins of the original transaction and minus the vouts = fee.