Fetching UTXOs on OP_NET

On OP_NET, every transaction requires UTXOs (Unspent Transaction Outputs) as inputs. UTXOs represent the leftover outputs from previous transactions that can be used in new transactions. Fetching UTXOs is essential when sending tokens or interacting with smart contracts on the OP_NET metaprotocol.

In this guide, we will cover two methods for fetching UTXOs:


Step 1: Install Required Packages

Ensure you have the necessary dependencies installed in your project to fetch UTXOs.

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

Fetch UTXOs for a Certain Amount

Sometimes, you need to gather UTXOs for a specific amount of satoshis to fund a transaction. Here’s how you can fetch UTXOs for a specified amount.

import { Wallet } from "@btc-vision/transaction";
import * as bitcoinjs from "bitcoinjs-lib";
import { JSONRpcProvider } from "opnet";

// Example usage:
async function fetchUTXOsForAmount() {
  const walletWif = "your-wallet-private-key-in-wif-format"; // Replace with your WIF private key
  const network = bitcoinjs.networks.regtest; // Define the Bitcoin network (e.g., regtest, mainnet)
  const walletGet = Wallet.fromWif(walletWif, network); // Import wallet using WIF format

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

  // Fetch UTXOs for the wallet address, specifying a required amount
  const utxos = await provider.utxoManager.getUTXOsForAmount({
    address: walletGet.p2tr,
    amount: 100_000n, // Define the required amount of satoshis for the transaction
  });

  // Print the UTXOs
  console.log(utxos);
}

fetchUTXOsForAmount();

Code Breakdown

  • Wallet Setup: We import the wallet using the WIF (Wallet Import Format) key and define the Bitcoin network (e.g., regtest, mainnet).

  • getUTXOsForAmount: This function fetches UTXOs for the specified amount (in this case, 100,000 satoshis).

  • UTXO Output: The UTXOs are fetched for the address, ensuring that the total value covers the specified amount.

Fetch UTXOs Without a Specified Amount

Sometimes, you may want to retrieve all available UTXOs for a wallet without specifying an amount. Here's how you can do that:

import { Wallet } from "@btc-vision/transaction";
import * as bitcoinjs from "bitcoinjs-lib";
import { JSONRpcProvider } from "opnet";

// Example usage:
async function fetchUTXOsWithoutAmount() {
  const walletWif = "your-wallet-private-key-in-wif-format"; // Replace with your WIF private key
  const network = bitcoinjs.networks.regtest; // Define the Bitcoin network (e.g., regtest, mainnet)
  const walletGet = Wallet.fromWif(walletWif, network); // Import wallet using WIF format

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

  // Fetch all available UTXOs for the wallet address without specifying an amount
  const utxos = await provider.utxoManager.getUTXOs({
    address: walletGet.p2tr,
  });

  // Print the UTXOs
  console.log(utxos);
}

fetchUTXOsWithoutAmount();

Code Breakdown

  • getUTXOs: This method retrieves all available UTXOs for the specified address, without any filtering for a particular amount.

  • Use Case: This method is useful when you simply need to see all available outputs for a given wallet address.

Last updated