Having issues broadcasting BitcoinJS transaction.

0

I'm trying to send testnet with bitcoinjs, but when I go to broadcast the hex, it gives me an error code that says "fee is too high: 12372928". Now, from my understanding, the fee is the input - output. However, even if I increase the output to match the wallet's balance, I get the same exact error message. Here's my code:

        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());
        }
    )

}

Would there be a way to manually set the fee to always be something like .0001?

Setheroni

Posted 2017-12-10T22:05:18.883

Reputation: 7

Answers

0

The miner fee will, by default, equal the inputs - outputs of your transaction.

I notice some problems with your code. You do not specify a change address to send the left-over amount. If I have an unspent output of 100 BTC and I want to send 10 BTC to someone else, I should have 1 input (the unspent output from the previous transaction) and 2 outputs (the receiver address and the change address). The inputs and outputs will look like this:

Inputs

  • Unspent output of 100 BTC

Outputs

  • Receiver address with 10 BTC
  • Change address (your BTC address) with 89.999 BTC

The fee will be 0.001 BTC since (in)(100) - (out)(10 + 89.999) = 0.001.

Hope I was able to help.

Monstrum

Posted 2017-12-10T22:05:18.883

Reputation: 1 021