Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasonzhouu/18a0ca00cc9107273ea65f6dab6857a0 to your computer and use it in GitHub Desktop.
Save jasonzhouu/18a0ca00cc9107273ea65f6dab6857a0 to your computer and use it in GitHub Desktop.
get bitcoin transaction size with RPC command

command used:

  • createrawtransaction
  • fundrawtransaction
  • signrawtransactionwithwallet
  • decoderawtransaction

network: regtest

procedure:

> createrawtransaction "[]" "{\"bcrt1q4vchswyxqtz953q8ue24zdjxag38z50sl7d3xx\":0.01}"
> 02000000000140420f0000000000160014ab3178388602c45a4407e655513646ea227151f000000000

> fundrawtransaction "02000000000140420f0000000000160014ab3178388602c45a4407e655513646ea227151f000000000"
> {
  "hex": "02000000010e67533d77574802c9e24a97079081f2fb7f0c0d7bdce32a41717137458578e80000000000feffffff02bcabf39400000000160014763cf29805c86b0684bed8da0bea1acce1bb9dae40420f0000000000160014ab3178388602c45a4407e655513646ea227151f000000000",
  "fee": 0.00002820,
  "changepos": 0
}

> signrawtransactionwithwallet "02000000010e67533d77574802c9e24a97079081f2fb7f0c0d7bdce32a41717137458578e80000000000feffffff02bcabf39400000000160014763cf29805c86b0684bed8da0bea1acce1bb9dae40420f0000000000160014ab3178388602c45a4407e655513646ea227151f000000000"
> {
  "hex": "020000000001010e67533d77574802c9e24a97079081f2fb7f0c0d7bdce32a41717137458578e80000000000feffffff02bcabf39400000000160014763cf29805c86b0684bed8da0bea1acce1bb9dae40420f0000000000160014ab3178388602c45a4407e655513646ea227151f0024730440220454d3910112e9cb9ca0cb97f63a3ac29f19f4d692a9036f0bb8e21401184db4b02205c5c05e319ea9555746c8ebefc5219d2fbc4374a172e1e7aae4fdcad58fe87a9012102e5d0576239c65457f3c6f474409f835022dcbc2f55397af1441f714804e4db1b00000000",
  "complete": true
}

> decoderawtransaction 020000000001010e67533d77574802c9e24a97079081f2fb7f0c0d7bdce32a41717137458578e80000000000feffffff02bcabf39400000000160014763cf29805c86b0684bed8da0bea1acce1bb9dae40420f0000000000160014ab3178388602c45a4407e655513646ea227151f0024730440220454d3910112e9cb9ca0cb97f63a3ac29f19f4d692a9036f0bb8e21401184db4b02205c5c05e319ea9555746c8ebefc5219d2fbc4374a172e1e7aae4fdcad58fe87a9012102e5d0576239c65457f3c6f474409f835022dcbc2f55397af1441f714804e4db1b00000000
> {
  "txid": "d37ace6662da41dedd1c348d871497f64ffabd0658e6a1ebfbf057d94feeed68",
  "hash": "74b88b46e50892fc4f99c57fbf14c37dfb48fda911dee863a02360da4ee7f808",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 0,
  "vin": [
    {
      "txid": "e8788545377171412ae3dc7b0d0c7ffbf2819007974ae2c9024857773d53670e",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "30440220454d3910112e9cb9ca0cb97f63a3ac29f19f4d692a9036f0bb8e21401184db4b02205c5c05e319ea9555746c8ebefc5219d2fbc4374a172e1e7aae4fdcad58fe87a901",
        "02e5d0576239c65457f3c6f474409f835022dcbc2f55397af1441f714804e4db1b"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 24.98997180,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 763cf29805c86b0684bed8da0bea1acce1bb9dae",
        "hex": "0014763cf29805c86b0684bed8da0bea1acce1bb9dae",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1qwc709xq9ep4sdp97mrdqh6s6ensmh8dwv9yhrv"
        ]
      }
    },
    {
      "value": 0.01000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 ab3178388602c45a4407e655513646ea227151f0",
        "hex": "0014ab3178388602c45a4407e655513646ea227151f0",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1q4vchswyxqtz953q8ue24zdjxag38z50sl7d3xx"
        ]
      }
    }
  ]
}


as showed in the result of last command:

  "size": 222,
  "vsize": 141,

reference: https://bitcoin.stackexchange.com/a/55352/81787 by Pieter Wuille

P.S. use command estimatesmartfee to get estimated transaction fees per KB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment