0

Good morning, for the last few months I have been using the following formula to calculate transaction fee based on the transaction size when sending BTC from one address to multiple addresses(or batch transaction). Currently I would like to reverse this and collect my BTC from multiple different addresses and send it to my cold wallet(which is one address that will purposely collect BTC). So instead of sending from 1 to many I would like to send BTC from many to 1.

 var transactionSizeBytes = _regularInputBytes + (countOfOutputsForThisTransactionWithoutChange * _regularOutputBytes) + (_regularOutputBytes/* bytes for change output*/) + _regularOverheadBytes;
            logger.LogInformation($"Transaction size is : {transactionSizeBytes} bytes");
            logger.LogInformation($"Transaction speed {_satoshiPerByteMedium} satoshi per byte ");
            var transactionFeeSatoshi = transactionSizeBytes * _satoshiPerByteMedium;
            var transactionFeeBtc = transactionFeeSatoshi * _satoshiInBtc;
            return transactionFeeBtc;

This is the way I am calculating my fee and transaction size for the batch transaction 1 to many. I am assuming that if I want to instead send BTC from many to 1 the formula will be changed to:

var transactionSizeBytes = (countOfInputsForThisTransactionWithoutChange * _regularInputBytes) + (_regularOutputBytes/* only one output for collection*/) + _regularOverheadBytes;

where countOfInputsForThisTransactionWithoutChange will be pretty much the count of addresses I want BTC to collect from. Please correct me if this is wrong.

1 Answer 1

0

Yes, your formula is correct for small counts of inputs and outputs of a single output-script type. Note that Bitcoin uses transaction weight (and/or vsize) since segwit, but transaction weight is forward-compatible to transaction sizes of non-segwit transactions.

tx_weight = overhead_weight + (input_weight × input_count) + (output_weight × output_count)

If you are only using P2PKH (legacy single-sig outputs) that is

tx_vsize = 10 vB + (148 vB × input_count) + (34 vB × output_count)

If you have different output types, you will need to account for the different weights of outputs: enter image description here

Also, if your input count or output count exceeds 254, you have to increase the size of the overhead because the respective counter will weigh 3 vB instead 1 vB.

3
  • thank you, so will just need to do some condition that will check if input_count > 254 || output_count > 254 and if so regularOverheadBytes will be 30 instead of 10 right? I do not use Segwit atm
    – Vladimir
    Feb 16, 2023 at 15:03
  • No, it would just be +2 vB per count that is greater than 254. What library are you using to build transactions? Usually they have functions to estimate transaction size.
    – Murch
    Feb 16, 2023 at 18:42
  • 1
    just realised they also have functions to estimate the fee for a transaction ...
    – Vladimir
    Feb 16, 2023 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.