# Manual Calculation of Transaction Size and Fee

I realize that the CLI has commands for calculating a minimum fee based on a transaction size, but I'm wanting to do a calculation manually. The documentation states that the formula for calculating the transaction minimum fee is `a * size(tx) + b` where `a` & `b` are protocol parameters, and `size(tx)` is the transaction size in bytes.

Is there a way to calculate the transaction size if I know the number of consumed and output UTxOs? Also, are `a` and `b` in the genesis file?

there are two options that do come to my mind to calculate a transaction size:

## starting from the fees

since we know that the fees are calulated as `a * size(tx) + b == fees` using some simple algebra we can see that `size(tx) == (fees - b) / a`

this should be the most reliable way, but you need the fees in order to calculate the size, so it shouldn't fit you request

## use the CBOR size

when builing a transaction using either `cardano-cli transaction build` or `cardano-cli transaction build-raw` you need to specify an `--out-file` which turns out to be a `.json` file with the following chema:

``````{
"type": "Tx AlonzoEra",
"description": "",
"cborHex": "<some hex value>"
}
``````

ultimately the `cborHex` value is what goes on-chain that value is an hexadecimal string and represents the bytes of the CBOR message

since we know that each byte can have a value from `0` to `255` the hex representation must take 2 hexadecimal digits per byte

this way you could calculate the tx size in bytes as half the length of the cborHex string

in nodejs

``````"<some hex value>".length / 2
``````

however, this approach returns usually a value smaller by ~400/~500 bytes in contrast to the first approach, so I might be missing some data

#### P.S. `cardano-serialization-library`

using the `cardano-serialization-library` it should be enough to use the `Transaction.to_bytes()` method and get the length

e.g.

``````myTx.to_bytes().length
``````

Here is my bash script

``````fee=\$(cardano-cli transaction calculate-min-fee \
--tx-body-file tx.tmp \
--tx-in-count 1 \
--tx-out-count 1 \
--mainnet \
--witness-count 1 \
--byron-witness-count 0 \
--protocol-params-file protocol.json | awk '{ print \$1 }') >> \$log
``````

In your case, it depends how many TX_in and TX_outs you will have.

• tx.tmp will create it by : cardano-cli transaction build-raw \ --fee 200000 \ --tx-in \${tx_in} \ --tx-out \${in_addr}+\${utxo_balance} \ --invalid-hereafter \$(( \${currentSlot} + 2000)) \ --out-file tx.tmp >> \$log Jan 26, 2022 at 6:25

The `a` and `b` fees parameters are protocol parameters, so you should probably check the current epoch, even they are unlikely to change suddenly.

``````\$ curl -s  -H "project_id: \$BLOCKFROST_PROJECT_ID" https://cardano-mainnet.blockfrost.io/api/v0/epochs/latest/parameters | jq '. | .min_fee_a, .min_fee_b'
44
155381
``````

``````a × size (in bytes) + b
• Thanks, you've both been helpful. I guess I probably also wasn't perfectly clear. I'm trying to figure out how to get the `size(tx)` value. I was wandering if I could do it without the CLI? I'm assuming its not the size of the ASCII characters in bytes but formatted differently. I couldn't find the exact info in the docs (although I'm sure its there somewhere). Aug 30, 2021 at 23:18