<blockquote>
  <ol>
  <li>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?</li>
  </ol>
</blockquote>

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

<blockquote>
  <ol start="2">
  <li>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?</li>
  </ol>
</blockquote>

<p>I believe you have misread the information you found, or it was wrong. The inputs of a transaction must be <em>greater</em> 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.</p>

<p>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 <a href="https://bitcoin.org/en/developer-reference#fundrawtransaction" rel="nofollow noreferrer">FundRawTransaction</a> call. You can also set the transaction rate for all future transactions while the wallet runs using <a href="https://bitcoin.org/en/developer-reference#settxfee" rel="nofollow noreferrer">SetTxFee</a>.</p>

CC BY-SA 3.0
2017-07-28T00:28:48.247Z
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: https://bitcoin.stackexchange.com/questions/57193/how-to-calculate-low-medium-high-fee-transaction-fee ), so I can't use a fixed fee.
John Graham
CC BY-SA 3.0
2017-07-28T00:28:52.617Z
So the steps should be: create the transaction and use fundrawtransaction to modify the fee; right?
John Graham
CC BY-SA 3.0
2017-07-28T00:47:49.327Z
Yep that would work, then sign it as well of course, etc.
meshcollider
CC BY-SA 3.0
2017-07-27T23:22:37.670Z
2017-07-27T23:22:37.670Z
meshcollider
<p>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. </p>

<p>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:</p>

<ol>
<li>List all unspent transactions</li>
<li>Select one or more inputs to create the transaction</li>
<li>The fee is calculated as the subtraction between the total of the inputs and the outputs</li>
</ol>

<p>My doubts at this moment would be:</p>

<ol>
<li><p>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?</p></li>
<li><p>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?</p></li>
<li><p>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</p></li>
</ol>

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

<bitcoind><transaction-fees><json-rpc><raw-transaction><createrawtransaction>
How to create raw transactions with custom fees?
John Graham
# How to create raw transactions with custom fees?

(Asked by: username_0 on 2017-07-27)

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.

**Tags:** bitcoind, transaction-fees, json-rpc, raw-transaction, createrawtransaction


---

## 1 answers

### Answer by username_1 on 2017-07-27. Score: 0 | Accepted answer

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

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

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](https://bitcoin.org/en/developer-reference#fundrawtransaction) call. You can also set the transaction rate for all future transactions while the wallet runs using [SetTxFee](https://bitcoin.org/en/developer-reference#settxfee).

> **Comments:**
> **username_0**: 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: https://bitcoin.stackexchange.com/questions/57193/how-to-calculate-low-medium-high-fee-transaction-fee ), so I can't use a fixed fee.  
> **username_0**: So the steps should be: create the transaction and use fundrawtransaction to modify the fee; right?  
> **username_1**: Yep that would work, then sign it as well of course, etc.  

---

