<p>I'm implementing the code to sign and send Bitcoin transactions on <strong>testnet</strong>, using the <strong>bitcore</strong> library, in javascript.</p>
<p>I need some help, as once I try to broadcast my transaction, it gives me this error:</p>
<blockquote>
<p>Error: txn-mempool-conflict</p>
</blockquote>
<p>I've seen online that these are conflicts with another transaction in the mempool, but I really can't find any code examples online to look at. Can someone help me, giving me a practical explanation of how I can do it? Any tips and tricks on the code are welcome.</p>
<p>Here is the code:</p>
<pre class="lang-js prettyprint-override"><code>async function signAndSendTransaction(address, toAddress, amount, addressPrivateKey) {
  try {
    const addressBalance = await getBalance(address);
    const toAddressBalance = await getBalance(toAddress);

    const amountInSatoshis = amount * SATOSHIS_PER_BTC;
    const addressBalanceInSatoshi = addressBalance * SATOSHIS_PER_BTC;
    console.log(&quot;AMOUNT IN SATOSHI \n&quot; + amountInSatoshis);

    const toAddressBalanceInSatoshi = toAddressBalance * SATOSHIS_PER_BTC;

    console.log(&quot;BALANCE ADDRESS \n&quot; + addressBalanceInSatoshi);

    console.log(&quot;BALANCE TO-ADDRESS \n&quot; + toAddressBalanceInSatoshi);
    if (addressBalanceInSatoshi &lt; amountInSatoshis) {
      console.log(`can't send ${amount} BTC to ${address} beacuse the balance is less than the tx amount.`);
      return;
    }

    const utxos = (await axios.get(`https://api.bitcore.io/api/BTC/testnet/address/${address}/?unspent=true`)).data;

    if (utxos.length === 0) {
      console.log(`There is no utxo for ${address}.`);
      return;
    }

    const unspentOutputs = utxos.map((utxo) =&gt; new bitcore.Transaction.UnspentOutput({
      txId: utxo.mintTxid,
      outputIndex: utxo.mintIndex,
      address: utxo.address,
      script: utxo.script,
      satoshis: utxo.value,
    }));

    let tx = new bitcore.Transaction();
    console.log(&quot;\n --------Unspent Outputs-------- \n&quot;);
    console.log(unspentOutputs);
    console.log(tx);
    tx.from(unspentOutputs);
    tx.to(toAddress, amountInSatoshis);
    tx.change(address);

    const estimatedFee = tx.getFee();

    console.log(estimatedFee);
    tx.fee(estimatedFee);

    // Private key of the specified public address that is sending BTC
    tx.sign(addressPrivateKey);

    console.log(&quot;\n --------TX OBJECT-------- \n&quot;);
    console.log(tx.toObject());

    const serializedTX = tx.serialize();
    console.log(serializedTX);

    const txid = (await axios.post(&quot;https://api.bitcore.io/api/BTC/testnet/tx/send&quot;, { rawTx: serializedTX })).data;
    console.log(txid);
  } catch (error) {
    console.error(`Error: ${error.message}`);
  }
}
</code></pre>

CC BY-SA 4.0
2023-06-01T19:05:59.047Z
Bitcoin uses the “UTXO model” to keep track of funds in the system. Each piece of bitcoin is uniquely identifiable and can only be spent once. It looks like you are trying to use the same UTXO more than once in transaction building.
Murch
CC BY-SA 4.0
2023-06-02T08:22:36.840Z
Where do you understand this from? Could you give me more details please? Where do you understand this from? Could you give me more details please? How can I avoid what you told me?
AlexCav
CC BY-SA 4.0
2023-06-02T14:20:51.733Z
`Error: txn-mempool-conflict` indicates that the new transaction is spending an input a previous transaction already spent.
Murch
<transactions><testnet><mempool><javascript><bitcore>
Transactions only work the first time
AlexCav
# Transactions only work the first time

(Asked by: username_0 on 2023-06-01)

I'm implementing the code to sign and send Bitcoin transactions on **testnet**, using the **bitcore** library, in javascript.

I need some help, as once I try to broadcast my transaction, it gives me this error:

> Error: txn-mempool-conflict

I've seen online that these are conflicts with another transaction in the mempool, but I really can't find any code examples online to look at. Can someone help me, giving me a practical explanation of how I can do it? Any tips and tricks on the code are welcome.

Here is the code:

```js
async function signAndSendTransaction(address, toAddress, amount, addressPrivateKey) {
  try {
    const addressBalance = await getBalance(address);
    const toAddressBalance = await getBalance(toAddress);

    const amountInSatoshis = amount * SATOSHIS_PER_BTC;
    const addressBalanceInSatoshi = addressBalance * SATOSHIS_PER_BTC;
    console.log("AMOUNT IN SATOSHI \n" + amountInSatoshis);

    const toAddressBalanceInSatoshi = toAddressBalance * SATOSHIS_PER_BTC;

    console.log("BALANCE ADDRESS \n" + addressBalanceInSatoshi);

    console.log("BALANCE TO-ADDRESS \n" + toAddressBalanceInSatoshi);
    if (addressBalanceInSatoshi < amountInSatoshis) {
      console.log(`can't send ${amount} BTC to ${address} beacuse the balance is less than the tx amount.`);
      return;
    }

    const utxos = (await axios.get(`https://api.bitcore.io/api/BTC/testnet/address/${address}/?unspent=true`)).data;

    if (utxos.length === 0) {
      console.log(`There is no utxo for ${address}.`);
      return;
    }

    const unspentOutputs = utxos.map((utxo) => new bitcore.Transaction.UnspentOutput({
      txId: utxo.mintTxid,
      outputIndex: utxo.mintIndex,
      address: utxo.address,
      script: utxo.script,
      satoshis: utxo.value,
    }));

    let tx = new bitcore.Transaction();
    console.log("\n --------Unspent Outputs-------- \n");
    console.log(unspentOutputs);
    console.log(tx);
    tx.from(unspentOutputs);
    tx.to(toAddress, amountInSatoshis);
    tx.change(address);

    const estimatedFee = tx.getFee();

    console.log(estimatedFee);
    tx.fee(estimatedFee);

    // Private key of the specified public address that is sending BTC
    tx.sign(addressPrivateKey);

    console.log("\n --------TX OBJECT-------- \n");
    console.log(tx.toObject());

    const serializedTX = tx.serialize();
    console.log(serializedTX);

    const txid = (await axios.post("https://api.bitcore.io/api/BTC/testnet/tx/send", { rawTx: serializedTX })).data;
    console.log(txid);
  } catch (error) {
    console.error(`Error: ${error.message}`);
  }
}

```

**Tags:** transactions, testnet, mempool, javascript, bitcore

> **Comments:**
> **username_1**: Bitcoin uses the “UTXO model” to keep track of funds in the system. Each piece of bitcoin is uniquely identifiable and can only be spent once. It looks like you are trying to use the same UTXO more than once in transaction building.  
> **username_0**: Where do you understand this from? Could you give me more details please? Where do you understand this from? Could you give me more details please? How can I avoid what you told me?  
> **username_1**: `Error: txn-mempool-conflict` indicates that the new transaction is spending an input a previous transaction already spent.  

---

## 0 answers

