In this section, you will learn how to transfer BTC between wallets on the OP_NET metaprotocol. The process involves fetching UTXOs, building a transaction, and broadcasting it to the OP_NET metaprotocol.
Step 1: Install Required Packages
Ensure you have the following dependencies installed in your project:
After fetching the UTXOs, create a transaction, sign it with the sender's private key, and set the fee parameters.
import { IFundingTransactionParameters, TransactionFactory, Wallet,} from"@btc-vision/transaction";/** * Transfers BTC from one wallet to another by creating and signing a transaction. * * @param{Wallet} walletGet - The sender's wallet object, which includes keypairs and addresses. * @param{bigint} amount - The amount of BTC to transfer in satoshis. * @param{string} to - The recipient's address. * @returns The signed transaction object ready for broadcast. */asyncfunctiontransferBTC(walletGet:Wallet, amount:bigint, to:string) {// Fetch UTXOs for the wallet, directing to the UTXO-fetching functionconstutxos=awaitgetUTXOs(walletGet.addresses, rpcUrl, amount);// Set transaction parameters such as fee, amount, and UTXOsconsttransactionParameters:IFundingTransactionParameters= { amount,// Amount to send (in satoshis) utxos,// UTXOs used to fund the transaction signer:walletGet.keypair,// The keypair of the wallet for signing the transaction network,// The BitcoinJS network (e.g., regtest, mainnet) feeRate:100,// Fee rate in satoshis per byte priorityFee:330n,// Extra fee for transaction priority to,// The recipient's Bitcoin address from:walletGet.p2tr,// Sender’s Taproot address (BIP86) };// Create and sign the transaction using TransactionFactoryconsttransactionFactory=newTransactionFactory();constsignedTx=awaittransactionFactory.createBTCTransfer( transactionParameters );return signedTx;}
Step 5: Broadcast the Transaction
Once the transaction is created and signed, broadcast it to the OP_NET metaprotocol using the sendRawTransaction method.
/** * Broadcasts the signed transaction to OP_NET. * * @async * @functionbroadcastTransaction * @param{string} signedTx - The signed transaction (hex string) ready for broadcast. * @returns Logs the transaction ID or throws an error if the transaction fails. */asyncfunctionbroadcastTransaction(signedTx:string) { const limitedProvider = new OPNetLimitedProvider("https://regtest.opnet.org"); // Initialize the OP_NET limited provider
constresult=awaitlimitedProvider.broadcastTransaction(signedTx,false);if (!result ||!result.result) {thrownewError("Transaction failed"); }console.log("Transaction ID:",result.result);}// Example usage:asyncfunctionmain() {constwalletWif="your-wallet-private-key-in-wif-format"; // Replace with your WIF private keyconstwalletGet=Wallet.fromWif(walletWif, network); // Import wallet using WIF formattry {// Transfer 100,000 satoshis to the destination addressconstsignedTx=awaittransferBTC( walletGet,100_000n,"destination-address" );awaitbroadcastTransaction(signedTx.tx); // Broadcast the signed transaction } catch (error) {console.error("Transfer failed:", error); // Catch any errors in the transfer process }}main();