2

I am doing transaction on addresses which are created outside of my local bitcoin-core server using third party lib. I am having its private/public key. I am able to create raw transaction using "createrawtransaction" and also able to sign and send txn using "signrawtransactionwithkey" & "sendrawtransaction" but when i am trying to calculate txn fee and adding change address using "fundrawtransaction" or "walletcreatefundedpsbt", am getting error as "insufficient fund" but i have enough unspent btc on my addresses. So how we can calculate txn fee before signing and sending txn for external addresses?

2 Answers 2

1

"fundrawtransaction" and "walletcreatefundedpsbt" are wallet RPCs. They add inputs and change outputs from your wallet to aim for a specific feerate for the transaction.

These operations inherently require the relevant wallet. Change addresses are generated by the sender's wallet, and inputs have to come from the wallet of the sender. If you don't have that wallet, you simply do not have access to that information.

I don't know what advice to give you except to run these RPCs on a instance of Bitcoin Core that actually has access to the relevant wallet.

If you do things entirely manually, the fee is the difference between the inputs and outputs of the transaction. If that fee value for the transaction you have is not desirable, you'll need to create a different transaction, with either different inputs, or by adding change yourself back to your own wallet.

1

So how we can calculate txn fee before signing and sending txn for external addresses?

As a reference:

           P2PKH   P2SH-P2WPKH  P2WPKH
        -------------------------------
 Input :  592 WU     364 WU     272 WU
Output :  136 WU     128 WU     124 WU

All transactions include 40 WU by default. If your transaction spends a P2SH-P2WPKH or P2WPKH output, you will need to add an additional 2 WU. To convert from Weight Units to virtual size, divide by 4 and round up.

  • For a standard transaction spending a P2PKH input and having 2 P2PKH outputs, the weight is 40+592+136+136 = 904 WU. A standard P2PKH transaction is 904/4 = 226 vB.
  • For a standard transaction spending a P2SH-P2WPKH input and having 2 P2SH-P2WPKH outputs, the weight is 40+2+364+128+128 = 662 WU. A standard P2SH-P2WPKH transaction is 662/4 = 166 vB.
  • For a standard transaction spending a P2WPKH input and having 2 P2WPKH outputs, the weight is 40+2+272+124+124 = 562 WU. A standard P2WPKH transaction is 562/4 = 141 vB.

These amounts are calculated based on pre-segwit data having a size of 4 WU/byte and segwit data having a size of 1 WU/byte. It is best to assume that signatures will be the largest they can be (72 bytes) since their size may vary by 1 byte. We overestimate the vB size so that a fee of 1 sat/vB is enough in case the signature is in fact +1 byte larger.

P2PKH Addresses

    INPUT (592 WU)
Outpoint   (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength (4 WU): 0x6b
ScriptSig  (428 WU): OP_PUSHBYTES_72 [71-byte signature] SIGHASH_TYPE OP_PUSHBYTES_33 [33-byte pubkey]
Sequence    (16 WU): [4-byte value]

    OUTPUT (136 WU)
Value         (32 WU): [8-byte value]
ScriptLength   (4 WU): 0x19
ScriptPubKey (100 WU): OP_DUP OP_HASH160 OP_PUSHBYTES_20 [20-byte pubkey] OP_EQUALVERIFY OP_CHECKSIG

P2SH-P2WPKH Addresses

    INPUT (364 WU)
Outpoint     (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength   (4 WU): 0x17
ScriptSig     (92 WU): OP_PUSHBYTES_22 [22-byte redeemscript]
Sequence      (16 WU): [4-byte value]
Witness Data (108 WU): NUM_PUSHES_2 DATA_PUSH_72 [71-byte signature] SIGHASH_TYPE DATA_PUSH_33 [33-byte pubkey]

    OUTPUT (128 WU)
Value        (32 WU): [8-byte value]
ScriptLength  (4 WU): 0x17
ScriptPubKey (92 WU): OP_HASH160 OP_PUSHBYTES_20 [20-byte scripthash] OP_EQUAL

P2WPKH Addresses

    INPUT (272 WU)
Outpoint     (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength   (4 WU): 0x00
ScriptSig      (0 WU): N/A
Sequence      (16 WU): [4-byte value]
Witness Data (108 WU): NUM_PUSHES_2 DATA_PUSH_72 [71-byte signature] SIGHASH_TYPE DATA_PUSH_33 [33-byte pubkey]

    OUTPUT (124 WU)
Value        (32 WU): [8-byte value]
ScriptLength  (4 WU): 0x16
ScriptPubKey (88 WU): OP_0 OP_PUSHBYTES_20 [20-byte pubkeyhash]

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.