Saturday, February 22, 2025

bitcoin core – Error in sending transaction:Insufficient funds

import Client from "bitcoin-core";
import fs from "fs";
const client = new Client({
  network: "regtest",
  username: "alice",
  password: "password",
  host: "http://alice:password@localhost:18443",
});
const SENDER_ADDRESS = "bcrt1qq2yshcmzdlznnpxx258xswqlmqcxjs4dssfxt2";

async function setTransactionFee() {
  try {
    const success = await client.setTxFee(0.00001);
    console.log("Transaction fee rate set to 21 sats/vB");
  } catch (error) {
    console.log("Error setting fee rate:", error);
  }
}
async function main() {
  // await client.loadWallet("testwallet");
  try {
    const address = await client.getNewAddress();
    console.log("Address:", address);
    await client.generateToAddress(103, address);
    const balance = await client.getBalance();
    console.log(balance);
    const txid1 = await client.sendToAddress(SENDER_ADDRESS, 100);
    console.log("Sent 100 BTC, TXID:", txid1);

    // 6. Get raw transaction details
    const txDetails = await client.getRawTransaction(txid1, true);

    // 7. Construct Raw Transaction
    const rawTx = await client.createRawTransaction(
      [
        {
          txid: txid1,
          vout: 0,
        },
      ],
      {
        bcrt1qq2yshcmzdlznnpxx258xswqlmqcxjs4dssfxt2: 100,
        data: Buffer.from("We are all Satoshi!!", "utf8").toString("hex"),
      }
    );

    // 8. Fund the raw transaction with required fee rate
    const fundedTx = await client.fundRawTransaction(rawTx, { fee_rate: 21 });
    console.log("Funded TX:", fundedTx);

    // 9. Sign the transaction
    const signedTx = await client.signRawTransactionWithWallet(fundedTx.hex);
    console.log("Signed TX:", signedTx);

    // 10. Broadcast the transaction
    const finalTxid = await client.sendRawTransaction(signedTx.hex);
    console.log("Final Transaction ID:", finalTxid);

    // 11. Write TXID to a file
    fs.writeFileSync("../out.txt", finalTxid);
    console.log("Transaction ID written to out.txt");
  } catch (err) {
    console.error("Error:", err);
  }
}
try {
  main();
} catch (e) {
  console.log("Error:", e);
}

I am trying to run this transaction
but getting the following error

Address: bcrt1q930lz38fuqaw83ujh8s050yyntaev2ll6xrzvu
11649.99941734
Sent 100 BTC, TXID: d6f19c05ec6f5022907b38c521ac387d8c3186f30ecef81d2bd1a40703d52eed
Error: RpcError: Insufficient funds

I have enough balance as u can see what am i doing wrong here?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles