hi, we use bitcoinjs to make transactions on the BTC network, but using the examples, seems like always everything is spent, example, we had 0.004 BTC and i did 1 transaction https://live.blockcypher.com/btc/tx/63e38ad4cf4c6b3d0fc8ab71bcfe715edba119776c0fc0d81bbaef8a5c84f830/ and all 0.004 BTC are gone, i guess its because the unspent i receive to use for the transaction is the one of 0.004 i have there, what would be the way to go to only spend the least amount possible? i thought with 0.004 i could do more then 1 transaction, or was that wrong?
what we are trying now is making a lot of small transactions to that wallet in hope that the unspent received will be 1 of these and not all, would that be ok?
The transaction is missing a change output to put the change somewhere (generate a new address and move the 'rest' there). BitcoinJS can't add this for you since it depends on how you generate keys
You should check over some of the developer documentation for transactions on bitcoin.org just to be sure, your application is probably losing a lot of money this way.
what we are trying now is making a lot of small transactions to that wallet in hope that the unspent received will be 1 of these and not all, would that be ok?
No - every one of those outputs has a fee associated with it (mostly due to signatures). If you split coins up into tiny amounts, you start to pay a large amount in fees (if you split it down to 0.000001 you start to clock up more fee than actually adding money to your transaction) - bigger outputs are better than lots of outputs.
So, you should set a change output, and then the coins will go back to your wallet, and eventually get spent again.
Since you've got change now you should check the fee you're paying! It depends on the transaction size, and whatever fee rate the miners seem to accept, but it can be done with some estimation, or try querying an API for it!
does that mean i need to call addOutput twice? once with the hash i want to save as OP_RETURN and the second with the address i want to send the btc/satoshis to?
@elgubenis if you have two add outputs in the transaction you are creating, you'll want to use addOutput twice.
Yeah that's right - put the address you're spending from, and that's where they'll go!
Just avoid creating change outputs < 0.00015 (signatures for P2PKH cost this much atm) cos they're not economical to spend!
care to write a small example? im not sure yet how to pass the actual amount and the actual adress i want to send to
@elgubenis did you want to maybe post what code you have so far?
Ping @elgubenis - happy to write a snippet with the address you provided, but I would need a snippet of the surrounding code if you can.
cool, thanks @dcousens & @afk11 i hope i get back quickly with the example, thanks, and please don't include your own address hawhawhawhaw..
Most helpful comment
The transaction is missing a change output to put the change somewhere (generate a new address and move the 'rest' there). BitcoinJS can't add this for you since it depends on how you generate keys
You should check over some of the developer documentation for transactions on bitcoin.org just to be sure, your application is probably losing a lot of money this way.