2

I want to know the size (in bytes) of a transaction before it's sent.

Is it possible? Maybe with raw transactions? or..

EDIT:

Not a duplicate. The 'duplicate' doesn't address how to find out how big it is. Just addresses how to calculate the bytes (inputs and outputs). I even commented on that thread. How do you find how many inputs and outputs there are. I understand how to calculate it. But how do I figure out if there are 10 outputs or 5 outputs?

3
  • Not a duplicate. The 'duplicate' doesn't address how to find out how big it is. Just addresses how to calculate the bytes (inputs and outputs). I even commented on that thread. How do you find how many inputs and outputs there are. I understand how to calculate it. But how do I figure out if there are 10 outputs or 5 outputs? Jul 7, 2017 at 16:16
  • 2
    I've voted to reopen. Jul 7, 2017 at 18:11
  • 2
    @MarcAlexander: Thank you for clarifying. I've also voted to reopen.
    – Murch
    Jul 7, 2017 at 21:05

1 Answer 1

4

Yes, this is actually what the raw transaction API is for.

The ordinary send RPCs (sendtoaddress and sendmany) do everything at once:

  • Construct outputs based on the addresses/amounts you want to send.
  • Do coin selection to find inputs to satisfy the amounts, determine the fee, and add change if necessary.
  • Sign the inputs with the keys in your wallet.
  • Broadcast the transaction to the network.

Since the fee depends on the size of the transaction, which depends on the coin selection, you cannot "intervene" in the middle of these RPCs.

The raw transaction API lets you do all these steps independently:

  • Constructing outputs based on addresses/amount using createrawtransaction.
  • Do coin selection to find inputs, determine the fee, and add change using fundrawtransaction.
  • Sign the inputs using signrawtransaction.
  • Broadcast the transaction using sendrawtransaction.

If you need to know the actual fee before broadcasting, you can use createrawtransaction + fundrawtransaction + signrawtransaction to construct a fully valid transaction without broadcasting. You can then inspect it, and decide to broadcast it using sendrawtransaction or not.

4
  • can you post a link for this?
    – coder123
    Dec 17, 2019 at 1:54
  • A link to what? Dec 19, 2019 at 21:02
  • the raw transaction api documentation if you know where it is?
    – coder123
    Dec 20, 2019 at 5:59
  • Seems to me that it would be useful to allow for user to input the current fees that result in acceptable delay (in satoshis per byte, as published, for example at bitcoinfees.earn.co), , and then do these steps automatically up to "signrawtransaction", caculate a transaction fee accordingly , and then as for an OK before setting the tx fee and actually broadcasting the transaction.
    – dbkeys
    Nov 1, 2021 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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