Is there a way to calculate transaction fees without having to go through cardano-cli? Maybe something like Blockfrost? or any other API providers? I don't want to run a local node to do that.
3 Answers
Transaction fees are computed based on two factors: size of the transactions, and required resources (memory and CPU steps).
As Melroy van den Berg mentioned, transaction size increases the fee linearly:
fee_for_size = a * size(tx) + b
Both a
and b
are specified in protocol paramaters. You can get the latest parameters with Blockfrost using this endpoint:
/epochs/latest/parameters
min_fee_a
is the coefficient, while min_fee_b
is the constant.
Blockfrost also provides another endpoint for finding the required resources for a given transaction (I don't remember the exact names used in its response, but let's call them memory
and steps
).
And if you take another look at the protocol parameters, you may notice two other fields: price_mem
and price_step
. These are, respectively, price increases per memory and cpu steps. Therefore, you can compute your final transaction fee:
fee = fee_for_size + fee_for_execution
where:
fee_for_size = a * size(tx) + b
fee_for_execution = memory * price_mem + steps * price_step
-
1This only applies if you know your execution units. To find them out, you might want to use Blockfrost endpoint docs.blockfrost.io/#tag/Cardano-Utilities/paths/… Commented Nov 6, 2022 at 16:51
Fee calculation is actually pretty simple.
0.155381 + 0.000044 * size
Where size
is the transaction size in bytes.
-
1This does not include the Smart Contract fee. The values you used are also subject to change, Commented Aug 10, 2022 at 3:10
-
answer could be improved by explaining where those numbers come from and be linking the documentation of that process. This way users can set your comment as an answer– Nic_T2HCommented Aug 12, 2022 at 17:22
You can use TyphonJs NPM library,
https://docs.strica.io/lib/typhonjs/classes/Transaction.html#getFee
cardano-cli
to first calculate the free and then use it to submit the transaction.