How to calculate segwit transaction fee in bytes?

1

I'm trying to calculate SegWit transaction fees in bytes, I have two Segwit inputs and 2 segwit outputs. can anyone calculate this transaction fees if I'm paying 100 Sat per byte?

Adam

Posted 2019-04-24T22:48:57.170

Reputation: 3 215

Answers

5

That depends on what those inputs and outputs look like.

The formula, as specified in BIP141, is:

  • Call base_size the number of bytes needed to serialize the transaction in legacy format (which does not include the witnesses).
  • Call total_size the number of bytes needed to serialize the transaction including witnesses.
  • The weight of the transaction equals 3*base_size + total_size
  • The virtual size of the transaction (which is what feerates are usually expressed against) equals weight/4 or 0.75*base_size + 0.25*total_size.

Some reference numbers:

  • Outputs:
    • A P2PKH (1... address) output is 34 vbytes.
    • A P2SH (3... address) output is 32 vbytes.
    • A P2WPKH (bc1q... address of length 42) output is 31 vbytes.
    • A P2WSH (bc1q... address of length 62) output is 43 vbytes.
  • Inputs:
    • A P2PKH spend with a compressed public key is 149 vbytes.
    • A P2WPKH spend is 68 vbytes.
    • A P2SH-P2WPKH spend is 93 vbytes.

Pieter Wuille

Posted 2019-04-24T22:48:57.170

Reputation: 54 032

Thanks for your expensive explanation, if I have 2 P2WPKH inputs and 2 P2WPKH output, how can I estimate the fees for this transaction? also practically can we convert vbytes to legacy bytes?Adam 2019-04-25T00:38:38.150

1@Adam You can't convert vbyte to bytes as they're independent metrics. Thankfully, legacy bytes are irrelevant for fee calculations. If you have two 2 P2WPKH outputs and inputs, you'll have 10 + 231 + 2101.25 = 274.5 vbytes. Whatever fee you're paying divided by 275 (rounding 274.5 up) gives you the fee/vbyte metric, which is what matters for miners.Pieter Wuille 2019-04-25T00:42:48.087

Thanks again for your explanation, as I see in this transaction on blockchain they says transaction size in bytes 191, do you know how did they calculate that? https://www.blockchain.com/btc/tx/87e4a93012116020d7c385d92a0d307363e8040bfb89800c709f66c47ca58316

Adam 2019-04-25T00:51:49.933

@Adam Stop looking at transaction size, as you've been told elsewhere. The relevant metric for feerate is either virtual size or weight.Pieter Wuille 2019-04-25T00:53:38.583

@Adam blockstream.info makes it very confusing by showing you fee/B (which is irrelevant) and not showing vsize. blockstream.info correctly tells you the transaction is 110 vbytes, 138.1 sat/vbyte: https://blockstream.info/tx/87e4a93012116020d7c385d92a0d307363e8040bfb89800c709f66c47ca58316

Pieter Wuille 2019-04-25T01:29:07.163

as you see on blockstream they did like convert vbytes to byte, how did they do so?Adam 2019-04-25T01:54:09.143

@Adam How many times do I need to repeat this? No, you cannot convert vbytes into bytes. They compute bytes by just counting the bytes. They count the vbytes by using the formula in my answer.Pieter Wuille 2019-04-25T02:17:58.410

Thanks a lot for your help, I understand that, but I was just wondering how blockchain and others do so.Adam 2019-04-25T02:19:31.547

They literally look at the size of the transaction. I'm afraid I can't explain this if it's non-obvious, but it's literally about the simplest operation there is on a transaction. As explained, the size is almost mostly irrelevant.Pieter Wuille 2019-04-25T02:24:50.043