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

How to get transaction fee? #1257

Open
sergeyshub opened this issue Mar 17, 2025 · 5 comments
Open

How to get transaction fee? #1257

sergeyshub opened this issue Mar 17, 2025 · 5 comments

Comments

@sergeyshub
Copy link

Can anyone point me to an example of how to get transaction miner fee with NBitcoin, given the transaction hash? I found this method:

public virtual Money GetFee(ICoin[] spentCoins)

but not sure what to pass there. Thank you!

@ajpro
Copy link

ajpro commented Mar 20, 2025

using System.Diagnostics;
using NBitcoin;
using NBitcoin.RPC;

// to determine the miner fee of a transaction just given its hash,
// we first need to get transaction data. Assume a local node is running.
// Alternatively, transactions can be pulled from online sources
// with the usual privacy caveats.

// basic RPC client setup to access local mainnet node. Adapt as needed.
var cred = new RPCCredentialString();
cred.CookieFile = "E:\\bitcoin\\.cookie";
var client = new RPCClient(cred, "http://127.0.0.1:8332", Network.Main);

// first get the transaction of interest from the node, given its hash.
// this is the transaction whose miner fees we want to calculate
// (sample value is a random recent transaction)
var txhash = "496d53eb3ad65146ca018de01b729ab923fa022d2af7928b6e1374eddb352568";
var t = client.GetRawTransaction(uint256.Parse(txhash));
Trace.Assert(!t.IsCoinBase);

// 't' itself does not contain the value of the inputs, so we need
// to fetch those from previous transactions.
// note: multiple inputs often refer to the same transaction, so it's
// possible to make this more efficient, but this is only a demo.

var spentPrevOutputs = new List<TxOut>();
foreach(var input in t.Inputs)
{
   Trace.Assert(!input.PrevOut.IsNull);
   var txin = client.GetRawTransaction(input.PrevOut.Hash);
   var spentPrevOutput = txin.Outputs[input.PrevOut.N];
   spentPrevOutputs.Add(spentPrevOutput);
}

// now we have all the spent outputs from previous transactions
// which serve as inputs to 't', so now its fee can be determined:
var fee = t.GetFee(spentPrevOutputs.ToArray());
Console.WriteLine($"Fee is {fee}");

@sergeyshub
Copy link
Author

Hi ajpro,

Thank you for this example. Does GetRawTransaction get info for any transaction? I thought it could only fetch transaction info for transactions in the mempool, or fetch info of any transaction if an additional block hash parameter is specified. If block hash is needed, which method(s) would I use to get it?

@ajpro
Copy link

ajpro commented Mar 20, 2025

GetRawTransaction can get any transaction as long as the node is running with txindex=1 (which indexes all transactions). Otherwise, it is limited to getting transactions "which are not entirely spent" (I'm assuming this means transactions referenced in the UTXO set but not sure).

Querying for transactions via block hash parameter also requires txindex=1

@sergeyshub
Copy link
Author

Will this work with a pruned node, or does the node need to store all blocks since the beginning?

Does this method apply to Litecoin, Dogecoin, and other alts?

@ajpro
Copy link

ajpro commented Mar 20, 2025

Will this work with a pruned node, or does the node need to store all blocks since the beginning?

In general, no. It would only work if outputs of the target transaction and all previous transactions that are referenced by its inputs are not "fully spent" (i.e., in the UTXO set) AND the block(s) containing those transactions have not yet been pruned.

Does this method apply to Litecoin, Dogecoin, and other alts?

I don't know, though it's reasonable to think that it would apply to Bitcoin-like alts that are supported by NBitcoin (assuming they haven't strayed too far from Bitcoin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants