I'm trying to make a bitcoin website and I tried to implement a sort of 'withdraw' button which would allow you to send your money in the wallet address to some other address. The problem I am facing is that post-transaction it is taking making the address balance negative.
This is the setup:
- There is bitcoind running in the background so I have one wallet on the server. It has a default account name of
""
(empty string) with some address. - I generate a new address for each user in the wallet. Let's say each user gets his/her own account named
"user_1"
,"user_2"
, etc. I generate addresses by basically doingbitcoin-cli getaccountaddress user_1
. - Users can deposit/withdraw money to/from this address.
- If I withdraw too much money from this address it'll make the address balance negative and I presume it is also taking out coins from the master account, i.e. the
""
account.
For the transfers I am using the following command bitcoin-cli sendfrom user_1 <some_other_bitcoin_address> <amount> <min_confirmations> <comment> <comment_to>
. If I truly pick more money than is available in the wallet then it'll error and tell me that I don't have enough balance. But in a certain edge case it goes through and make the address balance negative.
I figure that there are two approaches to this solution:
Option 1: Calculate the precise transaction fee and based on that fee make a check.
Option 2: Use
bitcoin-cli move "" "user_1" <amount_that_is_negative>
so it seems that the balance it not actually negative and that the site owner has decided to pay for the excess fee.
So basically, I need to solve the negative address balance problem and could also some clarification on the following:
- How to do #1? i.e. How to calculate the size of the transaction (in bytes) + transaction fee in advance?
- Using
bitcoin-cli settxfee <amount>
seems to return true but the actual transaction fee that is applied to me was much higher. Why was this the case? - I need to know more details about how the move command works. Is move an actual transaction that will incur transaction fees?
- Is option 2 a good option to hide/solve my problem?
- What are other options / correct implementation for implementing a withdraw button?
- Can
sendfrom
take out money from other accounts other than the""
account?
Thanks!