NBitcoin: What is the final exact fee that will be used by TransactionBuilder in the blockchain?

I have this code:

var fallbackFeeRate = new FeeRate(Money.Satoshis(100), 1);
var feeRate = client.GetFeeRate(6, fallbackFeeRate).FeeRate;

var builder = new TransactionBuilder();
var tx = builder
  .AddCoins(coinsToSpend)
  .AddKeys(signingKeys.ToArray())
  .Send(addressToSend, amountToSend)
  .SetChange(changeScriptPubKey)
  .SendEstimatedFees(feeRate)                                                
  .BuildTransaction(true);

bool builderVerify = builder.Verify(tx);

All is good. transaction signed and verified.

Now, what is the final exact fee (and size) that will be used by TransactionBuilder, before I broadcast it?

I see tx.GetFee(coins) and feeRate.GetFee(tx)

But they are slightly different. And the size is builder.EstimateSize(tx)?

My objective is to show the real transaction fee and size before the transaction is broadcast, same as in Bitcon Core (where it will initially show the estimated fee per KB, and then the actual fee that would be used)

Thanks in advanced

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (7 by maintainers)

Most upvoted comments

Just use

var fee = tx.GetFee(builder.FindSpentCoins(tx));

This are the real fees.

The estimated and the real fee are different because it is near impossible to know the fees before having signed, but to sign you need the fees… So the transaction builder is internally trying to do its best so the two match, but they might be a bit different.

just use

var tx = Transaction.Parse(txHex, network.NBitcoinNetwork);
var allParentCoins = parentTransactions.Values.SelectMany(p => p.Outputs.AsCoins());
 var fee = tx.GetFee(allParentCoins);