Search code examples
javascriptbitcoin

How to set amount of transaction fee using BitcoinJS?


How to setup transaction fee using BitcoinJS?

For example, i have unspent TX on 0.2 BTC lets call it myTx, and i want send to Bob 0.1 BTC, but if i do something like this:

var tx = new bitcoin.TransactionBuilder();
tx.addInput(myTx, 0)
var keyPair = bitcoin.ECPair.fromWIF(privateKeyWIF)
tx.sign(0, keyPair)
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 10000000)
tx.sign(0, keyPair)

It send 0.1 BTC to Bob and take 0.1 as fee, so how to set fee manually when you sending not all BTC from input?


Solution

  • One possibility could be that you sent yourself the amount of bitcoin that you don't want to pay as a fee. For example:

    Send x=0.1 BTC to bob, Send y=0.05 BTC to yourself -> Fee is z=0.05

    Change y as you desire.