Bitcoinjs-lib: Is there a way to manually add fee amount?

Created on 10 Dec 2017  ·  2Comments  ·  Source: bitcoinjs/bitcoinjs-lib

I've been trying to send testnet and when I attempt to push it onto the network, I get an error that says the fees are too high. So, is there a way for me to set the fee myself rather than input - output? If not, can someone please help me understand why this won't work? Here's the code:

    createWallet = (e) => {
        e.preventDefault()
        var keyPair = bitcoin.ECPair.makeRandom({network: testnet});
        console.log(keyPair.toWIF());
        console.log(keyPair.getAddress());

            this.setState({
                address: 'mphWVWbPgGKuaMpSpGNA3Ffrpva5cRChqP',
                privateKey: 'cW6dLEqkk2HAbQTSJaHnn4w3kd2iJfhBREccEE3vzrq8kcVE5TqS',
            });      
    }

    createTransaction = (e) => {
        e.preventDefault()
        const {address, privateKey} = this.state;
        var hashURL = `https://api.blockcypher.com/v1/btc/test3/addrs/${address}`


            axios.get(hashURL).then((response) => {
                this.setState({
                    hash: response.data.txrefs[0].tx_hash
                })
            }).then((result) => {

            var tx = new bitcoin.TransactionBuilder(testnet);

            var txId = this.state.hash;

            console.log(txId);

            tx.addInput(txId, 0)

            tx.addOutput(this.state.add, 1000000)

            var keyPair2 = bitcoin.ECPair.fromWIF(privateKey, testnet);

            tx.sign(0, keyPair2);
            console.log(tx.build().toHex());
            }
        )

    }
how to / question / docs

Most helpful comment

Yes. Please look up the concept of “change”

If you don’t include a change output (an extra output sending the remainder of the input to yourself) then all of the remainder goes to fees.

You manually set fees implicitly by adjusting the change input so that the leftovers is the fee you wish to send.

All 2 comments

input - output is how you manually set the fee.

Yes. Please look up the concept of “change”

If you don’t include a change output (an extra output sending the remainder of the input to yourself) then all of the remainder goes to fees.

You manually set fees implicitly by adjusting the change input so that the leftovers is the fee you wish to send.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LeonYanghaha picture LeonYanghaha  ·  3Comments

dakk picture dakk  ·  3Comments

coingeek picture coingeek  ·  4Comments

dcousens picture dcousens  ·  3Comments

zhaozhiming picture zhaozhiming  ·  3Comments