# How to create raw transactions with custom fees?

I have a website where users can send and withdraw funds using Bitcoin. I would like to give them the possibility to use different fees (low, medium and high) instead of a fixed fee or to let bitcoind choose the fee. I can't setup a default fee or be changing it at runtime because then if a user decides to use a high fee and a lower one, it could happen that the user with the high fee is assigned as low or vice versa.

Therefore, after researching I have come to the conclusion that it is necessary to build the transaction from the UTXOS that my node has. However, it is difficult for me to finish understanding how to carry out this task. I will give an example to explain my problem: If the node UTXOS is 1BTC, a user has 0.1 BTC in the service wallet and wishes to withdraw 0.07 BTC. I understand that the steps to be taken would be the following:

1. List all unspent transactions
2. Select one or more inputs to create the transaction
3. The fee is calculated as the subtraction between the total of the inputs and the outputs

My doubts at this moment would be:

1. If I understand correctly, for each incoming transaction we have, we will have an unspent transaction that will allow us to spend the available funds; Then, as long as you have in a database the correct balance to each user (updating that balance with the corresponding fees when withdrawing funds), so I should only use the same UTXO generated by each user when depositing btcs Or is it possible to use other UTXOs as long as the quantity is the same?

2. I have read in bitcointalk that the inputs of a raw transaction must be less than or equal to the outputs of that transaction. Is it not possible to use inputs higher than the outs and "return" the surplus funds again to a wallet of my node?

3. How can I calculate the fee used? I mean, I can decide that I want to use X satoshis/kb but how can I calculate the fee that the transaction will need before I create it? Because if I understood correctly, the size of the transaction I get once I have created such transaction. I do not know if there is a way to perform the calculation that the transaction will occupy in bytes / kilobytes

I have not found too much information about it, so if anyone can help me I would appreciate it.

1. so I should only use the same UTXO generated by each user when depositing btcs Or is it possible to use other UTXOs as long as the quantity is the same?

This is up to you, although it would probably be safer to only spend the users deposit UTXOs to make sure nothing has gone wrong (e.g. private key leak and the coins have ready been spent). But as long as the user gets the right amount of bitcoin at the end, it doesn't really matter.

1. I have read in bitcointalk that the inputs of a raw transaction must be less than or equal to the outputs of that transaction. Is it not possible to use inputs higher than the outs and "return" the surplus funds again to a wallet of my node?

I believe you have misread the information you found, or it was wrong. The inputs of a transaction must be greater than or equal to the outputs, otherwise you would be somehow generating new coins. If the inputs are strictly greater, the difference is given to the miners of the transaction as a fee. To "return" the surplus funds, you need to simply make a transaction output which sends the coins back to you, known as change.

If you generate a new transaction manually using bitcoin RPC calls, it will automatically estimate the fee itself. To use a custom fee rate, you can set the feeRate in the FundRawTransaction call. You can also set the transaction rate for all future transactions while the wallet runs using SetTxFee.

• Thanks for your answer! It really helped me. Just a couple of questions and I will mark your answer as valid. About the point 2 (ins and outs) I misread the info, my bad. And thanks for tell me about fundrawtransaction, I didn't know about it, it seems very useful but I want to give the user the possibility to use custom fees (feel to free to check my another post which is related: bitcoin.stackexchange.com/questions/57193/… ), so I can't use a fixed fee. Jul 28, 2017 at 0:28
• So the steps should be: create the transaction and use fundrawtransaction to modify the fee; right? Jul 28, 2017 at 0:28
• Yep that would work, then sign it as well of course, etc. Jul 28, 2017 at 0:47