Skip to main content

HTTP Provider

The HTTP provider is a core component of the OP_NET metaprotocol. It allows applications to interact with OP_NET nodes over standard HTTP connections, enabling features such as fetching blockchain data, sending transactions, and querying smart contracts.

This guide focuses on setting up the JSONRpcProvider as an HTTP provider for OP_NET.

What is a provider?

A provider facilitates communication between your application and an OP_NET node. It handles:

  • Reading blockchain data (e.g., blocks, transactions, and contracts).
  • Writing data to the blockchain, such as broadcasting transactions.
  • Querying smart contracts deployed on the OP_NET metaprotocol.

The JSONRpcProvider

The JSONRpcProvider, part of the opnet package, is the class responsible for establishing connections with OP_NET nodes. It inherits from the AbstractRpcProvider class and provides methods to send JSON-RPC requests to the node.

Constructor

export declare class JSONRpcProvider extends AbstractRpcProvider {
constructor(url: string, network: Network, timeout?: number);
}
  • url: string: The URL of the OP_NET node. List of active nodes can be found in the Networks guide.
  • network: Network: Network configuration provided by the @btc-vision/bitcoin package.
  • timeout?: number: Optional timeout for RPC calls in milliseconds.

Setting Up an HTTP Provider

import { JSONRpcProvider } from "opnet";
import { networks } from "@btc-vision/bitcoin";

// Define the RPC endpoint and network
const url = "https://regtest.opnet.org";
const network = networks.regtest;

// Initialize the provider
const provider = new JSONRpcProvider(url, network);

// Optional: Set a custom timeout (in milliseconds)
const timeout = 10000; // 10 seconds
const customProvider = new JSONRpcProvider(url, network, timeout);

Best Practices

  • Network Configuration: Always ensure that the network parameter matches the node's configuration to avoid mismatches.
  • Error Handling: Implement robust error handling for failed requests and timeouts.
  • Active Node Selection: Use active nodes from the supported networks table.

What’s Next?

After setting up an HTTP provider, you can proceed to: