2

I know that we have to calc inputs total minus outputs to get transaction fee, but using getrawtrasnaction RPC call, inputs values are not attached. how is it possible to get transaction fee?

Array
(
    [txid] => 318abc03cfa52c996b01d6ff10faf86447341d78acdf849026dd4c22ebd56cab
    [hash] => 318abc03cfa52c996b01d6ff10faf86447341d78acdf849026dd4c22ebd56cab
    [version] => 2
    [size] => 226
    [vsize] => 226
    [locktime] => 0
    [vin] => Array
        (
            [0] => Array
                (
                    [txid] => 71c6f340ef5f4688136e8b308393ff74a89b4bf780bf0e3c3611fa34dc858c07
                    [vout] => 0
                    [scriptSig] => Array
                        (
                            [asm] => 30450221009c305ffb2f04517ba1f422f22f0eded5afb855f0ee8cdd2634259774dfb0846b022019e05a9d25f21e808a0be320e3956a1e6fcb57cc4d725404db0bbab0ced78a88[ALL] 0221b96462902141e297804981ebc5465e475ebf0f4f4d7a4e558edc4856b33e75
                            [hex] => 4830450221009c305ffb2f04517ba1f422f22f0eded5afb855f0ee8cdd2634259774dfb0846b022019e05a9d25f21e808a0be320e3956a1e6fcb57cc4d725404db0bbab0ced78a8801210221b96462902141e297804981ebc5465e475ebf0f4f4d7a4e558edc4856b33e75
                        )

                    [sequence] => 4294967295
                )

        )

    [vout] => Array
        (
            [0] => Array
                (
                    [value] => 0.01
                    [n] => 0
                    [scriptPubKey] => Array
                        (
                            [asm] => OP_DUP OP_HASH160 e673381ba3ce7fa72520a09ee4e3ad68cd5d6528 OP_EQUALVERIFY OP_CHECKSIG
                            [hex] => 76a914e673381ba3ce7fa72520a09ee4e3ad68cd5d652888ac
                            [reqSigs] => 1
                            [type] => pubkeyhash
                            [addresses] => Array
                                (
                                    [0] => n2XTjk57QJgjevmYb75tJHy7AJGGeoJv2e
                                )

                        )

                )

            [1] => Array
                (
                    [value] => 0.089981
                    [n] => 1
                    [scriptPubKey] => Array
                        (
                            [asm] => OP_DUP OP_HASH160 108e2c445972c2561d8cff8ee487cfe6e054ab59 OP_EQUALVERIFY OP_CHECKSIG
                            [hex] => 76a914108e2c445972c2561d8cff8ee487cfe6e054ab5988ac
                            [reqSigs] => 1
                            [type] => pubkeyhash
                            [addresses] => Array
                                (
                                    [0] => mh2VRUG7BA8Rutz9tqTuhinqZxRo3QinGi
                                )

                        )

                )

        )

    [hex] => 0200000001078c85dc34fa11363c0ebf80f74b9ba874ff9383308b6e1388465fef40f3c671000000006b4830450221009c305ffb2f04517ba1f422f22f0eded5afb855f0ee8cdd2634259774dfb0846b022019e05a9d25f21e808a0be320e3956a1e6fcb57cc4d725404db0bbab0ced78a8801210221b96462902141e297804981ebc5465e475ebf0f4f4d7a4e558edc4856b33e75ffffffff0240420f00000000001976a914e673381ba3ce7fa72520a09ee4e3ad68cd5d652888acd44c8900000000001976a914108e2c445972c2561d8cff8ee487cfe6e054ab5988ac00000000
    [blockhash] => eb31cb0c8de0e8b3219537e9304ee7d09d5dc7092ef17caed470b9a1aed2485e
    [confirmations] => 2269
    [time] => 1522693379
    [blocktime] => 1522693379
)
3

To get the input values, you need to look up the values of the outputs that they spend from. So call getrawtransaction one each input's txid and then find the output at index vout. The value of that output is the input value. Do that for all of the inputs and sum them. That is the total input value. Sum the values of the outputs to get the total output value. Subtract the total output value from the total input value and you will get the transaction fee paid.

  • Does bitcoin-core make this effort for each transaction to get transaction fee? – Adam Apr 5 '18 at 20:26
  • 1
    @Adam as part of the validation rules, every full node needs to check that the fee of a transaction is not negative. So: yes, implicitly. – Pieter Wuille Apr 5 '18 at 20:32
  • 1
    Yes, but not in the way that you have to do it over RPC. Instead of pulling up each previous transaction, Bitcoin Core maintains an internal database of unspent transaction outputs. So it actually just pulls the UTXO from that database instead of pulling the transaction from the blockchain. – Andrew Chow Apr 5 '18 at 20:34
  • 2
    It can't be added to getrawtransaction as looking up that information for any arbitrary transaction is expensive. Bitcoin Core can do the lookup for new transactions which spends UTXOs. But once an output is spent, it becomes much more expensive to lookup that output, and maintaining a separate database for this is a waste of space and computational effort. – Andrew Chow Apr 5 '18 at 20:58
  • 1
    Expensive or impossible. Bitcoin Core can run in pruning mode, in which case it literally does not have that information anywhere. It would be highly unfortunate if people are forced to keep the whole blockchain around just to be able to compute fees. Using watch-only addresses to make the wallet track transactions you're interested in (including their fee information) is much more scalable. – Pieter Wuille Apr 5 '18 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.