How to calculate transaction fee?

  1. If I want to create a transaction which includes multiple input cells and output cells. How to calculate the fee of this transaction?

  2. If the transaction is simply a transfer, does dependent cell influence the transaction fee or not?

PS: I have already read this documentation thoroughly: https://github.com/nervosnetwork/rfcs/blob/45f83638f08da3a8c7a951db75c05fcbae59be8b/rfcs/0002-ckb/0002-ckb.md

I don’t think your answer solves my question. My question is how to calculate transaction fee if the transaction includes multiple input and output cells.

fee = total input - total output

Yes, fee = total inputs - total outputs is correct. However, my question is how to calculate transaction fee to make it enough to be broadcasted successfully but not too much? Considering the transaction contains multiple inputs and outputs.

There are three ways:

  1. There are a default option min_fee_rate in the ckb.toml: 1000 Shannons/KB. For a typical transfer tx which contains two inputs and two outputs, the tx size is 597 bytes, to calculate tx fee we need plus extra 4 bytes for the serialization consumption; so the min tx fee is (597 + 4) * 1000 / 1000 = 601 shannons. You can send a tx with 601 shannons fee, and try to increase the fee if the tx can’t get committed. https://github.com/nervosnetwork/ckb/blob/develop/resource/ckb.toml#L109

  2. CKB has an RPC to estimate tx fee by statistic the average committed time of txs, but since the current mainnet has very few transactions been committed, so it may not accuracy https://docs.nervos.org/api/rpc.html#estimate_fee_rate

  3. For some special txs that consume massive cycles, we can’t just estimate it by the tx size, you need to pay more fees to get committed fast.

If you want to write a wallet or such tools, the advice is to combine these strategies to find a proper fee.

Just remember we have a wiki document for transaction fee https://github.com/nervosnetwork/ckb/wiki/Transaction-»-Transaction-Fee

2 Likes

I see. Quote from your answer, how many bytes does one input consume? And what about an output?

A input occupied 8 + 4 + 4 = 16 bytes; an output size is depend on its construction, for a typical output occupied 61 bytes.

Based on your previous answers, how do you get the result that a transaction contains 2 inputs and 2 outputs would have size of 597 bytes if an input only occupies 16 bytes and an output only 61 bytes?

A transaction has other fields, to do verification it needs reference to lock script code cell, and the serialization itself takes few bytes., You can use SDK to deserialize tx and see it youself.

dao withdraw transaction is an exception. You need to add secondary reward (use calculate_dao_maximum_withdraw rpc to calculate) to input side.