Bitcoin: [Question] How to get incoming transaction fee

Created on 3 Jul 2017  ·  3Comments  ·  Source: bitcoin/bitcoin

Hi.

I have a question I couldn't find an easy solution for it.

I'm working to automate CPFP (child pays for parent) for incoming stuck transactions and want to make sure that the new transaction will have the fee so the resulting fee per byte will be set to the current network conditions, so the stuck transaction(s) will be quickly confirmed.

The problem is I can't determine the fee of the incoming transactions arriving to the wallet via gettransaction rpc call.

Here is an example:

$ ./bitcoin-cli gettransaction yyy
{
  "amount": 0.00010000,
  "confirmations": 0,
  "trusted": false,
  "txid": "yyy",
  "walletconflicts": [
  ],
  "time": 1499083856,
  "timereceived": 1499083856,
  "bip125-replaceable": "yes",
  "details": [
    {
      "account": "user_123",
      "address": "xxxxxxxx",
      "category": "receive",
      "amount": 0.00010000,
      "label": "user_123",
      "vout": 0
    }
  ],
  "hex": "1234567"
}

Bitcoin isn't returning the fee. fee is only returned if I send outgoing transactions.

Is there any way to determine what fee was set for incoming transaction(s) (via JSON-RPC)?

Thank you.

Most helpful comment

You will need to use getrawtransaction and then do the math.

So using this txid as an example : e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314

{
    "result": {
        "hex": "0200000001f9516e774026d5cd88e1039d6ce5879ccfe5df3bc43cf4473aa5528bb0687c7d0000000069463043021f547eaf1f48630195efe1c6424e517381ee633d6d245f9dfbae8891e48f3a8f02206d68b6e82f623c2c4c90f06db8803de9e0f44fa5965cd020aa6024fae13ef5a40121033aa5441548b3ba49cc974a0e9e141ffbfcdb70df7015da3fde5f63263bb01c6efeffffff020084d717000000001976a91461281b74375a87a0ed0a63c4b8540fc8cffbbf1988acc79d2112000000001976a914941cae6b6c65cb2b45322c5a1b5c62c14fda124488ac503c0700",
        "txid": "e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314",
        "hash": "e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314",
        "size": 224,
        "vsize": 224,
        "version": 2,
        "locktime": 474192,
        "vin": [
            {
                "txid": "7d7c68b08b52a53a47f43cc43bdfe5cf9c87e56c9d03e188cdd52640776e51f9",
                "vout": 0,
                "scriptSig": {
                    "asm": "3043021f547eaf1f48630195efe1c6424e517381ee633d6d245f9dfbae8891e48f3a8f02206d68b6e82f623c2c4c90f06db8803de9e0f44fa5965cd020aa6024fae13ef5a4[ALL] 033aa5441548b3ba49cc974a0e9e141ffbfcdb70df7015da3fde5f63263bb01c6e",
                    "hex": "463043021f547eaf1f48630195efe1c6424e517381ee633d6d245f9dfbae8891e48f3a8f02206d68b6e82f623c2c4c90f06db8803de9e0f44fa5965cd020aa6024fae13ef5a40121033aa5441548b3ba49cc974a0e9e141ffbfcdb70df7015da3fde5f63263bb01c6e"
                },
                "sequence": 4294967294
            }
        ],
        "vout": [
            {
                "value": 4.00000000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "OP_DUP OP_HASH160 61281b74375a87a0ed0a63c4b8540fc8cffbbf19 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a91461281b74375a87a0ed0a63c4b8540fc8cffbbf1988ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "19ribJqiHeVrFiwWb1ogmvVGE646f4acD3"
                    ]
                }
            },
            {
                "value": 3.04192967,
                "n": 1,
                "scriptPubKey": {
                    "asm": "OP_DUP OP_HASH160 941cae6b6c65cb2b45322c5a1b5c62c14fda1244 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a914941cae6b6c65cb2b45322c5a1b5c62c14fda124488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "1EW9NCrefefLERJMZK5wAHPYGHtfwkmEvN"
                    ]
                }
            }
        ],
        "blockhash": "00000000000000000142d9a78c0097f291a028006056d3958b987bbb2ce4c391",
        "confirmations": 1,
        "time": 1499173533,
        "blocktime": 1499173533
    },
    "error": null,
    "id": null
}

Now what you need to do is to calculate the number of fees, is to look inside vin and take the vout number. So in this example the vout number is 0. And then you go to vout and add the value of all the output together.

Now you will have to do another getrawtransaction on the vin txid. Which in our example will give us this :

{
    "result": {
        "hex": "...",
        "txid": "7d7c68b08b52a53a47f43cc43bdfe5cf9c87e56c9d03e188cdd52640776e51f9",
        "hash": "7d7c68b08b52a53a47f43cc43bdfe5cf9c87e56c9d03e188cdd52640776e51f9",
        "size": 520,
        "vsize": 520,
        "version": 2,
        "locktime": 473652,
        "vin": [...],
        "vout": [
            {
                "value": 7.04418967,
                "n": 0,
                "scriptPubKey": {
                    "asm": "OP_DUP OP_HASH160 941cae6b6c65cb2b45322c5a1b5c62c14fda1244 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a914941cae6b6c65cb2b45322c5a1b5c62c14fda124488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "1EW9NCrefefLERJMZK5wAHPYGHtfwkmEvN"
                    ]
                }
            },
            {
                "value": 25.00000000,
                "n": 1,
                "scriptPubKey": {
                    "asm": "OP_HASH160 53686151ca689cc39c42c6c63c774ec76ac12856 OP_EQUAL",
                    "hex": "a91453686151ca689cc39c42c6c63c774ec76ac1285687",
                    "reqSigs": 1,
                    "type": "scripthash",
                    "addresses": [
                        "39J33vf83H4YdPvnDwiWYBzhoSWqVDhQP9"
                    ]
                }
            }
        ],
        "blockhash": "0000000000000000013602fbbd4791c0f4b7e612d11ccd4d825cc0fc34318f39",
        "confirmations": 541,
        "time": 1498901391,
        "blocktime": 1498901391
    },
    "error": null,
    "id": null
}

So here, what you have to look for is, in vout, the n value that is equal to the previous number you got, so in our case it's 0. You take the value of this output which is 7.04418967 BTC. And now to calculate the fees, you take this number minus the previous value you got which give you :

fees = 7.04418967 - (4.00000000 + 3.04192967)
fees = 0.00226 BTC

Hope this will help you out.

All 3 comments

You will need to use getrawtransaction and then do the math.

So using this txid as an example : e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314

{
    "result": {
        "hex": "0200000001f9516e774026d5cd88e1039d6ce5879ccfe5df3bc43cf4473aa5528bb0687c7d0000000069463043021f547eaf1f48630195efe1c6424e517381ee633d6d245f9dfbae8891e48f3a8f02206d68b6e82f623c2c4c90f06db8803de9e0f44fa5965cd020aa6024fae13ef5a40121033aa5441548b3ba49cc974a0e9e141ffbfcdb70df7015da3fde5f63263bb01c6efeffffff020084d717000000001976a91461281b74375a87a0ed0a63c4b8540fc8cffbbf1988acc79d2112000000001976a914941cae6b6c65cb2b45322c5a1b5c62c14fda124488ac503c0700",
        "txid": "e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314",
        "hash": "e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314",
        "size": 224,
        "vsize": 224,
        "version": 2,
        "locktime": 474192,
        "vin": [
            {
                "txid": "7d7c68b08b52a53a47f43cc43bdfe5cf9c87e56c9d03e188cdd52640776e51f9",
                "vout": 0,
                "scriptSig": {
                    "asm": "3043021f547eaf1f48630195efe1c6424e517381ee633d6d245f9dfbae8891e48f3a8f02206d68b6e82f623c2c4c90f06db8803de9e0f44fa5965cd020aa6024fae13ef5a4[ALL] 033aa5441548b3ba49cc974a0e9e141ffbfcdb70df7015da3fde5f63263bb01c6e",
                    "hex": "463043021f547eaf1f48630195efe1c6424e517381ee633d6d245f9dfbae8891e48f3a8f02206d68b6e82f623c2c4c90f06db8803de9e0f44fa5965cd020aa6024fae13ef5a40121033aa5441548b3ba49cc974a0e9e141ffbfcdb70df7015da3fde5f63263bb01c6e"
                },
                "sequence": 4294967294
            }
        ],
        "vout": [
            {
                "value": 4.00000000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "OP_DUP OP_HASH160 61281b74375a87a0ed0a63c4b8540fc8cffbbf19 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a91461281b74375a87a0ed0a63c4b8540fc8cffbbf1988ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "19ribJqiHeVrFiwWb1ogmvVGE646f4acD3"
                    ]
                }
            },
            {
                "value": 3.04192967,
                "n": 1,
                "scriptPubKey": {
                    "asm": "OP_DUP OP_HASH160 941cae6b6c65cb2b45322c5a1b5c62c14fda1244 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a914941cae6b6c65cb2b45322c5a1b5c62c14fda124488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "1EW9NCrefefLERJMZK5wAHPYGHtfwkmEvN"
                    ]
                }
            }
        ],
        "blockhash": "00000000000000000142d9a78c0097f291a028006056d3958b987bbb2ce4c391",
        "confirmations": 1,
        "time": 1499173533,
        "blocktime": 1499173533
    },
    "error": null,
    "id": null
}

Now what you need to do is to calculate the number of fees, is to look inside vin and take the vout number. So in this example the vout number is 0. And then you go to vout and add the value of all the output together.

Now you will have to do another getrawtransaction on the vin txid. Which in our example will give us this :

{
    "result": {
        "hex": "...",
        "txid": "7d7c68b08b52a53a47f43cc43bdfe5cf9c87e56c9d03e188cdd52640776e51f9",
        "hash": "7d7c68b08b52a53a47f43cc43bdfe5cf9c87e56c9d03e188cdd52640776e51f9",
        "size": 520,
        "vsize": 520,
        "version": 2,
        "locktime": 473652,
        "vin": [...],
        "vout": [
            {
                "value": 7.04418967,
                "n": 0,
                "scriptPubKey": {
                    "asm": "OP_DUP OP_HASH160 941cae6b6c65cb2b45322c5a1b5c62c14fda1244 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a914941cae6b6c65cb2b45322c5a1b5c62c14fda124488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "1EW9NCrefefLERJMZK5wAHPYGHtfwkmEvN"
                    ]
                }
            },
            {
                "value": 25.00000000,
                "n": 1,
                "scriptPubKey": {
                    "asm": "OP_HASH160 53686151ca689cc39c42c6c63c774ec76ac12856 OP_EQUAL",
                    "hex": "a91453686151ca689cc39c42c6c63c774ec76ac1285687",
                    "reqSigs": 1,
                    "type": "scripthash",
                    "addresses": [
                        "39J33vf83H4YdPvnDwiWYBzhoSWqVDhQP9"
                    ]
                }
            }
        ],
        "blockhash": "0000000000000000013602fbbd4791c0f4b7e612d11ccd4d825cc0fc34318f39",
        "confirmations": 541,
        "time": 1498901391,
        "blocktime": 1498901391
    },
    "error": null,
    "id": null
}

So here, what you have to look for is, in vout, the n value that is equal to the previous number you got, so in our case it's 0. You take the value of this output which is 7.04418967 BTC. And now to calculate the fees, you take this number minus the previous value you got which give you :

fees = 7.04418967 - (4.00000000 + 3.04192967)
fees = 0.00226 BTC

Hope this will help you out.

Given that this is (technically) publicly available information - it would be great if the fee would be calculated and then added to the result object returned by getrawtransaction - perhaps this could be done when a user has their block indexed?

This would also significantly reduce the load on a server being used to calculate fees as consumers would no longer need to hit the bitcoin node more than once for any single transaction (assuming they want to calculate fees)

yeah, i would also say the fee should be shown there

Was this page helpful?
0 / 5 - 0 ratings