<p>That depends on what those inputs and outputs look like.</p>
<p>The formula, as specified in BIP141, is:</p>
<ul>
<li>Call <strong>base_size</strong> the number of bytes needed to serialize the transaction in legacy format (which does not include the witnesses).</li>
<li>Call <strong>total_size</strong> the number of bytes needed to serialize the transaction including witnesses.</li>
<li>The <strong>weight</strong> of the transaction equals <strong>3*base_size + total_size</strong></li>
<li>The <strong>virtual size</strong> of the transaction (which is what feerates are usually expressed against) equals <strong>weight/4</strong> or <strong>0.75<em>base_size + 0.25</em>total_size</strong>.</li>
</ul>
<p>Some reference numbers:</p>
<ul>
<li>Outputs:</li>
<li>A P2PKH (1... address) output is 34 vbytes.</li>
<li>A P2SH (3... address) output is 32 vbytes.</li>
<li>A P2WPKH (bc1q... address of length 42) output is 31 vbytes.</li>
<li>A P2WSH (bc1q... address of length 62) output is 43 vbytes.</li>
<li>Inputs:</li>
<li>A P2PKH spend with a compressed public key is 148 vbytes.</li>
<li>A P2WPKH spend is 68 vbytes.</li>
<li>A P2SH-P2WPKH spend is 91 vbytes.</li>
</ul>

CC BY-SA 4.0
2019-04-25T00:38:38.150Z
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?
Tailer
CC BY-SA 4.0
2019-04-25T00:42:48.087Z
@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 + 2*31 + 2*101.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
CC BY-SA 4.0
2019-04-25T00:51:49.933Z
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
Tailer
CC BY-SA 4.0
2019-04-25T00:53:38.583Z
@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
CC BY-SA 4.0
2019-04-25T01:29:07.163Z
@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
CC BY-SA 4.0
2019-04-25T01:54:09.143Z
as you see on blockstream they did like convert vbytes to byte, how did they do so?
Tailer
CC BY-SA 4.0
2019-04-25T02:17:58.410Z
@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
CC BY-SA 4.0
2019-04-25T02:19:31.547Z
Thanks a lot for your help, I understand that, but I was just wondering how blockchain and others do so.
Tailer
CC BY-SA 4.0
2019-04-25T02:24:50.043Z
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
CC BY-SA 4.0
2021-12-21T15:16:11.110Z
where does 101.25 vByte comes from because as Pieter Wullie said a "A P2WPKH spend is 68 vbytes" so no one said anything about 101.25, where did it come from? and what's the different about these two inputs.
H.AHM
CC BY-SA 4.0
2019-04-24T23:10:56.320Z
2021-12-21T17:14:44.523Z
2021-12-21T17:14:44.523Z
Pieter Wuille
<p>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?</p>

<transaction-fees><segregated-witness>
How to calculate segwit transaction fee in bytes?
Tailer
# How to calculate segwit transaction fee in bytes?

(Asked by: username_0 on 2019-04-24)

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?

**Tags:** transaction-fees, segregated-witness


---

## 1 answers

### Answer by username_1 on 2019-04-24. Score: 13 | Accepted answer

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 148 vbytes.
* A P2WPKH spend is 68 vbytes.
* A P2SH-P2WPKH spend is 91 vbytes.

> **Comments:**
> **username_0**: 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?  
> **username_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 + 2*31 + 2*101.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.  
> **username_0**: 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  
> **username_1**: @Adam Stop looking at transaction size, as you've been told elsewhere. The relevant metric for feerate is either virtual size or weight.  
> **username_1**: @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  
> **username_0**: as you see on blockstream they did like convert vbytes to byte, how did they do so?  
> **username_1**: @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.  
> **username_0**: Thanks a lot for your help, I understand that, but I was just wondering how blockchain and others do so.  
> **username_1**: 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.  
> **username_2**: where does 101.25 vByte comes from because as username_1 Wullie said a "A P2WPKH spend is 68 vbytes" so no one said anything about 101.25, where did it come from? and what's the different about these two inputs.  

---

