I'm writing an algorithm for calculating fee based on fee rate and amount to be sent (requested amount) in order to propose the fee to user;
- Sorting UTXOs ascending based on value
- Take enough UTXOs so sum is larger than requested amount
- Calculate change, add to outputs
- Build tx
- Calculate fee based on tx byte size times fee rate
- Is Change larger than calculated fee? (can we "fit" fee into change)
- If no: go back to step 2 and add more UTXOs to get to fee
- If yes: reduce the Change for fee and build final transaction
This works well in most cases, but if you have large number of UTXOs with small values and fee rate is large (e.g. 88 sat/byte), you can end up with very large transaction, i.e. very large fee for sending very small amount of coins.
I have tried to make a change in algorithm where after X number of iterations, I would give up and try to take enough UTXOs for requested amount and deduct fee from that amount and send what ever is left. But I ended up in long loop again, as UTXOs were small, transaction large and in every iteration I didn't had enough to pay the fee.
As this is obviously bad implementation, is there any other way to calculate fee bases on fee rate?