5

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:

  1. 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.
  2. 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 doing bitcoin-cli getaccountaddress user_1.
  3. Users can deposit/withdraw money to/from this address.
  4. 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!

4

How to do #1? i.e. How to calculate the size of the transaction (in bytes) + transaction fee in advance?

You can use the recently-introduced fundrawtransaction RPC:

  • First create a transaction with just the outputs you want but no inputs, using createrawtransaction.
  • Then call fundrawtransaction on it (which will compute fee, and add inputs and change).
  • Observe the fee, and do balance checks for your user.
  • If acceptable, sign the transaction (using signrawtransaction) and broadcast it (using sendrawtransaction).

Using bitcoin-cli settxfee seems to return true but the actual transaction fee that is applied to me was much higher. Why was this the case?

settxfee sets the fee per 1000 bytes, not the total. If your transaction ends up being larger than 1000 bytes, the fee will be higher.

I need to know more details about how the move command works. Is move an actual transaction that will incur transaction fees?

Read up on how the account system works. There is no transaction involved, as account balances are simply local bean counters. All coins in your wallet are shared across all accounts. Account balances just keep track how much of them belong to whom (but not which specific coins belong to whom). Move simply updates the balances without any transaction on the network.

Is option 2 a good option to hide/solve my problem? What are other options / correct implementation for implementing a withdraw button?

No, because the account system is deprecated and will be removed in a future version. Reasons for this is that it is very hard to understand (as this question shows!), and makes backups nearly impossible (as the account information for sendfrom cannot be recovered from the blockchain, restoring a wallet backup will mess up your accounting).

Instead, use the fundrawtransaction mechanism described above, and implement user balances in a higher-level layer (which does for example stores the account information in a database with proper backups).

Can sendfrom take out money from other accounts other than the "" account?

By definition, no. It creates a transaction using all coins in the entire wallet to fund it, and then subtracts the amount sent plus fee from the specified account.

0
  • I need to know more details about how the move command works. Is move an actual transaction that will incur transaction fees? Transfers via the move command are free!
  • Is option 2 a good option to hide/solve my problem? Looks like it masks the problem but this isn't a proper solution (because of below).
  • Can sendfrom take out money from other accounts other than the "" account? The answer to this question is unfortunately Yes.
  • What are other options / correct implementation for implementing a withdraw button?
  • 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.