I am implementing the code to signal and ship Bitcoin transactions on testnet, utilizing the bitcore library, in javascript.
I want some assist, as as soon as I attempt to broadcast my transaction, it provides me this error:
Error: txn-mempool-conflict
I’ve seen on-line that these are conflicts with one other transaction within the mempool, however I actually cannot discover any code examples on-line to have a look at. Can somebody assist me, giving me a sensible clarification of how I can do it? Any ideas and methods on the code are welcome.
Right here is the code:
async operate signAndSendTransaction(deal with, toAddress, quantity, addressPrivateKey) {
attempt {
const addressBalance = await getBalance(deal with);
const toAddressBalance = await getBalance(toAddress);
const amountInSatoshis = quantity * 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(`cannot ship ${quantity} BTC to ${deal with} beacuse the steadiness is lower than the tx quantity.`);
return;
}
const utxos = (await axios.get(`https://api.bitcore.io/api/BTC/testnet/deal with/${deal with}/?unspent=true`)).knowledge;
if (utxos.size === 0) {
console.log(`There isn't any utxo for ${deal with}.`);
return;
}
const unspentOutputs = utxos.map((utxo) => new bitcore.Transaction.UnspentOutput({
txId: utxo.mintTxid,
outputIndex: utxo.mintIndex,
deal with: utxo.deal with,
script: utxo.script,
satoshis: utxo.worth,
}));
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(deal with);
const estimatedFee = tx.getFee();
console.log(estimatedFee);
tx.price(estimatedFee);
// Non-public key of the desired public deal with that's sending BTC
tx.signal(addressPrivateKey);
console.log("n --------TX OBJECT-------- n");
console.log(tx.toObject());
const serializedTX = tx.serialize();
console.log(serializedTX);
const txid = (await axios.publish("https://api.bitcore.io/api/BTC/testnet/tx/ship", { rawTx: serializedTX })).knowledge;
console.log(txid);
} catch (error) {
console.error(`Error: ${error.message}`);
}
}