# Handling change addresses without using rawtransactions?

Since I'm using the wonderful accounts feature which is soon to be deprecated (shame on me, i know - but i simply didn't have resources and time as a single person to do an internal accounting system in a few weeks alongside everything else the project contains, this is a school project and will not be deployed), i was wondering how to handle sending transactions & change addresses, without using raw transactions.

Would the following work

SendMany(fromacc, toaddress: amount, 1, "comment", subtractfee)

(I'm planning to use SendMany for single transactions, because it has the subtractfee parameter compared to SendFrom).

Use the resulting TXID to query Blockcypher API (https://api.blockcypher.com/v1/btc/main/txs/b7bf53a74a4e4cb61ab523cc002fc4cea83fb359471d02b212d947cf86fe6077) and "grab" the change address from there (Since Bitcoin Core doesn't allow this wihout using rawtransactions?)

Which means that GetBalance(account) should show the correct balance of the account and not 0?

Or should i really not do that and use rawtransactions instead? :-)

edit:

So i just realised that i confused the subtractfee parameter in SendMany from the one in SendToAddress (which is what i need), so i suppose i need to use that instead.

would using:

$balance = getBalance($myaccount)
move("", $myaccount,$sendingamount, "", "comment")
SendToAddress($receiveaddr,$sendingamount, "comment", "commentto", 1, 0, 6)

do the the blockcypher grab TX id, verifyaddress and set account here

OR

$newBalance = getBalance($myaccount)
$change =$balance - $sendingamount move("",$myaccount, \$change, "", "comment")

Work in this case and would listunspent show the amount under the right account (not "")?

Also here is what I'm trying to do from a user perspective: https://imgur.com/a/IHrF4vI

• Why do you need to know the change address? – Andrew Chow May 16 '18 at 16:24
• Instead of asking about the approach that you want to do, you should ask about what it is that you want to accomplish. Otherwise this is an XY problem (xyproblem.info) and it doesn't really make sense to people who can provide you an answer. As it is right now, I don't understand what you are trying to do and what the problem actually is so I cannot provide you a good answer. – Andrew Chow May 16 '18 at 16:28
• Yeah, i apologize. I've been quiet confused about sending transactions and the heat has gotten to me today. I would like for a user with an account to be able to transfer what is available in their balance, that they have received from a generated address. The fee should be deducted from what they're sending, so the receiver gets less. The change address which receives the change should be accessable by the account and should somehow be able to be shown in their balance. So if i send 1BTC of a balance of 2BTC, i would like for my balance to show 1BTC, not 0BTC. – Peter Dalby May 16 '18 at 17:01
• I suppose i have to use raw transactions to accomplish this and directly specify which inputs to use for the transaction? i just heard that it's very likely to make mistakes when using this, and it's for advanced use only. I'd like to keep it as simple as if at all possible... :-) – Peter Dalby May 16 '18 at 17:07
• Also in the example the 2BTC balance should be considered one input which would mean that the entire balance would be empitied for the user when trying to send some of it. I need to show the expected balance :-) – Peter Dalby May 16 '18 at 17:38

It sounds like there is a misunderstanding of how accounts work (which is not surprising given that the accounts system is very unintuitive).

First of all, addresses are only tied to accounts for receiving, not sending. So when you send from an account, any input associated with any address in the wallet can be selected for the transaction. During that send, the account's balance is deducted by exactly the amount that was sent. Change goes back into the wallet and has no effect on the balance as it was not sent from the account.

The account field in listunspent is slightly misleading as outputs are really not associated with accounts at all. When you send from an account, ANY output can be chosen as inputs for the transaction, not just ones that are part of the account.

When sending, because you know the recipients address, you can use the subtractFeeFrom parameters of sendmany or sendtoaddress to specify that the fee will be subtracted from the specified addresses (i.e. the recipients). This should still deduct from the account balance.

• Thank you Andrew for clarifying. I really appreciate your efforts to helping the Bitcoin community. One thing i still need to get clarified "During that send, the account's balance is deducted by exactly the amount that was sent". I suppose i have to use sendmany then, since i don't understand how the users accounts balance can be deducted by the exact amount when nothing specifies it to with SendToAddress, which uses default "" account? Can you elobarote on the subtractFeeFrom parameter in sendmany? With sendmany you specify the receipient address, which pays the fee instead? – Peter Dalby May 16 '18 at 18:25
• also what if i leave the subtractFeeFrom parameter empty, will it then deduct from the amount specified instead, so the sender pays the fee? From rpc documentation it seems that "If no addresses are specified here, the sender pays the fee." – Peter Dalby May 16 '18 at 18:29
• Last thing; how should i check what the account is actually able to maximumly use/spend, currently in the UI it's just based on the getBalance(account) Should i in this case use listunspent and filter based on what is inside the account and the corresponding balance to then add them up? or perhaps loop through all generated addresses, for example: listunspent 0 9999999 "[\"3QsuwHtXsWP8vLFZiGL2VK4MEf93YRuVkk\", etc, etc, etc]" – Peter Dalby May 16 '18 at 18:36
• Right, if you want to use accounts, you have to use sendmany or sendfrom. With sendmany, you specify the address(es) that the fee will be deducted from. Those addresses must be recipients and their outputs will have an equal amount subtracted in order to pay for transaction fees. If you do not specify subtractfeefrom, then the recipients will pay the full amount and the sender sends a little bit more to cover the transaction fees. What do you mean by "maximumly use/spend"? Again, the accounts and their balances are not tied to outputs (so listunspent is irrelevant). – Andrew Chow May 16 '18 at 18:47
• Thanks. What i mean by maximumly use/spend is, based on an accounts receive and sending history that the amount which the account legitimately has as a final balance after all previous transactions are able to be sent out of the wallet. Perhaps this visual will explain it better: imgur.com/a/a3qnu5V – Peter Dalby May 16 '18 at 19:29