Transferring BTC on OP_NET

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:

npm install opnet bitcoinjs-lib
# or
yarn add opnet bitcoinjs-lib

Step 2: Setup the Provider

Before transferring BTC, set up the provider to communicate with the OP_NET metaprotocol.

import * as bitcoinjs from "bitcoinjs-lib";
import { JSONRpcProvider } from "opnet";

// Define the OP_NET RPC provider
const rpcUrl = "https://regtest.opnet.org";
const provider = new JSONRpcProvider(rpcUrl);

// Define the Bitcoin network (e.g., regtest, mainnet)
const network = bitcoinjs.networks.regtest;


Step 4: Create and Sign the Transaction

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.
 */
async function transferBTC(walletGet: Wallet, amount: bigint, to: string) {
  // Fetch UTXOs for the wallet, directing to the UTXO-fetching function
  const utxos = await getUTXOs(walletGet.addresses, rpcUrl, amount);

  // Set transaction parameters such as fee, amount, and UTXOs
  const transactionParameters: 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 TransactionFactory
  const transactionFactory = new TransactionFactory();
  const signedTx = await transactionFactory.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
 * @function broadcastTransaction
 * @param {string} signedTx - The signed transaction (hex string) ready for broadcast.
 * @returns Logs the transaction ID or throws an error if the transaction fails.
 */
async function broadcastTransaction(signedTx: string) {
  const limitedProvider = new OPNetLimitedProvider("https://regtest.opnet.org"); // Initialize the OP_NET limited provider
  const result = await limitedProvider.broadcastTransaction(signedTx, false);

  if (!result || !result.result) {
    throw new Error("Transaction failed");
  }
  console.log("Transaction ID:", result.result);
}

// Example usage:
async function main() {
  const walletWif = "your-wallet-private-key-in-wif-format"; // Replace with your WIF private key
  const walletGet = Wallet.fromWif(walletWif, network); // Import wallet using WIF format

  try {
    // Transfer 100,000 satoshis to the destination address
    const signedTx = await transferBTC(
      walletGet,
      100_000n,
      "destination-address"
    );
    await broadcastTransaction(signedTx.tx); // Broadcast the signed transaction
  } catch (error) {
    console.error("Transfer failed:", error); // Catch any errors in the transfer process
  }
}

main();

Last updated