<p>To get the input values, you need to look up the values of the outputs that they spend from. So call <code>getrawtransaction</code> one each input's <code>txid</code> and then find the output at index <code>vout</code>. 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.</p>

CC BY-SA 3.0
2018-04-05T20:26:47.413Z
Does bitcoin-core make this effort for each transaction to get transaction fee?
Tailer
CC BY-SA 3.0
2018-04-05T20:32:40.243Z
@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
CC BY-SA 3.0
2018-04-05T20:34:15.627Z
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.
Ava Chow
CC BY-SA 3.0
2018-04-05T20:47:08.457Z
Yeah doing that over RPC is so expensive, if you could add input value for each input for `getrawtransaction`, could much better.
Tailer
CC BY-SA 3.0
2018-04-05T20:58:47.307Z
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.
Ava Chow
CC BY-SA 3.0
2018-04-05T21:35:05.203Z
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
CC BY-SA 3.0
2018-04-05T20:24:01.760Z
2018-04-05T20:24:01.760Z
Ava Chow
<p>I know that we have to calc inputs total minus outputs to get transaction fee, but using <code>getrawtrasnaction</code> RPC call, inputs values are not attached. how is it possible to get transaction fee?</p>

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

                    [sequence] =&gt; 4294967295
                )

        )

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

                        )

                )

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

                        )

                )

        )

    [hex] =&gt; 0200000001078c85dc34fa11363c0ebf80f74b9ba874ff9383308b6e1388465fef40f3c671000000006b4830450221009c305ffb2f04517ba1f422f22f0eded5afb855f0ee8cdd2634259774dfb0846b022019e05a9d25f21e808a0be320e3956a1e6fcb57cc4d725404db0bbab0ced78a8801210221b96462902141e297804981ebc5465e475ebf0f4f4d7a4e558edc4856b33e75ffffffff0240420f00000000001976a914e673381ba3ce7fa72520a09ee4e3ad68cd5d652888acd44c8900000000001976a914108e2c445972c2561d8cff8ee487cfe6e054ab5988ac00000000
    [blockhash] =&gt; eb31cb0c8de0e8b3219537e9304ee7d09d5dc7092ef17caed470b9a1aed2485e
    [confirmations] =&gt; 2269
    [time] =&gt; 1522693379
    [blocktime] =&gt; 1522693379
)
</code></pre>

<transactions><bitcoin-core><transaction-fees>
How to get transaction fee from raw transaction?
Tailer
# How to get transaction fee from raw transaction?

(Asked by: username_0 on 2018-04-05)

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
)

```

**Tags:** transactions, bitcoin-core, transaction-fees


---

## 1 answers

### Answer by username_1 on 2018-04-05. Score: 5 | Accepted answer

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.

> **Comments:**
> **username_0**: Does bitcoin-core make this effort for each transaction to get transaction fee?  
> **username_2**: @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.  
> **username_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.  
> **username_0**: Yeah doing that over RPC is so expensive, if you could add input value for each input for `getrawtransaction`, could much better.  
> **username_1**: 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.  
> **username_2**: 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.  

---

