<p>The input you are trying to spend appears to be vout 1 in <a href="https://testnet.blockchain.info/tx/efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36" rel="nofollow noreferrer">the tx</a>.</p>

<p>Your current code tries to spend vout 0, which has already been spent. Try changing <code>transaction.addInput(input.sender_prev_txid, 0);</code> to <code>transaction.addInput(input.sender_prev_txid, 1);</code></p>

CC BY-SA 4.0
2018-05-21T04:27:45.537Z
2018-05-21T04:27:45.537Z
Raghav Sood
<p>I'm trying to setup a simple bitcoin testnet transaction using the bitcoinjs-lib npm module.  It seems like I'm building the transaction correctly, and I receive the hex code but I think I'm unable to broadcast my transaction for some reason.  Here is my code: </p>

<pre><code>const address_1 = 'mzMVV43nRcdGdL9D6xuMez2GkHq8oN1965'; //balance: 130281960 satoshi
const address_2 = 'moCytanji9FNUsDJWVeDGdEFxtGT5psHM6'; //balance: 0 satoshi

const input = {
  sender_prev_txid: 'efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36'
};

const output = {
  address: address_2,
  satoshi: 130200000
};

const createTestnetTransaction = (wif, input, output) =&gt; {
  const transaction = new bitcoin.TransactionBuilder(bitcoin.networks.testnet);
  const sender = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet);

  transaction.setVersion(1);
  transaction.addInput(input.sender_prev_txid, 0);
  transaction.addOutput(output.address, output.satoshi);
  transaction.sign(0, sender);
  console.log(transaction.build().toHex());
};

createTestnetTransaction(wif_1, input, output);
</code></pre>

<p>Using the following hex code:</p>

<pre><code>010000000136ebe684ed9cc3566e1909bf2071c8add8764cc6fcc529e1af6e9beac712c9ef000000006b483045022100ce67916a053e94faf940e81ae665fc81daae226d1a70e7a5bc82adac302a290802207b564151889dd22b9e755297ca502b06f1345106296379762daeae2ac46d0af301210226a0c89db3a526fc5751606bf2592bd1477a4ac0d95616a4dd6b01ba65080a96ffffffff01c0b1c207000000001976a914545ad9c5df8da219b16f8c844498090aa88a764c88ac00000000
</code></pre>

<p>I receive the error: 'Error validating transaction: Transaction efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36 referenced by input 0 of a7f8e0b48c77b5c192f301d655dffcedee1bd8aac2a5a058bb9552b4ff711002 has already been spent..'</p>

<p>If someone could help point out what I'm doing wrong and help my complete the transaction that would be great, thanks!</p>

<transactions><address><testnet><javascript><node.js>
Creating Testnet Bitcoin Transaction with bitcoinjs-lib
ColdCrush
# Creating Testnet Bitcoin Transaction with bitcoinjs-lib

(Asked by: username_0 on 2018-05-21)

I'm trying to setup a simple bitcoin testnet transaction using the bitcoinjs-lib npm module. It seems like I'm building the transaction correctly, and I receive the hex code but I think I'm unable to broadcast my transaction for some reason. Here is my code:

```
const address_1 = 'mzMVV43nRcdGdL9D6xuMez2GkHq8oN1965'; //balance: 130281960 satoshi
const address_2 = 'moCytanji9FNUsDJWVeDGdEFxtGT5psHM6'; //balance: 0 satoshi

const input = {
  sender_prev_txid: 'efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36'
};

const output = {
  address: address_2,
  satoshi: 130200000
};

const createTestnetTransaction = (wif, input, output) => {
  const transaction = new bitcoin.TransactionBuilder(bitcoin.networks.testnet);
  const sender = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet);

  transaction.setVersion(1);
  transaction.addInput(input.sender_prev_txid, 0);
  transaction.addOutput(output.address, output.satoshi);
  transaction.sign(0, sender);
  console.log(transaction.build().toHex());
};

createTestnetTransaction(wif_1, input, output);

```

Using the following hex code:

```
010000000136ebe684ed9cc3566e1909bf2071c8add8764cc6fcc529e1af6e9beac712c9ef000000006b483045022100ce67916a053e94faf940e81ae665fc81daae226d1a70e7a5bc82adac302a290802207b564151889dd22b9e755297ca502b06f1345106296379762daeae2ac46d0af301210226a0c89db3a526fc5751606bf2592bd1477a4ac0d95616a4dd6b01ba65080a96ffffffff01c0b1c207000000001976a914545ad9c5df8da219b16f8c844498090aa88a764c88ac00000000

```

I receive the error: 'Error validating transaction: Transaction efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36 referenced by input 0 of a7f8e0b48c77b5c192f301d655dffcedee1bd8aac2a5a058bb9552b4ff711002 has already been spent..'

If someone could help point out what I'm doing wrong and help my complete the transaction that would be great, thanks!

**Tags:** transactions, address, testnet, javascript, node.js


---

## 1 answers

### Answer by username_1 on 2018-05-21. Score: 2 | Accepted answer

The input you are trying to spend appears to be vout 1 in [the tx](https://testnet.blockchain.info/tx/efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36).

Your current code tries to spend vout 0, which has already been spent. Try changing `transaction.addInput(input.sender_prev_txid, 0);` to `transaction.addInput(input.sender_prev_txid, 1);`


---

