Friday, November 22, 2024

bitcoin core – how am i able to modify of switch perform

that is log of my souce code

[
  {
    txid: 'eb31223773c7b86288e16f63ce16d1ed40ca7b308ab612f3d698db87e33de45e',
    vout: 1,
    status: {
      confirmed: true,
      block_height: 790395,
      block_hash: '0000000000000000000481a20c90090f35f5efb9c7633d275ec9bd4a1cc519f7',
      block_time: 1684465824
    },
    value: 546
  },
  {
    txid: 'd3b262afa2fd6242952bfbed7b6fefd07d1225f58e35c330ca5bae21ad71d9d5',
    vout: 0,
    status: {
      confirmed: true,
      block_height: 826653,
      block_hash: '000000000000000000037cddb5c23599063fbc75a4c701dc143a493a9e1c63c0',
      block_time: 1705825641
    },
    value: 10000
  },
  {
    txid: '2429d9ad4f66b8eb0ea240033cd53cc0944e3a8dac32bc291a237145c9ab092d',
    vout: 0,
    status: {
      confirmed: true,
      block_height: 819740,
      block_hash: '00000000000000000003043fcdbf1f554027bfe32339c8184382704c6b39e690',
      block_time: 1701691113
    },
    value: 546
  }
]
Deprecation Warning: TransactionBuilder can be eliminated sooner or later. (v6.x.x or later) Please use the Psbt class as a substitute. Examples of utilization can be found within the transactions-psbt.js integration take a look at file on our Github. A excessive stage clarification is obtainable within the psbt.ts and psbt.js recordsdata as nicely.
송금 가능. UTXO 합계: 11092
DEPRECATED: TransactionBuilder signal technique arguments will change in v6, please use the TxbSignArg interface
DEPRECATED: TransactionBuilder signal technique arguments will change in v6, please use the TxbSignArg interface
DEPRECATED: TransactionBuilder signal technique arguments will change in v6, please use the TxbSignArg interface
Take a look at Mempool Settle for Consequence: [
  [Object: null prototype] {
    txid: 'e7b368d5ffea7d47039b59b31d5a8e64e6bd8845d697ecb4bdc9bb8889e5c6ac',
    allowed: false,
    'reject-reason': 'missing-inputs'
  }
]
Error fetching or processing UTXOs: bad-txns-inputs-missingorspent

however i do not know why it cannot work and methods to repair it..

who know this downside… please repair or clarify to this issues..

my souce code is right here..

async perform transferBitcoin(senderAddress, senderPrivate, recipientAddress, quantity) {
    //const community = bitcoin.networks.mainnet;
    //getUTX(senderAddress);
    // Bitcoin Core RPC configuration
    const rpcConfig = {
        username: 'raspibolt',
        password: 'test00000',
        port: 8332,
        host: 'localhost',
        community: 'mainnet',
    };



    // Non-public key for sending pockets
    const sendingPrivateKeyWIF = senderPrivate;
    const sendingKeyPair = bitcoin.ECPair.fromWIF(sendingPrivateKeyWIF, community);

    // Non-public key for receiving pockets
    const receivingPrivateKeyWIF = senderPrivate;
    const receivingKeyPair = bitcoin.ECPair.fromWIF(receivingPrivateKeyWIF, community);

    const receivingAddress = bitcoin.funds.p2wpkh({ pubkey: receivingKeyPair.publicKey, community }).tackle;



    quantity = parseInt(btcToSatoshi(quantity));
    // Bitcoin Core consumer
    const consumer = new BitcoinCore(rpcConfig);

    // Mempool.house API for UTXOs
    const mempoolApi = 'https://mempool.house/api';

    strive {
        const response = await axios.get(`${mempoolApi}/tackle/${senderAddress}/utxo`);
        const utxos = response.knowledge;
        console.log(utxos);

        if (utxos.size === 0) {
            console.error('No UTXOs discovered. Aborting transaction.');
            return;
        }

        const txb = new bitcoin.TransactionBuilder(community);

        // Iterate by means of UTXOs
        utxos.forEach((utxo) => {
            txb.addInput(utxo.txid, utxo.vout);
        });

        const outputScript = bitcoin.tackle.toOutputScript(receivingAddress, community);
        const outputAmount = 5000; // Alter this based mostly in your wants

        txb.addOutput(outputScript, outputAmount);

        // Calculate charge
        const charge = 1000; // Alter the charge as wanted

        // Calculate whole enter quantity (sum of UTXO values)
        const totalInputAmount = utxos.scale back((whole, utxo) => whole + utxo.worth, 0);
        const totalOutputAmount = outputAmount + charge;

        // 잔고 확인
        if (totalInputAmount < totalOutputAmount) {
        console.error('잔고 부족. 송금 불가능.');
        } else {
        console.log('송금 가능. UTXO 합계:', totalInputAmount);
        }


        // Calculate change quantity (whole enter - output quantity - charge)
        const changeAmount = totalInputAmount - outputAmount - charge;

        // Add change output if there's any change
        if (changeAmount > 0) {
            const changeAddress = senderAddress; // Change tackle is the sender's tackle on this instance
            const changeScript = bitcoin.tackle.toOutputScript(changeAddress, community);
            txb.addOutput(changeScript, changeAmount);
        }

        // Signal inputs
        utxos.forEach((utxo, index) => {
            txb.signal(index, sendingKeyPair);
        });

        // Construct the transaction
        const tx = txb.construct();
        const rawTx = tx.toHex();
        const txid = crypto.createHash('sha256').replace(Buffer.from(rawTx, 'hex')).digest('hex');

        // Take a look at transaction acceptance within the mempool
        const testResult = await consumer.testMempoolAccept([rawTx]);

        console.log('Take a look at Mempool Settle for Consequence:', testResult);


        // Broadcast the transaction
        const broadcastResult = await consumer.sendRawTransaction(rawTx);

        console.log("Transaction broadcasted. Consequence:", broadcastResult);

    } catch (error) {
        console.error('Error fetching or processing UTXOs:', error.message);
        if (error.response && error.response.knowledge) {
            console.error('Error response knowledge:', error.response.knowledge);
        }
    }

    
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles