Can I use bitcoin-cli estimatefee API for Segwit? I'm using estimatefee API to calculate transaction fee for Non-Segwit.

I'm using this formula now.

size = 180 * len(inputs) * 34 * len(outputs) + 10
necessary_fee = int(size / 1000 * fee_per_kb)


Example (1 inputs, 2 outputs. They are all p2pkh)

fee_per_kb = 25000 // result of bitcoin-cli estimatefee
size = 180 * 1 * 34 * 2 + 10
necessary_fee = int(size / 1000 * 25000)

// necessary_fee =&gt; 306250 satoshi


Now I need to calculate fee for Segwit. My question is that can I use bitcoin-cli estimatefee for weight? My expectation is this like below.

fee_per_kb = 25000 // result of bitcoin-cli estimatefee
virtual_size = (Something calculation logic including segwit discount)
necessary_fee = int(virtual_size / 1000 * 25000)
size = 180 * len(inputs) * 34 * len(outputs) + 10


Unless you are using uncompressed keys (which very few wallets do today), this formula is wrong. It should be 148 instead of 180 as compressed keys are 32 bytes shorter than uncompressed keys.


  My question is that can I use bitcoin-cli estimatefee for weight?


No, you cannot. Bitcoin Core's fee estimation works on virtual size which is just the weight divided by 4. So instead you calculate the virtual size instead of the block weight and then you can use the fee estimate.