Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to get incoming transaction fee #10734

Closed
gituser opened this issue Jul 3, 2017 · 6 comments
Closed

[Question] How to get incoming transaction fee #10734

gituser opened this issue Jul 3, 2017 · 6 comments

Comments

@gituser
Copy link

gituser commented Jul 3, 2017

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.

@Seccour
Copy link

Seccour commented Jul 4, 2017

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.

@fanquake fanquake closed this as completed Oct 7, 2017
@GrayedFox
Copy link

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)

@krtschmr
Copy link

krtschmr commented Aug 6, 2019

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

@tim-tx
Copy link

tim-tx commented Oct 11, 2021

fee() {
    tx=$(bitcoin-cli getrawtransaction $1 1)
    vin=$(jq -r '.vin[] | "\(.txid) \(.vout)"' <<< $tx | while read txid vout; do bitcoin-cli getrawtransaction $txid 1 | jq -r ".vout[$vout].value"; done | paste -sd+ | bc)
    vout=$(jq '.vout[].value' <<< $tx | paste -sd+ | bc)
    printf '%0.8f' $(bc <<< "$vin - $vout")
}
$ fee e198d4b193644116d9df0b9d65c4346a9f4d6214a46ffba8c0b2fd6999eda314
0.00226000

@maflcko
Copy link
Member

maflcko commented Oct 12, 2021

You can use getblock to get the fee in recent releases of Bitcoin Core. Maybe in getrawtransaction soon, too. See #23264

@Lancelight
Copy link

Lancelight commented Dec 11, 2021

These really are not great solutions. gettransaction is where the CALCULATED fee data belongs. getrawtransaction doesnt work with pruned nodes (otherwise whats the point of pruned mode), getblock doesnt return the fee for the specific TXID we need and isnt even the right section anyway, and some sort of absurd and nearly impossible to read bash script is the very definition of "hacky". Currently fees only show for OUTGOING TXs that the wallet sent. Incoming TXs do not have this data entry in gettransaction.

@bitcoin bitcoin locked and limited conversation to collaborators Dec 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants