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.
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 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
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
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.
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.