Tuesday, October 1, 2024

bitcoin core – please i hope to learn about how am i able to modify of switch operate

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 an alternative. Examples of utilization can be found within the transactions-psbt.js integration check file on our Github. A excessive degree rationalization is accessible within the psbt.ts and psbt.js information as properly.
송금 가능. 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 Outcome: [
[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 can’t work and the way to repair it..

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

my souce code is right here..

async operate 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’,
};

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

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

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



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

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

attempt {
    const response = await axios.get(`${mempoolApi}/deal with/${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 way of UTXOs
    utxos.forEach((utxo) => {
        txb.addInput(utxo.txid, utxo.vout);
    });

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

    txb.addOutput(outputScript, outputAmount);

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

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

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


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

    // Add change output if there's any change
    if (changeAmount > 0) {
        const changeAddress = senderAddress; // Change deal with is the sender's deal with on this instance
        const changeScript = bitcoin.deal with.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 shopper.testMempoolAccept([rawTx]);

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


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

    console.log("Transaction broadcasted. Outcome:", 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