On the OP_NET metaprotocol, wrapping BTC allows you to convert native Bitcoin (BTC) into wrapped BTC (wBTC), which can then be used in smart contracts or traded within the OP_NET ecosystem. In this guide, you’ll learn how to wrap BTC using OP_NET’s transaction handling functionality.
Step 1: Install Required Packages
Ensure you have the following dependencies installed in your project:
Before wrapping BTC, set up the necessary provider and wallet configurations:
import { Wallet } from"@btc-vision/transaction";import*as bitcoinjs from"bitcoinjs-lib";import { JSONRpcProvider } from"opnet";// Define the OP_NET RPC providerconstrpcUrl="https://regtest.opnet.org";constprovider=newJSONRpcProvider(rpcUrl);// Define the Bitcoin network (e.g., regtest, mainnet)constnetwork=bitcoinjs.networks.regtest;// Example of importing a wallet from its private key in WIF formatconstwalletWif="your-wallet-private-key-in-wif-format";constwalletGet=Wallet.fromWif(walletWif, network);console.log("Wallet Taproot Address:",walletGet.p2tr);
Now, fetch the wrapping parameters needed for the transaction:
import { OPNetLimitedProvider } from"@btc-vision/transaction";/** * Fetches the wrapping parameters from the OP_NET RPC. * * @param wrapAmount The amount of BTC you want to wrap * @param rpcUrl The OP_NET RPC URL (e.g., regtest, mainnet) * @returns The wrap parameters required for the wrapping transaction */asyncfunctionfetchWrapParameters(wrapAmount:bigint, rpcUrl:string) {constprovider=newOPNetLimitedProvider(rpcUrl);returnawaitprovider.fetchWrapParameters(wrapAmount);}
Step 5: Wrap BTC
With the UTXOs and wrap parameters, you're ready to wrap BTC into wBTC:
import { currentConsensusConfig, IWrapParameters, TransactionFactory,} from"@btc-vision/transaction";/** * Wraps BTC into wBTC on the OP_NET metaprotocol. * * @param walletGet The wallet object (with private keys and addresses) * @param wrapAmount The amount of BTC to wrap in satoshis * @param network The Bitcoin network (e.g., regtest, mainnet) * @param provider The JSON RPC provider to communicate with OP_NET * @returns Transaction status */asyncfunctionwrapBTC( walletGet:Wallet, wrapAmount:bigint, network:bitcoinjs.Network, provider:JSONRpcProvider) {try {constamountRequired= wrapAmount +currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES+50_000n;constutxos=awaitprovider.utxoManager.getUTXOsForAmount({ address:walletGet.p2tr, amount: wrapAmount, });if (!utxos ||utxos.length===0) {thrownewError("No UTXOs found for the wallet."); }// Fetch the wrapping parameters required for the transactionconstgenerationParameters=awaitfetchWrapParameters(wrapAmount, rpcUrl);if (!generationParameters) {thrownewError("Failed to fetch wrap parameters."); }// Create wrap parameters for the transactionconstwrapParameters:IWrapParameters= { from:walletGet.p2tr,// Sender's Taproot address utxos,// UTXOs for funding the transaction signer:walletGet.keypair,// Signer keypair for the transaction network,// The BitcoinJS network feeRate:100,// Fee rate (satoshis per byte) priorityFee:330n,// Priority fee for faster transaction amount: wrapAmount,// The amount of BTC to wrap generationParameters,// Wrapping-specific parameters };// Create and sign the wrap transactionconsttransactionFactory=newTransactionFactory();constfinalTx=awaittransactionFactory.wrap(wrapParameters);// Broadcast the transactionconstlimitedProvider=newOPNetLimitedProvider("https://regtest.opnet.org" ); // Initialize the OP_NET limited providerconstfirstTxBroadcast=awaitlimitedProvider.broadcastTransaction(finalTx.transaction[0],false );if (!firstTxBroadcast ||!firstTxBroadcast.success) {thrownewError("First transaction broadcast failed."); }constsecondTxBroadcast=awaitlimitedProvider.broadcastTransaction(finalTx.transaction[1],false );if (!secondTxBroadcast ||!secondTxBroadcast.success) {thrownewError("Second transaction broadcast failed."); }return { success:true, txId:secondTxBroadcast.result }; } catch (error) {console.error("Wrapping BTC failed:", error);return { success:false, message: error }; }}// Example usage:asyncfunctionmain() {constwrapAmount=100_000n; // Amount in satoshis to wrapconstresult=awaitwrapBTC(walletGet, wrapAmount, network, provider);if (result.success) {console.log("Successfully wrapped BTC. Transaction ID:",result.txId); } else {console.error("Wrapping failed:",result.message); }}main();